Skip to content

Commit 5fd97d9

Browse files
committed
add from_selection bookkeeping
1 parent 7f9add4 commit 5fd97d9

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

pandas/tseries/resample.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Resampler(_GroupBy):
6464
'binner', 'grouper', 'groupby',
6565
'sort', 'kind', 'squeeze', 'keys',
6666
'group_keys', 'as_index', 'exclusions',
67-
'_groupby']
67+
'_groupby', 'from_selection']
6868

6969
# don't raise deprecation warning on attributes starting with these
7070
# patterns - prevents warnings caused by IPython introspection
@@ -85,8 +85,12 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs):
8585
self.exclusions = set()
8686
self.binner = None
8787
self.grouper = None
88+
self.from_selection = False
8889

8990
if self.groupby is not None:
91+
# bookeeping to disallow upsampling if not resampling on index
92+
self.from_selection = (self.groupby.key is not None or
93+
self.groupby.level is not None)
9094
obj, converter = self._convert_obj(obj)
9195
self.groupby._set_grouper(obj, sort=True, converter=converter)
9296

@@ -711,10 +715,14 @@ def _upsample(self, method, limit=None):
711715
.fillna
712716
713717
"""
714-
# import pdb; pdb.set_trace()
715718
self._set_binner()
716719
if self.axis:
717720
raise AssertionError('axis must be 0')
721+
if self.from_selection:
722+
raise NotImplementedError("Upsampling from level= or on= selection "
723+
"is not supported, use .set_index(...) "
724+
"to explicitly set index to "
725+
"datetime-like")
718726

719727
ax = self.ax
720728
obj = self._selected_obj
@@ -773,7 +781,13 @@ def _convert_obj(self, obj):
773781
converter = None
774782
# convert to timestamp
775783
if not (self.kind is None or self.kind == 'period'):
776-
converter = lambda x: x.to_timestamp(how=self.convention)
784+
# if periondindex is the actual index obj, just convert it
785+
# otherwise, converter callback will be used on selection
786+
if self.from_selection:
787+
converter = lambda x: x.to_timestamp(how=self.convention)
788+
else:
789+
obj = obj.to_timestamp(how=self.convention)
790+
777791
return obj, converter
778792

779793
def aggregate(self, arg, *args, **kwargs):
@@ -850,6 +864,12 @@ def _upsample(self, method, limit=None):
850864
.fillna
851865
852866
"""
867+
# import pdb; pdb.set_trace()
868+
if self.from_selection:
869+
raise NotImplementedError("Upsampling from level= or on= selection "
870+
"is not supported, use .set_index(...) "
871+
"to explicitly set index to "
872+
"datetime-like")
853873
# we may need to actually resample as if we are timestamps
854874
if self.kind == 'timestamp':
855875
return super(PeriodIndexResampler, self)._upsample(method,

pandas/tseries/tests/test_resample.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -741,16 +741,22 @@ def test_selection_api_validation(self):
741741

742742
# non DatetimeIndex
743743
with tm.assertRaises(TypeError):
744-
df.resample('M', level='v')
744+
df.resample('2D', level='v')
745745

746746
with tm.assertRaises(ValueError):
747-
df.resample('M', on='date', level='d')
747+
df.resample('2D', on='date', level='d')
748748

749749
with tm.assertRaises(TypeError):
750-
df.resample('M', on=['a', 'date'])
750+
df.resample('2D', on=['a', 'date'])
751751

752752
with tm.assertRaises(KeyError):
753-
df.resample('M', level=['a', 'date'])
753+
df.resample('2D', level=['a', 'date'])
754+
755+
with tm.assertRaises(NotImplementedError):
756+
df.resample('2D', level='d').asfreq()
757+
758+
with tm.assertRaises(NotImplementedError):
759+
df.resample('2D', on='date').asfreq()
754760

755761
exp = df_exp.resample('2D').sum()
756762
exp.index.name = 'date'

0 commit comments

Comments
 (0)