Skip to content

Commit 854987f

Browse files
authored
BUG: Reindex casting to object in certain situation (#48190)
1 parent fee9b5d commit 854987f

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/source/whatsnew/v1.6.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Interval
152152
Indexing
153153
^^^^^^^^
154154
- Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`)
155+
- 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`)
155156
-
156157

157158
Missing

pandas/core/generic.py

+5
Original file line numberDiff line numberDiff line change
@@ -5345,6 +5345,11 @@ def _needs_reindex_multi(self, axes, method, level) -> bool_t:
53455345
and method is None
53465346
and level is None
53475347
and not self._is_mixed_type
5348+
and not (
5349+
self.ndim == 2
5350+
and len(self.dtypes) == 1
5351+
and is_extension_array_dtype(self.dtypes.iloc[0])
5352+
)
53485353
)
53495354

53505355
def _reindex_multi(self, axes, copy, fill_value):

pandas/tests/frame/methods/test_reindex.py

+9
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,15 @@ def test_reindex_uint_dtypes_fill_value(self, any_unsigned_int_numpy_dtype):
782782
)
783783
tm.assert_frame_equal(result, expected)
784784

785+
def test_reindex_single_column_ea_index_and_columns(self, any_numeric_ea_dtype):
786+
# GH#48190
787+
df = DataFrame({"a": [1, 2]}, dtype=any_numeric_ea_dtype)
788+
result = df.reindex(columns=list("ab"), index=[0, 1, 2], fill_value=10)
789+
expected = DataFrame(
790+
{"a": Series([1, 2, 10], dtype=any_numeric_ea_dtype), "b": 10}
791+
)
792+
tm.assert_frame_equal(result, expected)
793+
785794
def test_reindex_dups(self):
786795

787796
# GH4746, reindex on duplicate index error messages

0 commit comments

Comments
 (0)