如何解决Python Pandas DataFrame的“ Outbounds nanosecond timestamp”错误? [英] How to work around Python Pandas DataFrame's "Out of bounds nanosecond timestamp" error?

查看:70
本文介绍了如何解决Python Pandas DataFrame的“ Outbounds nanosecond timestamp”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码引发 超出范围的纳秒级时间戳:1452-04-15 00:00:00
错误。如果我将日期字符串替换为一些最近的日期(例如2017-01-01),则可以使用相同的代码。

The following code throws an "Out of bounds nanosecond timestamp: 1452-04-15 00:00:00 " error. The same code works if I replace the date strings to some recent dates such as 2017-01-01.

df=pd.DataFrame({'Date':np.arange('1452-04-15', '1519-05-02', dtype='datetime64[D]')})

此示例代码适用于提供一种重现该错误的简便方法。我真正想做的是将包含此类早期日期的csv读入数据框,并将字符串日期转换为np.datetime64 [D]或任何可比较的日期格式。

This example code is for providing an easy way to reproduce the error. What I am really trying to get done is to read a csv containing very early dates like these into a dataframe, and to convert the string dates into np.datetime64[D] or any comparable date format.

推荐答案

您需要 周期范围

r = pd.period_range('1452-04-15', '1519-05-02')
print (r)
PeriodIndex(['1452-04-15', '1452-04-16', '1452-04-17', '1452-04-18',
             '1452-04-19', '1452-04-20', '1452-04-21', '1452-04-22',
             '1452-04-23', '1452-04-24',
             ...
             '1519-04-23', '1519-04-24', '1519-04-25', '1519-04-26',
             '1519-04-27', '1519-04-28', '1519-04-29', '1519-04-30',
             '1519-05-01', '1519-05-02'],
            dtype='period[D]', length=24488, freq='D')

df = pd.DataFrame({'Date' : r})
print (df.head())
        Date
0 1452-04-15
1 1452-04-16
2 1452-04-17
3 1452-04-18
4 1452-04-19

因为时间戳限制

In [66]: pd.Timestamp.min
Out[66]: Timestamp('1677-09-21 00:12:43.145225')

In [67]: pd.Timestamp.max
Out[67]: Timestamp('2262-04-11 23:47:16.854775807')

这篇关于如何解决Python Pandas DataFrame的“ Outbounds nanosecond timestamp”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆