访问pandas时间增量序列的.days Fǎngwèn pandas shíjiān zēngliàng xùliè de .days

2024-09-28 20:40:02 发布

您现在位置:Python中文网/ 问答频道 /正文

熊猫TimedeltaIndex有一个属性days,可用于其他普通数据类型(float64等)的操作:

import pandas as pd
from pandas.tseries import offsets
idx1 = pd.date_range('2017-01', periods=10)
idx2 = idx1 + offsets.MonthEnd(1)
tds = idx2 - idx1

print(tds.days - 2)
Int64Index([28, 27, 26, 25, 24, 23, 22, 21, 20, 19], dtype='int64')

但是当tds被转换成一个序列(显式地,或者作为一个数据帧列),它就失去了这个属性。在

^{pr2}$

访问.days需要转换回Index

print(pd.Index(pd.Series(tds)).days)
Int64Index([30, 29, 28, 27, 26, 25, 24, 23, 22, 21], dtype='int64')

是否有比上述转换更直接地访问此属性的方法?


Tags: importpandasindex属性dayspdprintdtype