3
A TimedeltaIndex
Pandas hat ein Attribut days
, die für den Betrieb mit anderen normalen dtypes verwendet werden können (float64
, usw.):Zugriff `.days` für eine Pandas Serie von timedeltas
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')
Aber wenn tds
wird umgewandelt Eine Reihe (explizit oder als eine DataFrame-Spalte) verliert dieses Attribut.
print(pd.Series(tds).days)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-115-cb20b4d368f4> in <module>()
----> 1 print(pd.Series(tds).days)
C:\Users\bsolomon\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
3079 if name in self._info_axis:
3080 return self[name]
-> 3081 return object.__getattribute__(self, name)
3082
3083 def __setattr__(self, name, value):
AttributeError: 'Series' object has no attribute 'days'
Und .days
Zugriff erfordert zurück zu einem Index
Umwandlung:
print(pd.Index(pd.Series(tds)).days)
Int64Index([30, 29, 28, 27, 26, 25, 24, 23, 22, 21], dtype='int64')
Gibt es einen einfacheren Weg, um dieses Attribut für den Zugriff auf als bei der Umwandlung oben?