From ca5e77fe9c71b1b7052ccbb80b20735e1219250c Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 14 Jul 2023 14:14:37 -0700 Subject: [PATCH 1/2] BUG: DataFrame repr with ArrowDtype with extension --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/dtypes/dtypes.py | 5 +++-- pandas/tests/extension/test_arrow.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index b7cc254d5c7e5..3c35544c325d1 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -592,6 +592,7 @@ ExtensionArray - Bug in :meth:`Series.quantile` for pyarrow temporal types raising ArrowInvalid (:issue:`52678`) - Bug in :meth:`Series.rank` returning wrong order for small values with ``Float64`` dtype (:issue:`52471`) - Bug in :meth:`~arrays.ArrowExtensionArray.__iter__` and :meth:`~arrays.ArrowExtensionArray.__getitem__` returning python datetime and timedelta objects for non-nano dtypes (:issue:`53326`) +- Bug where the :class:`DataFrame` repr would not work when a column would have an :class:`ArrowDtype` with an ``pyarrow.ExtensionDtype`` (:issue:`54063`) - Bug where the ``__from_arrow__`` method of masked ExtensionDtypes(e.g. :class:`Float64Dtype`, :class:`BooleanDtype`) would not accept pyarrow arrays of type ``pyarrow.null()`` (:issue:`52223`) Styler diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 04e2b00744156..c19d6f778efb3 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -2102,8 +2102,9 @@ def type(self): elif pa.types.is_null(pa_type): # TODO: None? pd.NA? pa.null? return type(pa_type) - else: - raise NotImplementedError(pa_type) + elif isinstance(pa_type, pa.ExtensionType): + return type(self)(pa_type.storage_type).type + raise NotImplementedError(pa_type) @property def name(self) -> str: # type: ignore[override] diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index f622ef770b63f..7b74b0a2e2ef0 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -61,6 +61,7 @@ pa = pytest.importorskip("pyarrow", minversion="7.0.0") from pandas.core.arrays.arrow.array import ArrowExtensionArray +from pandas.core.arrays.arrow.extension_types import ArrowPeriodType @pytest.fixture(params=tm.ALL_PYARROW_DTYPES, ids=str) @@ -3139,3 +3140,15 @@ def test_to_numpy_temporal(pa_type): expected = np.array(expected, dtype=object) assert result[0].unit == expected[0].unit tm.assert_numpy_array_equal(result, expected) + + +def test_arrowextensiondtype_dataframe_repr(): + # GH 54062 + df = pd.DataFrame( + pd.period_range("2012", periods=3), + columns=["col"], + dtype=ArrowDtype(ArrowPeriodType("D")), + ) + result = repr(df) + expected = " col\n0 15340\n1 15341\n2 15342" + assert result == expected From 0c20f1d97234ebb6442a75e079914d2e6fa4a873 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 17 Jul 2023 13:14:41 -0700 Subject: [PATCH 2/2] add comment --- pandas/tests/extension/test_arrow.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index c23e8ac40d7bd..2a6b57a365a11 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -3154,5 +3154,7 @@ def test_arrowextensiondtype_dataframe_repr(): dtype=ArrowDtype(ArrowPeriodType("D")), ) result = repr(df) + # TODO: repr value may not be expected; address how + # pyarrow.ExtensionType values are displayed expected = " col\n0 15340\n1 15341\n2 15342" assert result == expected