[英]Converting dates when importing from CSV, OutOfBoundsDatetime: Out of bounds nanosecond timestamp. Pandas
I'm importing data from a csv, and I'm trying to set a specific date to today's date.我正在从 csv 导入数据,并且正在尝试将特定日期设置为今天的日期。
Data in the csv if formatted this way:如果以这种方式格式化,csv 中的数据:
All data in that column are dates and are formatted exactly the same.该列中的所有数据都是日期并且格式完全相同。 I read in the data with
df = pd.read_csv(r'<filapath.csv>)
at the moment.我现在用
df = pd.read_csv(r'<filapath.csv>)
读入数据。
Then this is run to convert all instances of '7/21/2020' into today's date:然后运行此命令以将“2020 年 7 月 21 日”的所有实例转换为今天的日期:
df['filedate'] = np.where(pd.to_datetime(df['filedate']) == '7/21/2020', pd.Timestamp('now').floor(freq='d'),df['filedate'])
I receive this error: pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-14 00:00:00
我收到此错误:
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-14 00:00:00
I don't want to use errors='coerce'
because the column will always be 100% populated with real dates, and I will later need to filter the dataframe by date.我不想使用
errors='coerce'
因为该列将始终 100% 填充真实日期,并且稍后我需要按日期过滤 dataframe。 There seems to be some "ghost" precision in the csv data I can't see. csv 数据中似乎有一些“幽灵”精度,我看不到。 I cannot modify the csv column in this case and I can't use any packages outside of pandas and numpy.
在这种情况下,我无法修改 csv 列,也无法使用 pandas 和 numpy 之外的任何包。
...or alternatively .loc
: ...或者
.loc
:
df.loc[df['filedate'] == '7/21/2020', 'filedate'] = pd.Timestamp('now').floor(freq='d')
Use.replace() function.使用.replace() function。
df['filedate'].replace({'7/21/2020':pd.Timestamp('now').floor(freq='d')})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.