偏移量前滚后加上增加一个月偏移量后,熊猫出界纳秒时间戳

我很困惑,为什么大熊猫会用下面这些行打破日期时间对象的界限:

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
171702 次浏览

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

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

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

Straight out of: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timestamp-limitations

Workaround:

This will force the dates which are outside the bounds to NaT

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

Setting the errors parameter in pd.to_datetime to 'coerce' causes replacement of out of bounds values with NaT. Quoting the docs:

If ‘coerce’, then invalid parsing will be set as 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.

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

You can try with strptime() in datetime library along with lambda expression to convert text to date values in a series object:

Example:

df['F'].apply(lambda x: datetime.datetime.strptime(x, '%m/%d/%Y %I:%M:%S') if type(x)==str else np.NaN)

The reason you are seeing this error message "OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 3000-12-23 00:00:00" is because pandas timestamp data type stores date in nanosecond resolution(from the docs).

Which means the date values have to be in the range

pd.Timestamp.min(1677-09-21 00:12:43.145225) and


pd.Timestamp.max(2262-04-11 23:47:16.854775807)

Even if you only want the date with resolution of seconds or microseconds, pandas will still store it internally in nanoseconds. There is no option in pandas to store a timestamp outside of the above mentioned range.

This is surprising because databases like sql server and libraries like numpy allows to store date beyond this range. Also maximum of 64 bits are used in most of the cases to store the date.

But here is the difference. SQL server stores date in nanosecond resolution but only up to a accuracy of 100 ns(as opposed to 1 ns in pandas). Since the space is limited(64 bits), its a matter of range vs accuracy. With pandas timestamp we have higher accuracy but lower date range.

In case of numpy (pandas is built on top of numpy) datetime64 data type,

  • if the date falls in the above mentioned range you can store it in nanoseconds which is similar to pandas.
  • OR you can give up the nanosecond resolution and go with microseconds which will give you a much larger range. This is something that is missing in pandas timestamp type.

However if you choose to store in nanoseconds and the date is outside the range then numpy will automatically wrap around this date and you might get unexpected results (referenced below in the 4th solution).

np.datetime64("3000-06-19T08:17:14.073456178", dtype="datetime64[ns]")
> numpy.datetime64('1831-05-11T09:08:06.654352946')

Now with pandas we have below options,

import pandas as pd
data = {'Name': ['John', 'Sam'], 'dob': ['3000-06-19T08:17:14', '2000-06-19T21:17:14']}
my_df = pd.DataFrame(data)

1)If you are ok with losing the data which is out of range then simply use below param to convert out of range date to NaT(not a time).

my_df['dob'] = pd.to_datetime(my_df['dob'], errors = 'coerce')

enter image description here

2)If you dont want to lose the data then you can convert the values into a python datetime type. Here the column "dob" is of type pandas object but the individual value will be of type python datetime. However doing this we will lose the benefit of vectorized functions.

import datetime as dt
my_df['dob'] = my_df['dob'].apply(lambda x: dt.datetime.strptime(x,'%Y-%m-%dT%H:%M:%S') if type(x)==str else pd.NaT)
print(type(my_df.iloc[0][1]))
> <class 'datetime.datetime'>

enter image description here

3)Another option is to use numpy instead of pandas series if possible. In case of pandas dataframe, you can convert a series(or column in a df) to numpy array. Process the data separately and then join it back to the dataframe.

4)we can also use pandas timespans as suggested in the docs. Do checkout the difference b/w timestamp and period before using this data type. Date range and frequency here works similar to numpy(mentioned above in the numpy section).

my_df['dob'] = my_df['dob'].apply(lambda x: pd.Period(x, freq='ms'))

enter image description here