Skip to content

BUG: .reset_index() should raise with an invalid level name (GH20925) #21016

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 17 commits into from
May 18, 2018
7 changes: 4 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,13 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
inplace = validate_bool_kwarg(inplace, 'inplace')
if drop:
new_index = com._default_index(len(self))
if level is not None and isinstance(self.index, MultiIndex):
if level is not None:
if not isinstance(level, (tuple, list)):
level = [level]
level = [self.index._get_level_number(lev) for lev in level]
if len(level) < len(self.index.levels):
new_index = self.index.droplevel(level)
if isinstance(self.index, MultiIndex):
if len(level) < self.index.nlevels:
new_index = self.index.droplevel(level)

if inplace:
self.index = new_index
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,24 @@ def test_head_tail(test_data):
assert_series_equal(test_data.series.head(0), test_data.series[0:0])
assert_series_equal(test_data.series.tail(), test_data.series[-5:])
assert_series_equal(test_data.series.tail(0), test_data.series[0:0])


def test_reset_index_drop_errmsg():
Copy link
Member

Choose a reason for hiding this comment

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

I just noticed this is in the wrong file... reset_index is tested in pandas/tests/series/test_alter_axes.py, not test_indexing.py.

While you're at it, please replace # https://github.com/pandas-dev/pandas/issues/20925 with the standard # GH 20925.

Copy link
Contributor Author

@KalyanGokhale KalyanGokhale May 17, 2018

Choose a reason for hiding this comment

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

Thanks didn't know that the right file was test_alter_axes.py

Have moved the function there (now renamed as test_reset_index_drop_errors based on @TomAugspurger 's comment) - also updated comment for # GH 20925

Copy link
Contributor

Choose a reason for hiding this comment

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

errmsg -> errors or error_message

Copy link
Contributor Author

@KalyanGokhale KalyanGokhale May 17, 2018

Choose a reason for hiding this comment

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

done. renamed def as test_reset_index_drop_errors

# https://github.com/pandas-dev/pandas/issues/20925

# Check KeyError raised for series index where passed level name is missing
s = pd.Series(range(4))
with tm.assert_raises_regex(KeyError, 'must be same as name'):
s.reset_index('wrong', drop=True)
with tm.assert_raises_regex(KeyError, 'must be same as name'):
s.reset_index('wrong')

# Check KeyError raised for series where 'level' to be dropped is missing
s = pd.Series(range(4), index=pd.MultiIndex.from_product([[1, 2]] * 2))
with tm.assert_raises_regex(KeyError, 'not found'):
s.reset_index('wrong', drop=True)

# Check that .reset_index([],drop=True) doesn't fail
Copy link
Member

Choose a reason for hiding this comment

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

This is fine but probably not in the right test: could you append it to test_reset_index_level (in pandas/tests/series/test_alter_axes.py)?

Copy link
Contributor Author

@KalyanGokhale KalyanGokhale May 17, 2018

Choose a reason for hiding this comment

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

Done. Have moved it to function test_reset_index_level outside the for loop

result = pd.Series(range(4)).reset_index([], drop=True)
expected = pd.Series(range(4))
assert_series_equal(result, expected)