From 08d7dbe5b35613dc2264fc3c2ab5c70f91625169 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 21 Aug 2022 22:11:44 +0200 Subject: [PATCH 1/5] BUG: Reindex casting to object in certain situation --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/generic.py | 3 +++ pandas/tests/frame/methods/test_reindex.py | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index c197f3df45814..652117347f8e5 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -1005,6 +1005,7 @@ Indexing - Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`) - Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) - Bug in :meth:`loc.__setitem__` treating ``range`` keys as positional instead of label-based (:issue:`45479`) +- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when indexing columns and index (:issue:`48190`) - Bug in :meth:`DataFrame.__setitem__` casting extension array dtypes to object when setting with a scalar key and :class:`DataFrame` as value (:issue:`46896`) - Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`) - Bug in :meth:`Series.loc` raising with boolean indexer containing ``NA`` when :class:`Index` did not match (:issue:`46551`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 342e4eae09257..76b642bb70185 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5333,6 +5333,9 @@ def _needs_reindex_multi(self, axes, method, level) -> bool_t: and method is None and level is None and not self._is_mixed_type + and not ( + len(self.dtypes) == 1 and is_extension_array_dtype(self.dtypes.iloc[0]) + ) ) def _reindex_multi(self, axes, copy, fill_value): diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index 8575e7895ae5a..fca84ba3036bf 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -772,6 +772,15 @@ def test_reindex_fill_value(self): expected = df.reindex(range(15)).fillna(0) tm.assert_frame_equal(result, expected) + def test_reindex_single_column_ea_index_and_columns(self, any_numeric_ea_dtype): + # GH#48190 + df = DataFrame({"a": [1, 2]}, dtype=any_numeric_ea_dtype) + result = df.reindex(columns=list("ab"), index=[0, 1, 2], fill_value=10) + expected = DataFrame( + {"a": Series([1, 2, 10], dtype=any_numeric_ea_dtype), "b": 10} + ) + tm.assert_frame_equal(result, expected) + def test_reindex_dups(self): # GH4746, reindex on duplicate index error messages From 74db786d37e9b1b4616592716c0a2f6b8cbb09fc Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Tue, 30 Aug 2022 10:01:32 +0200 Subject: [PATCH 2/5] Move whatsnew --- doc/source/whatsnew/v1.6.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index eac5e5d3a0f52..c4d8cfdfa5fb4 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -151,7 +151,7 @@ Interval Indexing ^^^^^^^^ -- +- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when indexing columns and index (:issue:`48190`) - Missing From 8640beffb22cfece2f017a8c4acc0938847c6051 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 1 Sep 2022 20:50:36 +0200 Subject: [PATCH 3/5] Remove whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 42df296d14f8a..8671b73526f80 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -1059,7 +1059,6 @@ Indexing - Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`) - Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) - Bug in :meth:`loc.__setitem__` treating ``range`` keys as positional instead of label-based (:issue:`45479`) -- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when indexing columns and index (:issue:`48190`) - Bug in :meth:`DataFrame.__setitem__` casting extension array dtypes to object when setting with a scalar key and :class:`DataFrame` as value (:issue:`46896`) - Bug in :meth:`Series.__setitem__` when setting a scalar to a nullable pandas dtype would not raise a ``TypeError`` if the scalar could not be cast (losslessly) to the nullable type (:issue:`45404`) - Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`) From b63988ffae47cf095c1f23f47a3fd931e190e75f Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 2 Sep 2022 01:10:39 +0200 Subject: [PATCH 4/5] Add check --- pandas/core/generic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 552f80a17f115..a6e58a0c90dc7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5346,7 +5346,9 @@ def _needs_reindex_multi(self, axes, method, level) -> bool_t: and level is None and not self._is_mixed_type and not ( - len(self.dtypes) == 1 and is_extension_array_dtype(self.dtypes.iloc[0]) + self.ndim == 2 + and len(self.dtypes) == 1 + and is_extension_array_dtype(self.dtypes.iloc[0]) ) ) From 662c970fac132a0254ffa788ccaf081623646601 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Fri, 2 Sep 2022 19:05:09 +0200 Subject: [PATCH 5/5] Update doc/source/whatsnew/v1.6.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.6.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index b57a5a78b9eca..6eea5daa3ac3e 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -152,7 +152,7 @@ Interval Indexing ^^^^^^^^ - Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`) -- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when indexing columns and index (:issue:`48190`) +- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`) - Missing