簡體   English   中英

pandas read_parquet 錯誤地導入日期字段

[英]pandas read_parquet imports date field incorrectly

我有一個名為 BusinessDate 的鑲木地板文件,其中包含一個日期字段。 當我將其導入 dataframe 時,它會自動確定字段 BusinessDate 為日期(datetime64 [ns,UTC])。 但是,由於此 BusinessDate 字段的格式為 YYYY-MM-DD,因此其中一些日期被錯誤地導入。 例如 2013-02-01 應該是 2013 年 2 月的第一個,而實際上它被解釋為 2013 年 1 月的第二個。在導入 parquet 文件時是否可以設置 BusinessDate 字段的正確格式?

最初我使用:

df.read_parquet('data.parquet')

如果我有一個 csv 文件,我的解決方案是:

custom_date_parser = lambda x: datetime.strptime(x, '%Y-%m-%d')
df.read_csv('data.csv',parse_dates=['BusinessDate'], date_parser=custom_date_parser)

但是,當我嘗試使用類似代碼嘗試修復日期問題時,會出現錯誤:

custom_date_parser = lambda x: datetime.strptime(x, '%Y-%m-%d')
df.read_parquet('data.parquet',parse_dates=['BusinessDate'], date_parser=custom_date_parser)

該錯誤是由於 read_parquet function 沒有 parse_dates 或 date_parser 屬性而導致的,例如 read_csv function 確實有。

所以我的問題是:如何在 pandas 中導入鑲木地板文件,以便將“BusinessDate”字段正確導入為正確格式的日期,在我的情況下為 YYYY-MM-DD。 Or in case this not possible with pandas read_parquet function, is it possible to import the 'BusinessDate' field as a string field in a pandas dataframe so that I can change it afterwards.

好問題。 Pandas 尚無此功能。

閱讀鑲木地板后,我建議您使用 lambda function ,如下所示:

df['new_col'] = df['col'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM