gpt4 book ai didi

python - 偏移量前滚后加上一个月偏移量后的 Pandas 超出纳秒时间戳

转载 作者:IT老高 更新时间:2023-10-28 22:06:50 27 4
gpt4 key购买 nike

我很困惑 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com