Polarsで日時を扱う
from PolarsのExpression
#wip
型
Date
e.g. 2014-07-08
内部では32bitのUNIX時間で表される
Datetime
https://docs.pola.rs/api/python/stable/reference/api/polars.datatypes.Datetime.html
e.g. 2014-07-08 07:00:00
内部では64bitのUNIX時間で表される
Duration
A time delta type that is created when subtracting Date/Datetime.
Similar to timedelta in Python.
Time
Time representation, internally represented as nanoseconds since midnight.
Parsing - Polars user guide
pl.read_csvでparse時に型を頑張って推論
code:py
df = pl.read_csv("docs/assets/data/apple_stock.csv", try_parse_dates=True)
cast
code:py
df = df.with_columns(pl.col("Date").str.to_date("%Y-%m-%d"))
.dtで日時操作できる
code:py
df_with_year = df.with_columns(pl.col("Date").dt.year().alias("year"))
Filtering - Polars user guide
Pythonのdatetimeを使う
code:py
from datetime import datetime
範囲
code:py
filtered_range_df = df.filter(
pl.col("Date").is_between(datetime(1995, 7, 1), datetime(1995, 11, 1)),
)
print(filtered_range_df)
https://docs.pola.rs/user-guide/transformations/time-series/rolling/
https://docs.pola.rs/user-guide/transformations/time-series/resampling/
https://docs.pola.rs/user-guide/transformations/time-series/timezones/
https://docs.pola.rs/api/python/dev/reference/expressions/api/polars.datetime.html
https://docs.pola.rs/api/python/stable/reference/api/polars.datatypes.Datetime.html