[英]Subtract two Pandas DataFrame time indexes from each other?
I have two DataFrames (say A
and B
), each with an index that is a pandas.tseries.index.DateTimeIndex
class. 我有两个DataFrame(例如A
和B
),每个都有一个pandas.tseries.index.DateTimeIndex
类的索引。
How do I find the number of days between each row of the DataFrames? 如何找到DataFrames每行之间的天数?
So that A.index - B.index
would give me something like: 这样A.index - B.index
会给我类似的东西:
34
25
34
and so on. 等等。
Assuming both indices contain the same number of date observations, you can zip them and calculate the differences using a list comprehension. 假设两个索引包含相同数量的日期观测值,则可以压缩它们并使用列表推导来计算差异。
df1 = pd.DataFrame(np.random.randn(5, 2), index=pd.date_range('2015-1-1', periods=5, freq='M'))
df2 = pd.DataFrame(np.random.randn(5, 2), index=pd.date_range('2015-6-1', periods=5, freq='M'))
>>> [(d2.date() - d1.date()).days for d1, d2 in zip(df1.index, df2.index)]
Out[46]: [150, 153, 153, 153, 153]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.