Skip to content

Backport PR #48058 on branch 1.4.x (REGR: fix reset_index (Index.insert) regression with custom Index subclasses) #48199

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
- Fixed regression using custom Index subclasses (for example, used in xarray) with :meth:`~DataFrame.reset_index` or :meth:`Index.insert` (:issue:`47071`)
- Fixed regression in :meth:`DatetimeIndex.intersection` when the :class:`DatetimeIndex` has dates crossing daylight savings time (:issue:`46702`)
- Fixed regression in :func:`merge` throwing an error when passing a :class:`Series` with a multi-level name (:issue:`47946`)
- Fixed regression in :meth:`DataFrame.eval` creating a copy when updating inplace (:issue:`47449`)
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6614,9 +6614,12 @@ def insert(self, loc: int, item) -> Index:
loc = loc if loc >= 0 else loc - 1
new_values[loc] = item

# Use self._constructor instead of Index to retain NumericIndex GH#43921
# TODO(2.0) can use Index instead of self._constructor
return self._constructor._with_infer(new_values, name=self.name)
if self._typ == "numericindex":
# Use self._constructor instead of Index to retain NumericIndex GH#43921
# TODO(2.0) can use Index instead of self._constructor
return self._constructor._with_infer(new_values, name=self.name)
else:
return Index._with_infer(new_values, name=self.name)

def drop(self, labels, errors: str_t = "raise") -> Index:
"""
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/indexes/test_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Tests involving custom Index subclasses
"""
import numpy as np

from pandas import (
DataFrame,
Index,
)
import pandas._testing as tm


class CustomIndex(Index):
def __new__(cls, data, name=None):
# assert that this index class cannot hold strings
if any(isinstance(val, str) for val in data):
raise TypeError("CustomIndex cannot hold strings")

if name is None and hasattr(data, "name"):
name = data.name
data = np.array(data, dtype="O")

return cls._simple_new(data, name)


def test_insert_fallback_to_base_index():
# https://github.com/pandas-dev/pandas/issues/47071

idx = CustomIndex([1, 2, 3])
result = idx.insert(0, "string")
expected = Index(["string", 1, 2, 3], dtype=object)
tm.assert_index_equal(result, expected)

df = DataFrame(
np.random.randn(2, 3), columns=idx, index=Index([1, 2], name="string")
)
result = df.reset_index()
tm.assert_index_equal(result.columns, expected)