python - 相隔 >292 年的日期差值

标签 python datetime numpy pandas

我尝试获取各种 pandas 日期的日期增量。但是,对于 >292 年的时间增量,我得到负值。例如,

import pandas as pd
dates = pd.Series(pd.date_range('1700-01-01', periods=4500, freq='m'))
days_delta = (dates-dates.min()).astype('timedelta64[D]')

但是,使用 DatetimeIndex 我可以做到并且它按我想要的方式工作,

import pandas as pd
import numpy as np
dates = pd.date_range('1700-01-01', periods=4500, freq='m')
days_fun = np.vectorize(lambda x: x.days)
days_delta = days_fun(dates.date - dates.date.min())

接下来的问题是如何为 Series 对象获取正确的 days_delta?

最佳答案

阅读here特别是关于 timedelta 限制:

Pandas represents Timedeltas in nanosecond resolution using 64 bit integers. As such, the 64 bit integer limits determine the Timedelta limits.

顺便说一句,这与文档中提到的 Pandas 时间戳中的限制相同:

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

这表明文档为规避时间戳限制而提出的相同建议可以应用于时间增量。时间戳限制的解决方案可在文档 ( here ) 中找到:

If you have data that is outside of the Timestamp bounds, see Timestamp limitations, then you can use a PeriodIndex and/or Series of Periods to do computations.

关于python - 相隔 >292 年的日期差值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35814172/

相关文章:

c# - 为什么不同机器上的相同文化给出不同的缩写月份?

java - 计算两个时间的差异

python - 从三个 1D numpy 数组填充 2D numpy 数组

python - 稀疏 SciPy 矩阵与两个 NumPy 向量的矩阵乘法

python - 寻找最小化约束的最佳解决方案?

python - 找到两组相同列表的所有唯一组合?

Python 在函数中使用 Lambda

Python OpenCV - ConvexHull 错误 "Points is not a numpy array, neither a scalar"?

ruby-on-rails - Rails 和 Sqlite 问题 - 将日期与数据库时间戳进行比较

python - 格式化 numpy 数组并保存到 *.txt