Python 偏移前滚加上一个月的偏移后,熊猫越界纳秒时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32888124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:25:59  来源:igfitidea点击:

pandas out of bounds nanosecond timestamp after offset rollforward plus adding a month offset

pythondatetimepandasdatetimeoffset

提问by László

I am confused how pandas blew out of bounds for datetime objects with these lines:

我很困惑熊猫如何用这些行超出日期时间对象的范围:

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))

Here all_treatments.iloc[i,micolix]is a datetime set by pd.to_datetime(all_treatments['INDATUMA'], errors='coerce',format='%Y%m%d'), and INDATUMAis date information in the format 20070125.

这里all_treatments.iloc[i,micolix]是设置日期时间pd.to_datetime(all_treatments['INDATUMA'], errors='coerce',format='%Y%m%d'),并INDATUMA在格式的最新信息20070125

This logic seems to work on mock data (no errors, dates make sense), so at the moment I cannot reproduce while it fails in my entire data with the following error:

这个逻辑似乎适用于模拟数据(没有错误,日期有意义),所以目前我无法重现,而它在我的整个数据中失败并出现以下错误:

pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2262-05-01 00:00:00

采纳答案by Shankar ARUL - jupyterdata.com

Since pandas represents timestamps in nanosecond resolution, the timespan that can be represented using a 64-bit integer is limited to approximately 584 years

由于 pandas 以纳秒分辨率表示时间戳,因此可以使用 64 位整数表示的时间跨度被限制为大约 584 年

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')

And your value is out of this range 2262-05-01 00:00:00 and hence the outofbounds error

并且您的值超出此范围 2262-05-01 00:00:00 因此出现越界错误

Straight out of: http://pandas-docs.github.io/pandas-docs-travis/timeseries.html#timestamp-limitations

直接出自:http: //pandas-docs.github.io/pandas-docs-travis/timeseries.html#timestamp-limitations

回答by Pawel Kranzberg

Setting the errorsparameter in pd.to_datetimeto 'coerce'causes replacement of out of bounds values with NaT. Quoting the docs:

errors参数设置pd.to_datetime'coerce'会导致用 替换越界值NaT。引用文档

If ‘coerce', then invalid parsing will be set as NaT

如果'coerce',则无效解析将被设置为NaT

E.g.:

例如:

datetime_variable = pd.to_datetime(datetime_variable, errors = 'coerce')

This does not fix the data (obviously), but still allows processing the non-NaT data points.

这不会修复数据(显然),但仍然允许处理非 NaT 数据点。

回答by EdivaldoJunior

None of above are so good, because it will delete your data. But, you can only mantain and edit your conversion:

以上都不是很好,因为它会删除您的数据。但是,您只能维护和编辑您的转换:

# convertin from epoch to datatime mantainig the nanoseconds timestamp
xbarout= pd.to_datetime(xbarout.iloc[:,0],unit='ns')