0

I have a dataframe with two columns: 'case' and '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

My goal is to count the amount of cases that there are in a certain hour of every day. For this data it would be:

2013-01-01 01AM = 2
2013-01-01 02AM = 1
2013-01-01 03AM = 0
2013-01-01 04AM = 1

and so on

I wanted to use df.resample('H').agg(), but I get the following error :

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

I find it weird because I used to_datetime and set_index so my dataframes index is the datetime and the type is datetime .

My code:

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'
barbsan
  • 3,418
  • 11
  • 21
  • 28
enter_thevoid
  • 113
  • 1
  • 11

1 Answers1

0

I think you are looking for:

df.set_index('outDateTime').resample('H').size()
Loochie
  • 2,414
  • 13
  • 20
  • so you are saying to change agg() to size() , but it didn't resolve the problem i had . as i said , i used set_index and it didin't work . the error i get is the same – enter_thevoid Apr 04 '19 at 14:43