Skip to content

Commit c27f430

Browse files
winklerandjreback
authored andcommitted
TST/CLN: raise error when resampling with on= or level= selection
Added tests to cover all code paths, moved the check to _convert_obj() and removed then-redundant check from _upsample() method.
1 parent ca7b6f2 commit c27f430

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

pandas/core/resample.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,13 @@ def _get_binner_for_time(self):
842842
def _convert_obj(self, obj):
843843
obj = super(PeriodIndexResampler, self)._convert_obj(obj)
844844

845+
if self._from_selection:
846+
# see GH 14008, GH 12871
847+
msg = ("Resampling from level= or on= selection"
848+
" with a PeriodIndex is not currently supported,"
849+
" use .set_index(...) to explicitly set index")
850+
raise NotImplementedError(msg)
851+
845852
offset = to_offset(self.freq)
846853
if offset.n > 1:
847854
if self.kind == 'period': # pragma: no cover
@@ -852,14 +859,7 @@ def _convert_obj(self, obj):
852859

853860
# convert to timestamp
854861
if not (self.kind is None or self.kind == 'period'):
855-
if self._from_selection:
856-
# see GH 14008, GH 12871
857-
msg = ("Resampling from level= or on= selection"
858-
" with a PeriodIndex is not currently supported,"
859-
" use .set_index(...) to explicitly set index")
860-
raise NotImplementedError(msg)
861-
else:
862-
obj = obj.to_timestamp(how=self.convention)
862+
obj = obj.to_timestamp(how=self.convention)
863863

864864
return obj
865865

@@ -906,11 +906,7 @@ def _upsample(self, method, limit=None, fill_value=None):
906906
.fillna
907907
908908
"""
909-
if self._from_selection:
910-
raise ValueError("Upsampling from level= or on= selection"
911-
" is not supported, use .set_index(...)"
912-
" to explicitly set index to"
913-
" datetime-like")
909+
914910
# we may need to actually resample as if we are timestamps
915911
if self.kind == 'timestamp':
916912
return super(PeriodIndexResampler, self)._upsample(

pandas/tests/test_resample.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,12 +2294,17 @@ def test_selection(self):
22942294
index=pd.MultiIndex.from_arrays([
22952295
np.arange(len(index), dtype=np.int64),
22962296
index], names=['v', 'd']))
2297-
2298-
with pytest.raises(NotImplementedError):
2299-
df.resample('2D', on='date')
2300-
2301-
with pytest.raises(NotImplementedError):
2302-
df.resample('2D', level='d')
2297+
for freq in ['H', '12H', '2D', 'W']:
2298+
# check up- and downsampling with base freqs and freq multiples
2299+
with pytest.raises(NotImplementedError):
2300+
df.resample(freq, on='date')
2301+
with pytest.raises(NotImplementedError):
2302+
df.resample(freq, level='d')
2303+
for kind_param in ['timestamp', 'period']:
2304+
with pytest.raises(NotImplementedError):
2305+
df.resample(freq, on='date', kind=kind_param)
2306+
with pytest.raises(NotImplementedError):
2307+
df.resample(freq, level='d', kind=kind_param)
23032308

23042309
def test_annual_upsample_D_s_f(self):
23052310
self._check_annual_upsample_cases('D', 'start', 'ffill')

0 commit comments

Comments
 (0)