我正在尝试检查 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 加法溢出更多背景信息: