Skip to content

Commit 0e36d7e

Browse files
Backport PR #32633: TYP: Remove _ensure_type (#32637)
1 parent 4dae4ab commit 0e36d7e

File tree

7 files changed

+31
-30
lines changed

7 files changed

+31
-30
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Fixed regressions
2828
- Fixed regression in the repr of an object-dtype :class:`Index` with bools and missing values (:issue:`32146`)
2929
- Fixed regression in :meth:`read_csv` in which the ``encoding`` option was not recognized with certain file-like objects (:issue:`31819`)
3030
- Fixed regression in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with (tz-aware) index and ``method=nearest`` (:issue:`26683`)
31+
- Fixed regression in :meth:`DataFrame.reindex_like` on a :class:`DataFrame` subclass raised an ``AssertionError`` (:issue:`31925`)
3132

3233

3334
.. ---------------------------------------------------------------------------

pandas/core/base.py

-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import numpy as np
99

1010
import pandas._libs.lib as lib
11-
from pandas._typing import T
1211
from pandas.compat import PYPY
1312
from pandas.compat.numpy import function as nv
1413
from pandas.errors import AbstractMethodError
@@ -85,14 +84,6 @@ def __sizeof__(self):
8584
# object's 'sizeof'
8685
return super().__sizeof__()
8786

88-
def _ensure_type(self: T, obj) -> T:
89-
"""Ensure that an object has same type as self.
90-
91-
Used by type checkers.
92-
"""
93-
assert isinstance(obj, type(self)), type(obj)
94-
return obj
95-
9687

9788
class NoNewAttributesMixin:
9889
"""Mixin which prevents adding new attributes.

pandas/core/frame.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -3853,7 +3853,7 @@ def reindex(self, *args, **kwargs) -> "DataFrame":
38533853
# Pop these, since the values are in `kwargs` under different names
38543854
kwargs.pop("axis", None)
38553855
kwargs.pop("labels", None)
3856-
return self._ensure_type(super().reindex(**kwargs))
3856+
return super().reindex(**kwargs)
38573857

38583858
def drop(
38593859
self,
@@ -4174,8 +4174,8 @@ def replace(
41744174

41754175
@Appender(_shared_docs["shift"] % _shared_doc_kwargs)
41764176
def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> "DataFrame":
4177-
return self._ensure_type(
4178-
super().shift(periods=periods, freq=freq, axis=axis, fill_value=fill_value)
4177+
return super().shift(
4178+
periods=periods, freq=freq, axis=axis, fill_value=fill_value
41794179
)
41804180

41814181
def set_index(
@@ -8410,14 +8410,12 @@ def isin(self, values) -> "DataFrame":
84108410
from pandas.core.reshape.concat import concat
84118411

84128412
values = collections.defaultdict(list, values)
8413-
return self._ensure_type(
8414-
concat(
8415-
(
8416-
self.iloc[:, [i]].isin(values[col])
8417-
for i, col in enumerate(self.columns)
8418-
),
8419-
axis=1,
8420-
)
8413+
return concat(
8414+
(
8415+
self.iloc[:, [i]].isin(values[col])
8416+
for i, col in enumerate(self.columns)
8417+
),
8418+
axis=1,
84218419
)
84228420
elif isinstance(values, Series):
84238421
if not values.index.is_unique:

pandas/core/generic.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -8536,9 +8536,9 @@ def _align_frame(
85368536
)
85378537

85388538
if method is not None:
8539-
left = self._ensure_type(
8540-
left.fillna(method=method, axis=fill_axis, limit=limit)
8541-
)
8539+
_left = left.fillna(method=method, axis=fill_axis, limit=limit)
8540+
assert _left is not None # needed for mypy
8541+
left = _left
85428542
right = right.fillna(method=method, axis=fill_axis, limit=limit)
85438543

85448544
# if DatetimeIndex have different tz, convert to UTC
@@ -10079,9 +10079,9 @@ def pct_change(
1007910079
if fill_method is None:
1008010080
data = self
1008110081
else:
10082-
data = self._ensure_type(
10083-
self.fillna(method=fill_method, axis=axis, limit=limit)
10084-
)
10082+
_data = self.fillna(method=fill_method, axis=axis, limit=limit)
10083+
assert _data is not None # needed for mypy
10084+
data = _data
1008510085

1008610086
rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1
1008710087
if freq is not None:

pandas/core/reshape/pivot.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ def pivot_table(
148148
table = table.sort_index(axis=1)
149149

150150
if fill_value is not None:
151-
table = table._ensure_type(table.fillna(fill_value, downcast="infer"))
151+
_table = table.fillna(fill_value, downcast="infer")
152+
assert _table is not None # needed for mypy
153+
table = _table
152154

153155
if margins:
154156
if dropna:

pandas/io/formats/format.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ def _chk_truncate(self) -> None:
281281
series = series.iloc[:max_rows]
282282
else:
283283
row_num = max_rows // 2
284-
series = series._ensure_type(
285-
concat((series.iloc[:row_num], series.iloc[-row_num:]))
286-
)
284+
series = concat((series.iloc[:row_num], series.iloc[-row_num:]))
287285
self.tr_row_num = row_num
288286
else:
289287
self.tr_row_num = None

pandas/tests/frame/indexing/test_indexing.py

+11
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,17 @@ def test_reindex_methods(self, method, expected_values):
15961596
actual = df[::-1].reindex(target, method=switched_method)
15971597
tm.assert_frame_equal(expected, actual)
15981598

1599+
def test_reindex_subclass(self):
1600+
# https://github.com/pandas-dev/pandas/issues/31925
1601+
class MyDataFrame(DataFrame):
1602+
pass
1603+
1604+
expected = DataFrame()
1605+
df = MyDataFrame()
1606+
result = df.reindex_like(expected)
1607+
1608+
tm.assert_frame_equal(result, expected)
1609+
15991610
def test_reindex_methods_nearest_special(self):
16001611
df = pd.DataFrame({"x": list(range(5))})
16011612
target = np.array([-0.1, 0.9, 1.1, 1.5])

0 commit comments

Comments
 (0)