在熊猫中减去日期时间列时出现溢出错误

问题描述 投票:0回答:0

我正在尝试检查 Pandas 中两个时间戳列之间的差异是否大于

n
秒。我其实并不关心其中的区别。我只想知道它是否大于
n
秒,我也可以将
n
限制在 1 到 60 之间的范围内。

听起来很简单,对吧?

这个问题有很多有价值的答案概述了如何做到这一点。

问题: 由于我无法控制的原因,两个时间戳之间的差异可能相当大,这就是我遇到整数溢出的原因。

这是一个

MCVE

import pandas as pd import pandas.testing dataframe = pd.DataFrame( { "historic": [pd.Timestamp("1900-01-01T00:00:00+00:00")], "futuristic": [pd.Timestamp("2200-01-01T00:00:00+00:00")], } ) # Goal: Figure out if the difference between # futuristic and historic is > n seconds, i.e.: # futuristic - historic > n number_of_seconds = 1 dataframe["diff_greater_n"] = ( dataframe["futuristic"] - dataframe["historic"] ) / pd.Timedelta(seconds=1) > number_of_seconds expected_dataframe = pd.DataFrame( { "historic": [pd.Timestamp("1900-01-01T00:00:00+00:00")], "futuristic": [pd.Timestamp("2200-01-01T00:00:00+00:00")], "diff_greater_n": [True], } ) pandas.testing.assert_frame_equal(dataframe, expected_dataframe)

错误

OverflowError:int64 加法溢出

更多背景信息:

    时间戳需要有秒精度,即我不关心任何毫秒
  • 这是对数据框的多项或组合检查之一
  • 数据框可能有几百万行
  • 我很高兴终于可以询问关于 stackoverflow 的溢出错误
python pandas dataframe integer-overflow
© www.soinside.com 2019 - 2024. All rights reserved.