У меня есть фрейм данных с двумя столбцами: 'case' и 'datetime'.
index------ case----------outDateTime
71809----10180227.0-----2013-01-01 01:41:01
71810----10180229.0-----2013-01-01 04:20:05
71811----10180230.0-----2013-01-01 06:20:22
575------10180232.0-----2013-01-01 02:01:13
23757----10180233.0-----2013-01-01 01:48:49
Моя цель - подсчитать количество случаев, которые есть в определенный час каждого дня. Для этих данных это будет:
2013-01-01 01AM = 2
2013-01-01 02AM = 1
2013-01-01 03AM = 0
2013-01-01 04AM = 1
и т. Д.
Я хотел использовать df.resample('H').agg()
, но я получаю следующую ошибку:
TypeError: Действителен только с DatetimeIndex, TimedeltaIndex или PeriodIndex, но получил экземпляр 'Index'
Я нахожу это странным, потому что я использовал to_datetime
и set_index
, поэтому мой индекс данных - это дата и время, а тип - время-дата.
Мой код:
pd.to_datetime(Dout['outDateTime'],dayfirst=True)
Dout.set_index('outDateTime',inplace=True)
Dout.isnull().values.any()
false
Dout = Dout.resample('H').agg()
-------------------------------------------------------------------
--------
TypeError Traceback (most recent
call last)
<ipython-input-196-75f9fbadd6cc> in <module>
----> 1 Dout = Dout.resample('H').agg()
~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in
resample(self, rule, how, axis, fill_method, closed, label,
convention, kind, loffset, limit, base, on, level)
7108 axis=axis, kind=kind, loffset=loffset,
7109 convention=convention,
-> 7110 base=base, key=on, level=level)
7111 return _maybe_process_deprecations(r,
7112 how=how,
~/anaconda3/lib/python3.7/site-packages/pandas/core/resample.py in
resample(obj, kind, **kwds)
1146 """ create a TimeGrouper and return our resampler """
1147 tg = TimeGrouper(**kwds)
-> 1148 return tg._get_resampler(obj, kind=kind)
1149
1150
~/anaconda3/lib/python3.7/site-packages/pandas/core/resample.py in
_get_resampler(self, obj, kind)
1274 raise TypeError("Only valid with DatetimeIndex, "
1275 "TimedeltaIndex or PeriodIndex, "
-> 1276 "but got an instance of %r" %
type(ax).__name__)
1277
1278 def _get_grouper(self, obj, validate=True):
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or
PeriodIndex, but got an instance of 'Index'