From 02ab1334a83266c571a4b1ebd8d0201c6ba80c42 Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 22 Nov 2021 21:42:38 +0100 Subject: [PATCH] BUG: Series.reset_index not ignoring name with inplace and drop --- doc/source/whatsnew/v1.4.0.rst | 3 ++- pandas/core/series.py | 5 ----- pandas/tests/series/methods/test_reset_index.py | 7 +++++++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 1f656f267783f..ff5c8f28cb368 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -546,7 +546,7 @@ Datetimelike - Bug in constructing a :class:`Series` from datetime-like strings with mixed timezones incorrectly partially-inferring datetime values (:issue:`40111`) - Bug in addition with a :class:`Tick` object and a ``np.timedelta64`` object incorrectly raising instead of returning :class:`Timedelta` (:issue:`44474`) - Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`) -- Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc addine ``None`` and replacing existing value (:issue:`44509`) +- Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`) - Timedelta @@ -614,6 +614,7 @@ Indexing - Bug when setting string-backed :class:`Categorical` values that can be parsed to datetimes into a :class:`DatetimeArray` or :class:`Series` or :class:`DataFrame` column backed by :class:`DatetimeArray` failing to parse these strings (:issue:`44236`) - Bug in :meth:`Series.__setitem__` with an integer dtype other than ``int64`` setting with a ``range`` object unnecessarily upcasting to ``int64`` (:issue:`44261`) - Bug in :meth:`Series.__setitem__` with a boolean mask indexer setting a listlike value of length 1 incorrectly broadcasting that value (:issue:`44265`) +- Bug in :meth:`Series.reset_index` not ignoring ``name`` argument when ``drop`` and ``inplace`` are set to ``True`` (:issue:`44575`) - Bug in :meth:`DataFrame.loc.__setitem__` and :meth:`DataFrame.iloc.__setitem__` with mixed dtypes sometimes failing to operate in-place (:issue:`44345`) - Bug in :meth:`DataFrame.loc.__getitem__` incorrectly raising ``KeyError`` when selecting a single column with a boolean key (:issue:`44322`). diff --git a/pandas/core/series.py b/pandas/core/series.py index e0a63b8e35105..564b8ee7c1a77 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1449,9 +1449,6 @@ def reset_index(self, level=None, drop=False, name=lib.no_default, inplace=False """ inplace = validate_bool_kwarg(inplace, "inplace") if drop: - if name is lib.no_default: - name = self.name - new_index = default_index(len(self)) if level is not None: if not isinstance(level, (tuple, list)): @@ -1462,8 +1459,6 @@ def reset_index(self, level=None, drop=False, name=lib.no_default, inplace=False if inplace: self.index = new_index - # set name if it was passed, otherwise, keep the previous name - self.name = name or self.name else: return self._constructor( self._values.copy(), index=new_index diff --git a/pandas/tests/series/methods/test_reset_index.py b/pandas/tests/series/methods/test_reset_index.py index b159317bf813b..f38491508cc23 100644 --- a/pandas/tests/series/methods/test_reset_index.py +++ b/pandas/tests/series/methods/test_reset_index.py @@ -160,6 +160,13 @@ def test_drop_pos_args_deprecation(self): expected = DataFrame({"a": [1, 2, 3], 0: [1, 2, 3]}) tm.assert_frame_equal(result, expected) + def test_reset_index_inplace_and_drop_ignore_name(self): + # GH#44575 + ser = Series(range(2), name="old") + ser.reset_index(name="new", drop=True, inplace=True) + expected = Series(range(2), name="old") + tm.assert_series_equal(ser, expected) + @pytest.mark.parametrize( "array, dtype",