From b3fbf48d47675914c5a9ce94f1e412d660957e7f Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 21 Dec 2023 01:26:44 +0100 Subject: [PATCH] BUG: convert_dtypes raising when preserving object dtype --- pandas/core/internals/blocks.py | 2 +- pandas/tests/frame/methods/test_convert_dtypes.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 1af2d9e739038..20eff9315bc80 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -746,7 +746,7 @@ def astype( Block """ values = self.values - if squeeze and values.ndim == 2: + if squeeze and values.ndim == 2 and is_1d_only_ea_dtype(dtype): if values.shape[0] != 1: raise ValueError("Can not squeeze with more than one column.") values = values[0, :] # type: ignore[call-overload] diff --git a/pandas/tests/frame/methods/test_convert_dtypes.py b/pandas/tests/frame/methods/test_convert_dtypes.py index a181a271181ca..521d2cb14ac6a 100644 --- a/pandas/tests/frame/methods/test_convert_dtypes.py +++ b/pandas/tests/frame/methods/test_convert_dtypes.py @@ -193,3 +193,10 @@ def test_convert_dtypes_avoid_block_splitting(self): ) tm.assert_frame_equal(result, expected) assert result._mgr.nblocks == 2 + + def test_convert_dtypes_from_arrow(self): + # GH#56581 + df = pd.DataFrame([["a", datetime.time(18, 12)]], columns=["a", "b"]) + result = df.convert_dtypes() + expected = df.astype({"a": "string[python]"}) + tm.assert_frame_equal(result, expected)