如果日期无效,Polars read_parquet 方法会将原始日期值转换为不同的值

问题描述 投票:0回答:1

我正在使用极坐标从 S3 存储桶读取镶木地板文件,下面是我使用的代码:

df = pl.read_parquet(parquet_file_name, storage_options=storage_options, hive_partitioning=False)

在S3存储桶中,日期列的值(由于年份为0200,该值无效)存储为

 start_date = 0200-03-01 00:00:00

使用 Polars.read_parquet 方法从 S3 存储桶读取此值后,它会在内部将日期转换为

 start_date = 1953-10-28 10:43:41.128654848

并将该列的极坐标数据框中的数据类型设置为 Datetime(time_unit='ns', time_zone=None)

有什么方法可以保留日期,即使它是无效的?我尝试进行强制转换,但没有帮助,因为 read_parquet 方法在内部将其读取为 1953-10-28,并且在已转换的值之上执行任何强制转换都没有帮助。

如有任何帮助,我们将不胜感激。

注意:另外,为了进行比较,我尝试使用 pandas,它的行为与 Polars 相同,即 start_date 是 1953-10-28 10:43:41.128654848

python pandas python-polars
1个回答
0
投票

郑重声明,“0200”根本不是无效年份。我无法重现您的问题:

>>> df = pl.Series("d", ["0200-03-01 00:00:00"]).str.to_datetime().to_frame()
>>> df
shape: (1, 1)
┌─────────────────────┐
│ d                   │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 0200-03-01 00:00:00 │
└─────────────────────┘
>>> df.write_parquet("/tmp/df.parquet")
>>> pl.read_parquet("/tmp/df.parquet")
shape: (1, 1)
┌─────────────────────┐
│ d                   │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 0200-03-01 00:00:00 │
└─────────────────────┘

镶木地板本身的数据可能已损坏。

© www.soinside.com 2019 - 2024. All rights reserved.