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 ^^^ diff --git a/pandas/core/common.py b/pandas/core/common.py index e606be3cc2a23..3e240ce383586 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -205,9 +205,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): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 55a26d57fa1d6..bc0513c42e023 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2141,7 +2141,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/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index c824f0026af50..891dde0975505 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -824,6 +824,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 5109542403b43..09cc38b40fc6d 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -957,6 +957,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]) + s.index.name = 'foo' + + s.index += 1 + assert s.index.name == 'foo' + def test_difference(self): first = self.strIndex[5:20]