Skip to content

Commit 55db35e

Browse files
cchwalajreback
authored andcommitted
FIX: Raise errors when wrong string arguments are passed to resample (#19307)
1 parent b286789 commit 55db35e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ Groupby/Resample/Rolling
486486

487487
- Bug when grouping by a single column and aggregating with a class like ``list`` or ``tuple`` (:issue:`18079`)
488488
- Fixed regression in :func:`DataFrame.groupby` which would not emit an error when called with a tuple key not in the index (:issue:`18798`)
489-
-
489+
- Bug in :func:`DataFrame.resample` which silently ignored unsupported (or mistyped) options for ``label``, ``closed`` and ``convention`` (:issue:`19303`)
490490
-
491491

492492
Sparse

pandas/core/resample.py

+11
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,17 @@ class TimeGrouper(Grouper):
10611061
def __init__(self, freq='Min', closed=None, label=None, how='mean',
10621062
axis=0, fill_method=None, limit=None, loffset=None,
10631063
kind=None, convention=None, base=0, **kwargs):
1064+
# Check for correctness of the keyword arguments which would
1065+
# otherwise silently use the default if misspelled
1066+
if label not in {None, 'left', 'right'}:
1067+
raise ValueError('Unsupported value {} for `label`'.format(label))
1068+
if closed not in {None, 'left', 'right'}:
1069+
raise ValueError('Unsupported value {} for `closed`'.format(
1070+
closed))
1071+
if convention not in {None, 'start', 'end', 'e', 's'}:
1072+
raise ValueError('Unsupported value {} for `convention`'
1073+
.format(convention))
1074+
10641075
freq = to_offset(freq)
10651076

10661077
end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])

pandas/tests/test_resample.py

+15
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ def test_resample_basic(self):
963963
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
964964
name='index')
965965
s = Series(np.random.randn(14), index=rng)
966+
966967
result = s.resample('5min', closed='right', label='right').mean()
967968

968969
exp_idx = date_range('1/1/2000', periods=4, freq='5min', name='index')
@@ -985,6 +986,20 @@ def test_resample_basic(self):
985986
expect = s.groupby(grouper).agg(lambda x: x[-1])
986987
assert_series_equal(result, expect)
987988

989+
def test_resample_string_kwargs(self):
990+
# Test for issue #19303
991+
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
992+
name='index')
993+
s = Series(np.random.randn(14), index=rng)
994+
995+
# Check that wrong keyword argument strings raise an error
996+
with pytest.raises(ValueError):
997+
s.resample('5min', label='righttt').mean()
998+
with pytest.raises(ValueError):
999+
s.resample('5min', closed='righttt').mean()
1000+
with pytest.raises(ValueError):
1001+
s.resample('5min', convention='starttt').mean()
1002+
9881003
def test_resample_how(self):
9891004
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
9901005
name='index')

0 commit comments

Comments
 (0)