FloatYearToDatetime
Base classes
| Name |
Children |
Inherits |
ColumnOp datacook.engine.ops.series The base of all per-column ops: one series per selected column, put back by `assign()`. |
|
|
⋔ Inheritance diagram
graph TD
2405312689["dates.FloatYearToDatetime"]
124287307["series.ColumnOp"]
1539482213["base.Op"]
2751680898["builtins.object"]
124287307 --> 2405312689
1539482213 --> 124287307
2751680898 --> 1539482213
🛈 DocStrings
Bases: ColumnOp
Old pandastools float_year_to_datetime: 2020.5 -> 2020-07-02 (365.25 days a year).
Source code in src/datacook/engine/ops/dates.py
| class FloatYearToDatetime(series.ColumnOp):
"""Old pandastools `float_year_to_datetime`: 2020.5 -> 2020-07-02 (365.25 days a year)."""
id = "float_year_to_datetime"
title = _("Decimal year to date / time")
description = _("Convert decimal years (2020.5) into date / time values.")
category = categories.DATETIME
icon = "mdi.calendar-arrow-right"
@classmethod
def series(cls, params: Any, frame: Frame, column: str) -> pd.Series:
import pandas as pd
years = frame[column].astype("float64")
start = pd.to_datetime((years // 1).astype("Int64").astype("string"), format="%Y")
return start + pd.to_timedelta(years % 1 * 365.25, unit="D")
@classmethod
def expr(cls, params: Any, var: str, column: str) -> str:
years = f'{series.column_code(var, column)}.astype("float64")'
return (
f'pd.to_datetime(({years} // 1).astype("Int64").astype("string"), format="%Y")'
f' + pd.to_timedelta({years} % 1 * 365.25, unit="D")'
)
@classmethod
def match_expr(cls, node: ast.expr, var: str) -> tuple[str, dict[str, Any]] | None:
for sub in ast.walk(node):
if (column := series.column_of(sub, var)) is not None:
return column, {} # the rest is checked by regenerating the code
return None
|
:fa-brands-github: Show source on GitHub