Skip to content

BUG: PeriodIndexGrouper fails with Grouper selection #14008

Open
@chris-b1

Description

@chris-b1

xref #13961

In [3]: df = pd.DataFrame({'date': pd.period_range('2015-01-01', freq='D', periods=5),
   ...:                    'a': np.arange(5)},
   ...:                    index=pd.MultiIndex.from_arrays([
   ...:                             [1,2,3,4,5],
   ...:                             pd.period_range('2015-01-01', freq='D', periods=5)],
   ...:                         names=['v','d']))

In [4]: df
Out[4]: 
              a       date
v d                       
1 2015-01-01  0 2015-01-01
2 2015-01-02  1 2015-01-02
3 2015-01-03  2 2015-01-03
4 2015-01-04  3 2015-01-04
5 2015-01-05  4 2015-01-05

In [5]: df.groupby(pd.Grouper(key='date', freq='2D')).sum()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-7d905f24cc6a> in <module>()
----> 1 df.groupby(pd.Grouper(key='date', freq='2D')).sum()

C:\Users\Chris\Documents\python-dev\pandas\pandas\core\generic.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, **kwargs)
   3962         return groupby(self, by=by, axis=axis, level=level, as_index=as_index,
   3963                        sort=sort, group_keys=group_keys, squeeze=squeeze,
-> 3964                        **kwargs)
   3965 
   3966     def asfreq(self, freq, method=None, how=None, normalize=False):

C:\Users\Chris\Documents\python-dev\pandas\pandas\core\groupby.py in groupby(obj, by, **kwds)
   1495         raise TypeError('invalid type: %s' % type(obj))
   1496 
-> 1497     return klass(obj, by, **kwds)
   1498 
   1499 

C:\Users\Chris\Documents\python-dev\pandas\pandas\core\groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, **kwargs)
    365                                                     level=level,
    366                                                     sort=sort,
--> 367                                                     mutated=self.mutated)
    368 
    369         self.obj = obj

C:\Users\Chris\Documents\python-dev\pandas\pandas\core\groupby.py in _get_grouper(obj, key, axis, level, sort, mutated)
   2381     # a passed-in Grouper, directly convert
   2382     if isinstance(key, Grouper):
-> 2383         binner, grouper, obj = key._get_grouper(obj)
   2384         if key.key is None:
   2385             return grouper, [], obj

C:\Users\Chris\Documents\python-dev\pandas\pandas\tseries\resample.py in _get_grouper(self, obj)
   1027     def _get_grouper(self, obj):
   1028         # create the resampler and return our binner
-> 1029         r = self._get_resampler(obj)
   1030         r._set_binner()
   1031         return r.binner, r.grouper, r.obj

C:\Users\Chris\Documents\python-dev\pandas\pandas\tseries\resample.py in _get_resampler(self, obj, kind)
   1015                                         groupby=self,
   1016                                         kind=kind,
-> 1017                                         axis=self.axis)
   1018         elif isinstance(ax, TimedeltaIndex):
   1019             return TimedeltaIndexResampler(obj,

C:\Users\Chris\Documents\python-dev\pandas\pandas\tseries\resample.py in __init__(self, obj, groupby, axis, kind, **kwargs)
     88 
     89         if self.groupby is not None:
---> 90             self.groupby._set_grouper(self._convert_obj(obj), sort=True)
     91 
     92     def __unicode__(self):

C:\Users\Chris\Documents\python-dev\pandas\pandas\tseries\resample.py in _convert_obj(self, obj)
    764         # convert to timestamp
    765         if not (self.kind is None or self.kind == 'period'):
--> 766             obj = obj.to_timestamp(how=self.convention)
    767         return obj
    768 

C:\Users\Chris\Documents\python-dev\pandas\pandas\core\frame.py in to_timestamp(self, freq, how, axis, copy)
   5174         axis = self._get_axis_number(axis)
   5175         if axis == 0:
-> 5176             new_data.set_axis(1, self.index.to_timestamp(freq=freq, how=how))
   5177         elif axis == 1:
   5178             new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how))

AttributeError: 'MultiIndex' object has no attribute 'to_timestamp'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions