我想从包含标准 iso 格式“yyyy-mm-dd”的日期的 json 字符串中读取极坐标数据帧。 当我尝试读取字符串并使用
schema
或 schema_override
设置日期列的 dtype 时,这只会导致 NULL 值。
from datetime import datetime, timedelta
from io import StringIO
import polars as pl
# Generate a list of dates
start_date = datetime.today()
dates = [start_date + timedelta(days=i) for i in range(100)]
date_strings = [date.strftime("%Y-%m-%d") for date in dates]
# Create a Polars DataFrame
df = pl.DataFrame({"dates": date_strings})
df_reread = pl.read_json(
StringIO(df.write_json()),
schema_overrides={"dates": pl.Date},
)
输出
print(df_reread)
shape: (100, 1)
┌───────┐
│ dates │
│ --- │
│ date │
╞═══════╡
│ null │
│ null │
│ null │
│ null │
│ null │
│ … │
│ null │
│ null │
│ null │
│ null │
│ null │
└───────┘
有没有办法从 json 字符串中正确读取日期数据类型?
from datetime import datetime, timedelta
from io import StringIO
import polars as pl
# Generate a list of dates
start_date = datetime.today()
dates = [start_date + timedelta(days=i) for i in range(100)]
date_strings = [date.strftime("%Y-%m-%d") for date in dates]
# Create a Polars DataFrame
df = pl.DataFrame({"dates": date_strings})
# Write DataFrame to JSON and read it back in
df_reread = pl.read_json(StringIO(df.write_json()))
# Explicitly cast the 'dates' column to Date type
df_reread = df_reread.with_columns([
pl.col("dates").str.strptime(pl.Date, "%Y-%m-%d")
])
print(df_reread)