diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 37e4c9a1378d1..c7545ed38085f 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -548,7 +548,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 @@ -616,6 +616,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 f0f5bd7c3e2b2..93a68ef3703fd 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",