熊猫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
被转换成一个序列(显式地,或者作为一个数据帧列),它就失去了这个属性。在
访问.days
需要转换回Index
:
print(pd.Index(pd.Series(tds)).days)
Int64Index([30, 29, 28, 27, 26, 25, 24, 23, 22, 21], dtype='int64')
是否有比上述转换更直接地访问此属性的方法?
使用^{} 访问器:
输出:
^{2}$相关问题 更多 >
编程相关推荐