Skip to content

Commit 44799ce

Browse files
committed
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 0a6a3f6 commit 44799ce

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

pandas/core/resample.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,13 @@ def _get_binner_for_time(self):
801801
def _convert_obj(self, obj):
802802
obj = super(PeriodIndexResampler, self)._convert_obj(obj)
803803

804+
if self._from_selection:
805+
# see GH 14008, GH 12871
806+
msg = ("Resampling from level= or on= selection"
807+
" with a PeriodIndex is not currently supported,"
808+
" use .set_index(...) to explicitly set index")
809+
raise NotImplementedError(msg)
810+
804811
offset = to_offset(self.freq)
805812
if offset.n > 1:
806813
if self.kind == 'period': # pragma: no cover
@@ -811,14 +818,7 @@ def _convert_obj(self, obj):
811818

812819
# convert to timestamp
813820
if not (self.kind is None or self.kind == 'period'):
814-
if self._from_selection:
815-
# see GH 14008, GH 12871
816-
msg = ("Resampling from level= or on= selection"
817-
" with a PeriodIndex is not currently supported,"
818-
" use .set_index(...) to explicitly set index")
819-
raise NotImplementedError(msg)
820-
else:
821-
obj = obj.to_timestamp(how=self.convention)
821+
obj = obj.to_timestamp(how=self.convention)
822822

823823
return obj
824824

@@ -865,11 +865,7 @@ def _upsample(self, method, limit=None, fill_value=None):
865865
.fillna
866866
867867
"""
868-
if self._from_selection:
869-
raise ValueError("Upsampling from level= or on= selection"
870-
" is not supported, use .set_index(...)"
871-
" to explicitly set index to"
872-
" datetime-like")
868+
873869
# we may need to actually resample as if we are timestamps
874870
if self.kind == 'timestamp':
875871
return super(PeriodIndexResampler, self)._upsample(

pandas/tests/test_resample.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -2252,12 +2252,17 @@ def test_selection(self):
22522252
index=pd.MultiIndex.from_arrays([
22532253
np.arange(len(index), dtype=np.int64),
22542254
index], names=['v', 'd']))
2255-
2256-
with pytest.raises(NotImplementedError):
2257-
df.resample('2D', on='date')
2258-
2259-
with pytest.raises(NotImplementedError):
2260-
df.resample('2D', level='d')
2255+
for freq in ['H', '12H', '2D', 'W']:
2256+
# check up- and downsampling with base freqs and freq multiples
2257+
with pytest.raises(NotImplementedError):
2258+
df.resample(freq, on='date')
2259+
with pytest.raises(NotImplementedError):
2260+
df.resample(freq, level='d')
2261+
for kind_param in ['timestamp', 'period']:
2262+
with pytest.raises(NotImplementedError):
2263+
df.resample(freq, on='date', kind=kind_param)
2264+
with pytest.raises(NotImplementedError):
2265+
df.resample(freq, level='d', kind=kind_param)
22612266

22622267
def test_annual_upsample_D_s_f(self):
22632268
self._check_annual_upsample_cases('D', 'start', 'ffill')

0 commit comments

Comments
 (0)