-
Notifications
You must be signed in to change notification settings - Fork 29
Function `baltic.decimalDate`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The decimalDate()
function in BALTIC converts calendar dates in a specified format to a decimal date representation. A decimal date represents the fraction of the year that has passed by the given date.
def decimalDate(date, fmt="%Y-%m-%d", variable=False)
-
date
(str): The date to be converted. -
fmt
(str): The format of the input date string. Default is "%Y-%m-%d". -
variable
(bool): If True, allows for variable date precision. Default is False.
- float: The decimal representation of the date.
- Parses the input date string according to the specified format.
- If
variable
is True, adjusts the format to match the available precision in the date. - Converts the date to a datetime object.
- Calculates the fraction of the year that has passed up to the given date.
- Returns the year plus this fraction as a decimal number.
- Converting dates to a continuous numerical representation for time-based analyses.
- Handling dates with different levels of precision in a single dataset.
- Preparing date data for plotting on a continuous time axis.
- Performing date arithmetic or comparisons.
import baltic as bt
# Basic usage
print(bt.decimalDate("2023-05-23"))
# Output: 2023.3890410958904
# Using variable precision
print(bt.decimalDate("2023", fmt="%Y", variable=True))
# Output: 2023.0
print(bt.decimalDate("2023-05", fmt="%Y-%m", variable=True))
# Output: 2023.3306849315068
# Custom date format
print(bt.decimalDate("23/05/2023", fmt="%d/%m/%Y"))
# Output: 2023.3890410958904
- When
variable
is True, the function can handle dates with different levels of precision:- A year-only date (e.g., "2023") will be interpreted as January 1st of that year.
- A year-month date (e.g., "2023-05") will be interpreted as the first day of that month.
- The function uses the total seconds in the year to calculate the decimal portion, accounting for leap years.
- If the input date string doesn't match the specified format, a
ValueError
will be raised. - This function is particularly useful in phylogenetic analyses where a continuous representation of time is needed.
Wiki written with the assistance of claude.ai 3.5 "Sonnet".
- Core
baltic
classes:-
Class
tree
- Tree Construction and Manipulation methods
- Tree Analysis methods
- Tree Conversion and Output methods
- Tree Visualization
- Utility Methods
- Class
leaf
- Class
node
- Class
clade
- Class
reticulation
-
Class