how to perform operation apply() on DatetimeIndexResampler type

Viewed 19

I want to use apply() to perform operation on my df after resampling but I get an error : But when I use resampling, my data is a Serie and not a Dataframe anymore, then I can't use apply() to do what I want, but I also need to use resample.

The error occuring is :

'Series' object has no attribute 'columns'

    df = pd.DataFrame(data)
    x =df[df.columns[0]].fillna(0)
    x_r = x.resample("s")
    x= x_r.apply(['mean',np.max,np.min])

How can I process this ?

here is the error :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_296/635968.py in <module>
      2 print(x_mm)
      3 
----> 4 x_mm.apply(['mean',np.max,np.min])

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/resample.py in aggregate(self, func, *args, **kwargs)
    333     def aggregate(self, func, *args, **kwargs):
    334 
--> 335         result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
    336         if result is None:
    337             how = func

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/apply.py in agg(self)
    162         elif is_list_like(arg):
    163             # we require a list, but not a 'str'
--> 164             return self.agg_list_like()
    165 
    166         if callable(arg):

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/apply.py in agg_list_like(self)
    334         if selected_obj.ndim == 1:
    335             for a in arg:
--> 336                 colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj)
    337                 try:
    338                     new_res = colg.aggregate(a)

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/resample.py in _gotitem(self, key, ndim, subset)
    390         # try the key selection
    391         try:
--> 392             return grouped[key]
    393         except KeyError:
    394             return grouped

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/base.py in __getitem__(self, key)
    218 
    219         if isinstance(key, (list, tuple, ABCSeries, ABCIndex, np.ndarray)):
--> 220             if len(self.obj.columns.intersection(key)) != len(key):
    221                 bad_keys = list(set(key).difference(self.obj.columns))
    222                 raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")

/local/opt/anaconda/anaconda3/envs/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5485         ):
   5486             return self[name]
-> 5487         return object.__getattribute__(self, name)
   5488 
   5489     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'columns'
0 Answers
Related