Skip to content

BUGfix: reset_index can reset index name (#17067) #17243

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Indexing
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
- Fixes in ``Index.__add__`` disappearing index name after plus operation (:issue:`17067`)

I/O
^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def is_bool_indexer(key):
return False


def _default_index(n):
def _default_index(n, name=None):
from pandas.core.index import RangeIndex
return RangeIndex(0, n, name=None)
return RangeIndex(0, n, name=name)


def _mut_exclusive(**kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ def argsort(self, *args, **kwargs):
return result.argsort(*args, **kwargs)

def __add__(self, other):
return Index(np.array(self) + other)
return Index(np.array(self) + other, name=self.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

actually this is not correct. We don't assign the index name on a boolean operation. only on an inplace operation (e.g. __iadd__). This also would apply to other ops like isub etc.

Copy link
Author

Choose a reason for hiding this comment

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

@jreback Thank you for your advice on my first pandas PR. :) Should I raise exception in case of

s = pd.Series([1, 2, 3])
s.index.name = 'foo'
s.index += 1
assert s.index.name == 'foo'

Copy link
Contributor

Choose a reason for hiding this comment

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

no, this is slightly complicated. the __iadd__ operator needs to be modified (and not __add__), and corresponding ones.


def __radd__(self, other):
return Index(other + np.array(self))
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ def test_reset_index_range(self):
index=RangeIndex(stop=2))
assert_frame_equal(result, expected)

def test_reset_index_name(self):
# issue `17067`: after reset_index, index name should be None
df = pd.DataFrame(index=pd.Index([], name='x'))
assert df.reset_index(level=[]).index.name is None

def test_set_index_names(self):
df = pd.util.testing.makeDataFrame()
df.index.name = 'name'
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,14 @@ def test_iadd_string(self):
index += '_x'
assert 'a_x' in index

def test_iadd_index_name(self):
# issue `17067`: test Index keeping index name after plus op
s = pd.Series([1, 2, 3])
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 as a comment. add a 1-liner here what you are testing

s.index.name = 'foo'

s.index += 1
assert s.index.name == 'foo'

def test_difference(self):

first = self.strIndex[5:20]
Expand Down