Skip to content

FIX: Raise errors when wrong string arguments are passed to resample #19307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ Groupby/Resample/Rolling

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

Sparse
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,16 @@ class TimeGrouper(Grouper):
def __init__(self, freq='Min', closed=None, label=None, how='mean',
axis=0, fill_method=None, limit=None, loffset=None,
kind=None, convention=None, base=0, **kwargs):
# Check for correctness of the keyword arguments which would
# otherwise silently use the default if misspelled
if label not in {None, 'left', 'right'}:
raise ValueError('Unsupported value %s for `label`' % label)
if closed not in {None, 'left', 'right'}:
raise ValueError('Unsupported value %s for `closed`' % closed)
if convention not in {None, 'start', 'end', 'e', 's'}:
raise ValueError('Unsupported value %s for `convention`'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use '%', use .format() instead.

% convention)

freq = to_offset(freq)

end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ def test_resample_basic(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
s = Series(np.random.randn(14), index=rng)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add as a separate test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. No problem.

result = s.resample('5min', closed='right', label='right').mean()

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

def test_resample_string_kwargs(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number here as a comment

name='index')
s = Series(np.random.randn(14), index=rng)

# Check that wrong keyword argument strings raise an error
with pytest.raises(ValueError) as e_info:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback There is also a linting error here because of the unused e_info. I will also remove these, okay?

s.resample('5min', label='righttt').mean()
with pytest.raises(ValueError) as e_info:
s.resample('5min', closed='righttt').mean()
with pytest.raises(ValueError) as e_info:
s.resample('5min', convention='starttt').mean()

def test_resample_how(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
Expand Down