作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我很困惑 pandas 是如何用这些行超出日期时间对象的范围的:
import pandas as pd
BOMoffset = pd.tseries.offsets.MonthBegin()
# here some code sets the all_treatments dataframe and the newrowix, micolix, mocolix counters
all_treatments.iloc[newrowix,micolix] = BOMoffset.rollforward(all_treatments.iloc[i,micolix] + pd.tseries.offsets.DateOffset(months = x))
all_treatments.iloc[newrowix,mocolix] = BOMoffset.rollforward(all_treatments.iloc[newrowix,micolix]+ pd.tseries.offsets.DateOffset(months = 1))
这里 all_treatments.iloc[i,micolix]
是由 pd.to_datetime(all_treatments['INDATUMA'], errors='coerce',format='%Y% m%d')
,INDATUMA
是日期信息,格式为20070125
。
这个逻辑似乎适用于模拟数据(没有错误,日期有意义),所以目前我无法重现,而它在我的整个数据中失败并出现以下错误:
pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2262-05-01 00:00:00
最佳答案
由于 pandas 以纳秒分辨率表示时间戳,因此可以使用 64 位整数表示的时间跨度被限制为大约 584 年
In [54]: pd.Timestamp.min
Out[54]: Timestamp('1677-09-22 00:12:43.145225')
In [55]: pd.Timestamp.max
Out[55]: Timestamp('2262-04-11 23:47:16.854775807')
您的值超出了 2262-05-01 00:00:00 的范围,因此出现了越界错误
直接出:https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timestamp-limitations
解决方法:
这将强制超出界限的日期为 NaT
pd.to_datetime(date_col_to_force, errors = 'coerce')
关于python - 偏移量前滚后加上一个月偏移量后的 Pandas 超出纳秒时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32888124/
我是一名优秀的程序员,十分优秀!