From e0516e9933a06b064540cd7df0210b80a71b5af0 Mon Sep 17 00:00:00 2001 From: hurcy Date: Mon, 14 Aug 2017 14:17:53 +0900 Subject: [PATCH 1/4] BUGfix: reset_index can reset index name (#17067) --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/common.py | 2 +- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/test_base.py | 11 +++++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 8b2c4d16f4e1a..e470aa595ecd1 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -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`) I/O ^^^ diff --git a/pandas/core/common.py b/pandas/core/common.py index 44cb36b8a3207..4d08f12c5dc6b 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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') def _mut_exclusive(**kwargs): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index de6221987a59a..887e415243ed9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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) def __radd__(self, other): return Index(other + np.array(self)) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index ef36e4a91aa1c..d25d1ed532a15 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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]) + s.index.name = 'foo' + + s.index += 1 + assert s.index.name == 'foo' + + def test_reset_index_name(self): + df = pd.DataFrame(index=pd.Index([], name='x')) + assert df.reset_index(level=[]).index.name is 'index' + def test_difference(self): first = self.strIndex[5:20] From 2d0e937176c50cefe7590c85dd7d2d0bf55c0375 Mon Sep 17 00:00:00 2001 From: hurcy Date: Tue, 15 Aug 2017 15:10:52 +0900 Subject: [PATCH 2/4] BUGfix: reset_index can reset index name (#17067) --- pandas/core/common.py | 4 ++-- pandas/tests/indexes/test_base.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 4d08f12c5dc6b..1f4ad722bac1d 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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='index') + return RangeIndex(0, n, name=name) def _mut_exclusive(**kwargs): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index d25d1ed532a15..9f35fdd25db3b 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -868,7 +868,7 @@ def test_iadd_index_name(self): def test_reset_index_name(self): df = pd.DataFrame(index=pd.Index([], name='x')) - assert df.reset_index(level=[]).index.name is 'index' + assert df.reset_index(level=[]).index.name is None def test_difference(self): From f4d1ece5cb1f4de5f59b3b8e27fd8a1f0e286867 Mon Sep 17 00:00:00 2001 From: hurcy Date: Wed, 16 Aug 2017 22:06:47 +0900 Subject: [PATCH 3/4] update @jreback reviews --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/tests/frame/test_alter_axes.py | 5 +++++ pandas/tests/indexes/test_base.py | 5 +---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index e470aa595ecd1..403915eef015e 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -324,7 +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`) +- Fixes in ``Index.__add__`` disappearing index name after plus operation (:issue:`17067`) I/O ^^^ diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 8bcc19e6d8ba4..0826aa9cebece 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -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' diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 9f35fdd25db3b..d153f98800bc7 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -860,16 +860,13 @@ def test_iadd_string(self): 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]) s.index.name = 'foo' s.index += 1 assert s.index.name == 'foo' - def test_reset_index_name(self): - df = pd.DataFrame(index=pd.Index([], name='x')) - assert df.reset_index(level=[]).index.name is None - def test_difference(self): first = self.strIndex[5:20] From 575976338914e97610db1e85714eddf64198c5f7 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 5 Jan 2018 18:47:18 -0500 Subject: [PATCH 4/4] fix whatsnew --- doc/source/whatsnew/v0.21.0.txt | 4 ---- doc/source/whatsnew/v0.23.0.txt | 3 ++- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index c50e99f01f357..3e673bd4cbc28 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -1067,9 +1067,6 @@ 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`) -<<<<<<< HEAD -- Fixes in ``Index.__add__`` disappearing index name after plus operation (:issue:`17067`) -======= - Bug in ``.iloc`` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` causing the wrong indexes to be read from and written to (:issue:`17148`) - Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`) - Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`) @@ -1078,7 +1075,6 @@ Indexing - Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) - Bug in :func:`Series.rename` when called with a callable, incorrectly alters the name of the ``Series``, rather than the name of the ``Index``. (:issue:`17407`) - Bug in :func:`String.str_get` raises ``IndexError`` instead of inserting NaNs when using a negative index. (:issue:`17704`) ->>>>>>> master I/O ^^^ diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index d53de30187156..761149ee7bc63 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -338,7 +338,7 @@ Conversion - Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`) - Bug in :class:`FY5253Quarter`, :class:`LastWeekOfMonth` where rollback and rollforward behavior was inconsistent with addition and subtraction behavior (:issue:`18854`) - Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`) -- Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`) +- Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`) - Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`) - Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) @@ -364,6 +364,7 @@ Indexing - Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`) - Bug in ``__setitem__`` when indexing a :class:`DataFrame` with a 2-d boolean ndarray (:issue:`18582`) - Bug in ``str.extractall`` when there were no matches empty :class:`Index` was returned instead of appropriate :class:`MultiIndex` (:issue:`19034`) +- Bug in :func:`Index.__add__` would lose the index name after operations (:issue:`17067`) I/O ^^^