gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:59:26 27 4
gpt4 key购买 nike

我尝试获取各种 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com