Предположим, мы создаем несколько дат:
import polars as pl
df = pl.DataFrame(
[
pl.Series("start", ["2023-01-01"], dtype=pl.Date).str.to_date(),
pl.Series("end", ["2024-01-01"], dtype=pl.Date).str.to_date(),
]
)
Теперь я могу создать диапазон дат из них:
dates = pl.date_range(df[0, "start"], df[0, "end"], "1mo", eager=True)
Но я хочу определить функцию, которая принимает пару дат и выдает диапазон в качестве оболочки pl.date_range
:
def my_date_range(start: pl.Date, end: pl.Date) -> pl.Series:
return pl.date_range(start, end, "1mo", eager=True)
Вышеуказанное не проверяется с помощью pyright
/Pylance, потому что:
Argument of type "Date" cannot be assigned to parameter "start" of type "IntoExprColumn | date | datetime" in function "date_range"
Type "Date" is incompatible with type "IntoExprColumn | date | datetime"
"Date" is incompatible with "date"
"Date" is incompatible with "datetime"
"Date" is incompatible with "Expr"
"Date" is incompatible with "Series"
"Date" is incompatible with "str"PylancereportArgumentType
Если я проверю type(df[0, "start"])
, я увижу:
datetime.date
и pl.Date
бесполезен, потому что isinstance(df[0, "start"], pl.Date) == False
.
Я не могу понять, как импортировать datetime.date
, чтобы использовать его в качестве аннотации типа (попытка import polars.datetime as dt
поднимает No module named 'polars.datetime'
).
Как это может быть сделано? Или, другими словами: как следует аннотировать аргументы даты my_date_range
?
Поскольку datetime.date
совместим с параметрами start
и end
, ожидаемыми pl.date_range()
, этого должно быть достаточно:
import polars as pl
from datetime import date
def my_date_range(start: date, end: date) -> pl.Series:
return pl.date_range(start, end, "1mo", eager=True)