简体   繁体   English

从 CSV 导入时转换日期,OutOfBoundsDatetime:超出范围纳秒时间戳。 Pandas

[英]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 中的数据:

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.

相关问题 OutOfBoundsDatetime:越界纳秒时间戳 - OutOfBoundsDatetime: Out of bounds nanosecond timestamp 返回特定月份和年份的 df 行 python pandas OutOfBoundsDatetime:越界纳秒时间戳:1-01-01 00:00:00 - Return rows of df of particular month and year python pandas OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00 OutOfBoundsDatetime:越界纳秒时间戳:1-01-01 00:00:34 - OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34 超出范围的纳秒级时间戳 - Out of bounds nanosecond timestamp 将列转换为日期时间格式时显示超出范围的纳秒时间戳错误 - Showing out of bounds nanosecond timestamp error when converting column to datetime format 越界纳秒时间戳:日期的 1-01-01 00:00:00 - Out of bounds nanosecond timestamp: 1-01-01 00:00:00 for dates Python Pandas to_datetime pandas.datetime的出纳秒时间戳 - Python Pandas to_datetime Out of bounds nanosecond timestamp on a pandas.datetime 如何解决Python Pandas DataFrame的“ Outbounds nanosecond timestamp”错误? - How to work around Python Pandas DataFrame's “Out of bounds nanosecond timestamp” error? 偏移量前滚后加上一个月偏移量后的熊猫超出纳秒时间戳 - pandas out of bounds nanosecond timestamp after offset rollforward plus adding a month offset 超出范围的纳秒级时间戳记:1-01-01 00:00:00 - Out of bounds nanosecond timestamp: 1-01-01 00:00:00
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM