什么决定了 Pandas 的最小和最大时间戳?

wil*_*llk 2 python datetime pandas

Pandas 中的最小值Timestamp是:

pd.Timestamp.min
Timestamp('1677-09-21 00:12:43.145225')
Run Code Online (Sandbox Code Playgroud)

最大值为:

pd.Timestamp.max
Timestamp('2262-04-11 23:47:16.854775807')
Run Code Online (Sandbox Code Playgroud)

这意味着您无法将超出此范围的值转换为 Pandas 日期时间:

pd.Timestamp(datetime.date(2500, 1, 1))
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 2500-01-01 00:00:00
Run Code Online (Sandbox Code Playgroud)

是什么决定了这些限制?

pit*_*tix 5

日期时间类型使用带符号的 64 位整数以纳秒为单位存储。那么范围是 [2^-63;2^63 -1 ]。使用 0 作为 UNIX 纪元 (1970/01/01 00:00:00.0),通过运行此代码,您可以看到结果距离 0(unix 纪元)大约为 292 年。那么,最大值是用前导0后跟 63表示的日期1

运行此代码来亲自证明这一点。

max_int=2**63-1 # maximum integer
max_int/=10**9 # from nanoseconds to seconds
max_int/=86400 # from seconds to days
max_int/=365 # from days to years (suppose no leap years)
print(1970+max_int) # print the maximum year, with an error of days
Run Code Online (Sandbox Code Playgroud)

编辑:正如本在下面的评论中所写,我没有报告来源。这里