Skip to content

Commit f266817

Browse files
simonjayhawkinsSeeminSyed
authored andcommitted
TYP: Remove _ensure_type (pandas-dev#32633)
* remove _ensure_type by TomAugspurger * mypy fixup * add test * add whatsnew
1 parent 1223029 commit f266817

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
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

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

1111
import pandas._libs.lib as lib
12-
from pandas._typing import T
1312
from pandas.compat import PYPY
1413
from pandas.compat.numpy import function as nv
1514
from pandas.errors import AbstractMethodError
@@ -87,15 +86,6 @@ def __sizeof__(self):
8786
# no memory_usage attribute, so fall back to object's 'sizeof'
8887
return super().__sizeof__()
8988

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

10090
class NoNewAttributesMixin:
10191
"""

pandas/core/frame.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -3637,7 +3637,7 @@ def reindex(self, *args, **kwargs) -> "DataFrame":
36373637
# Pop these, since the values are in `kwargs` under different names
36383638
kwargs.pop("axis", None)
36393639
kwargs.pop("labels", None)
3640-
return self._ensure_type(super().reindex(**kwargs))
3640+
return super().reindex(**kwargs)
36413641

36423642
def drop(
36433643
self,
@@ -3955,8 +3955,8 @@ def replace(
39553955

39563956
@Appender(_shared_docs["shift"] % _shared_doc_kwargs)
39573957
def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> "DataFrame":
3958-
return self._ensure_type(
3959-
super().shift(periods=periods, freq=freq, axis=axis, fill_value=fill_value)
3958+
return super().shift(
3959+
periods=periods, freq=freq, axis=axis, fill_value=fill_value
39603960
)
39613961

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

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

pandas/core/generic.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -8442,9 +8442,9 @@ def _align_frame(
84428442
)
84438443

84448444
if method is not None:
8445-
left = self._ensure_type(
8446-
left.fillna(method=method, axis=fill_axis, limit=limit)
8447-
)
8445+
_left = left.fillna(method=method, axis=fill_axis, limit=limit)
8446+
assert _left is not None # needed for mypy
8447+
left = _left
84488448
right = right.fillna(method=method, axis=fill_axis, limit=limit)
84498449

84508450
# if DatetimeIndex have different tz, convert to UTC
@@ -9977,9 +9977,9 @@ def pct_change(
99779977
if fill_method is None:
99789978
data = self
99799979
else:
9980-
data = self._ensure_type(
9981-
self.fillna(method=fill_method, axis=axis, limit=limit)
9982-
)
9980+
_data = self.fillna(method=fill_method, axis=axis, limit=limit)
9981+
assert _data is not None # needed for mypy
9982+
data = _data
99839983

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

pandas/core/reshape/pivot.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def pivot_table(
150150
table = table.sort_index(axis=1)
151151

152152
if fill_value is not None:
153-
table = table._ensure_type(table.fillna(fill_value, downcast="infer"))
153+
_table = table.fillna(fill_value, downcast="infer")
154+
assert _table is not None # needed for mypy
155+
table = _table
154156

155157
if margins:
156158
if dropna:

pandas/io/formats/format.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ def _chk_truncate(self) -> None:
283283
series = series.iloc[:max_rows]
284284
else:
285285
row_num = max_rows // 2
286-
series = series._ensure_type(
287-
concat((series.iloc[:row_num], series.iloc[-row_num:]))
288-
)
286+
series = concat((series.iloc[:row_num], series.iloc[-row_num:]))
289287
self.tr_row_num = row_num
290288
else:
291289
self.tr_row_num = None

pandas/tests/frame/indexing/test_indexing.py

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

1608+
def test_reindex_subclass(self):
1609+
# https://github.com/pandas-dev/pandas/issues/31925
1610+
class MyDataFrame(DataFrame):
1611+
pass
1612+
1613+
expected = DataFrame()
1614+
df = MyDataFrame()
1615+
result = df.reindex_like(expected)
1616+
1617+
tm.assert_frame_equal(result, expected)
1618+
16081619
def test_reindex_methods_nearest_special(self):
16091620
df = pd.DataFrame({"x": list(range(5))})
16101621
target = np.array([-0.1, 0.9, 1.1, 1.5])

0 commit comments

Comments
 (0)