From 625999393857215e7c0f8d7391da60fa666fbddb Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 11 Mar 2020 17:10:38 +0000 Subject: [PATCH 1/4] remove _ensure_type by TomAugspurger --- pandas/core/base.py | 10 ---------- pandas/core/frame.py | 20 +++++++++----------- pandas/core/generic.py | 8 ++------ pandas/core/reshape/pivot.py | 2 +- pandas/io/formats/format.py | 4 +--- 5 files changed, 13 insertions(+), 31 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 478b83f538b7d..40ff0640a5bc4 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -9,7 +9,6 @@ import numpy as np import pandas._libs.lib as lib -from pandas._typing import T from pandas.compat import PYPY from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError @@ -87,15 +86,6 @@ def __sizeof__(self): # no memory_usage attribute, so fall back to object's 'sizeof' return super().__sizeof__() - def _ensure_type(self: T, obj) -> T: - """ - Ensure that an object has same type as self. - - Used by type checkers. - """ - assert isinstance(obj, type(self)), type(obj) - return obj - class NoNewAttributesMixin: """ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 30abcafb56ffb..d391f1be2c49f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3637,7 +3637,7 @@ def reindex(self, *args, **kwargs) -> "DataFrame": # Pop these, since the values are in `kwargs` under different names kwargs.pop("axis", None) kwargs.pop("labels", None) - return self._ensure_type(super().reindex(**kwargs)) + return super().reindex(**kwargs) def drop( self, @@ -3955,8 +3955,8 @@ def replace( @Appender(_shared_docs["shift"] % _shared_doc_kwargs) def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> "DataFrame": - return self._ensure_type( - super().shift(periods=periods, freq=freq, axis=axis, fill_value=fill_value) + return super().shift( + periods=periods, freq=freq, axis=axis, fill_value=fill_value ) def set_index( @@ -8409,14 +8409,12 @@ def isin(self, values) -> "DataFrame": from pandas.core.reshape.concat import concat values = collections.defaultdict(list, values) - return self._ensure_type( - concat( - ( - self.iloc[:, [i]].isin(values[col]) - for i, col in enumerate(self.columns) - ), - axis=1, - ) + return concat( + ( + self.iloc[:, [i]].isin(values[col]) + for i, col in enumerate(self.columns) + ), + axis=1, ) elif isinstance(values, Series): if not values.index.is_unique: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f53135174741e..388a0c0b462b9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8442,9 +8442,7 @@ def _align_frame( ) if method is not None: - left = self._ensure_type( - left.fillna(method=method, axis=fill_axis, limit=limit) - ) + left = left.fillna(method=method, axis=fill_axis, limit=limit) right = right.fillna(method=method, axis=fill_axis, limit=limit) # if DatetimeIndex have different tz, convert to UTC @@ -9977,9 +9975,7 @@ def pct_change( if fill_method is None: data = self else: - data = self._ensure_type( - self.fillna(method=fill_method, axis=axis, limit=limit) - ) + data = self.fillna(method=fill_method, axis=axis, limit=limit) rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1 if freq is not None: diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 61aa34f724307..0f3b8737ccb8e 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -150,7 +150,7 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - table = table._ensure_type(table.fillna(fill_value, downcast="infer")) + table = table.fillna(fill_value, downcast="infer") if margins: if dropna: diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index c879eaeda64e0..f011293273c5b 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -283,9 +283,7 @@ def _chk_truncate(self) -> None: series = series.iloc[:max_rows] else: row_num = max_rows // 2 - series = series._ensure_type( - concat((series.iloc[:row_num], series.iloc[-row_num:])) - ) + series = concat((series.iloc[:row_num], series.iloc[-row_num:])) self.tr_row_num = row_num else: self.tr_row_num = None From 276724452a267590c92f4c0f8889e8e94e3bba7f Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 11 Mar 2020 17:18:04 +0000 Subject: [PATCH 2/4] mypy fixup --- pandas/core/generic.py | 8 ++++++-- pandas/core/reshape/pivot.py | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 388a0c0b462b9..6f743d7388574 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8442,7 +8442,9 @@ def _align_frame( ) if method is not None: - left = left.fillna(method=method, axis=fill_axis, limit=limit) + _left = left.fillna(method=method, axis=fill_axis, limit=limit) + assert _left is not None # needed for mypy + left = _left right = right.fillna(method=method, axis=fill_axis, limit=limit) # if DatetimeIndex have different tz, convert to UTC @@ -9975,7 +9977,9 @@ def pct_change( if fill_method is None: data = self else: - data = self.fillna(method=fill_method, axis=axis, limit=limit) + _data = self.fillna(method=fill_method, axis=axis, limit=limit) + assert _data is not None # needed for mypy + data = _data rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1 if freq is not None: diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 0f3b8737ccb8e..a8801d8ab3f6e 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -150,7 +150,9 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - table = table.fillna(fill_value, downcast="infer") + _table = table.fillna(fill_value, downcast="infer") + assert _table is not None # needed for mypy + table = _table if margins: if dropna: From 67c97ef17f001e398a5e92e556c3c94004c40e6d Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 11 Mar 2020 17:54:01 +0000 Subject: [PATCH 3/4] add test --- pandas/tests/frame/indexing/test_indexing.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index ade17860a99b7..7892030a6727e 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1605,6 +1605,17 @@ def test_reindex_methods(self, method, expected_values): actual = df[::-1].reindex(target, method=switched_method) tm.assert_frame_equal(expected, actual) + def test_reindex_subclass(self): + # https://github.com/pandas-dev/pandas/issues/31925 + class MyDataFrame(DataFrame): + pass + + expected = DataFrame() + df = MyDataFrame() + result = df.reindex_like(expected) + + tm.assert_frame_equal(result, expected) + def test_reindex_methods_nearest_special(self): df = pd.DataFrame({"x": list(range(5))}) target = np.array([-0.1, 0.9, 1.1, 1.5]) From 63eb95098adaffeafe25d8b139a9ab8e6ae228c6 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 11 Mar 2020 18:07:21 +0000 Subject: [PATCH 4/4] add whatsnew --- doc/source/whatsnew/v1.0.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index 123dfa07f4331..3d5e77b0350e6 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -27,6 +27,7 @@ Fixed regressions - Fixed regression in the repr of an object-dtype :class:`Index` with bools and missing values (:issue:`32146`) - Fixed regression in :meth:`read_csv` in which the ``encoding`` option was not recognized with certain file-like objects (:issue:`31819`) - Fixed regression in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with (tz-aware) index and ``method=nearest`` (:issue:`26683`) +- Fixed regression in :meth:`DataFrame.reindex_like` on a :class:`DataFrame` subclass raised an ``AssertionError`` (:issue:`31925`) .. ---------------------------------------------------------------------------