-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
Oh, now I realize that I shouldn't modify core/common.py :( |
pandas/core/common.py
Outdated
@@ -206,7 +206,7 @@ def is_bool_indexer(key): | |||
|
|||
def _default_index(n): | |||
from pandas.core.index import RangeIndex | |||
return RangeIndex(0, n, name=None) | |||
return RangeIndex(0, n, name='index') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is right... the name of a default index should just be None.
Codecov Report
@@ Coverage Diff @@
## master #17243 +/- ##
=======================================
Coverage 91.53% 91.53%
=======================================
Files 148 148
Lines 48688 48688
=======================================
Hits 44566 44566
Misses 4122 4122
Continue to review full report at Codecov.
|
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`) | |||
- Bug in reset_index reset index name (:issue: `17067`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be more descriptive here, no space before the issue number and the colon
pandas/tests/indexes/test_base.py
Outdated
@@ -859,6 +859,17 @@ def test_iadd_string(self): | |||
index += '_x' | |||
assert 'a_x' in index | |||
|
|||
def test_iadd_index_name(self): | |||
s = pd.Series([1, 2, 3]) |
There was a problem hiding this comment.
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
pandas/tests/indexes/test_base.py
Outdated
s.index += 1 | ||
assert s.index.name == 'foo' | ||
|
||
def test_reset_index_name(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test needs to go to pandas/tests/frame/test_alter_axes.py (with other reset_index tests)
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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.
can you rebase and update |
and move note to 0.22.0 |
can you rebase / update |
I rebased; if you can fix according to comments. |
@hurcy can you rebase this and update |
closing as stale |
git diff upstream/master -u -- "*.py" | flake8 --diff
I added name parameter value to preserve index name before
reset_index
.