diff --git a/python/cudf/cudf/core/column/interval.py b/python/cudf/cudf/core/column/interval.py index dcec8957bb2..dc609f732e0 100644 --- a/python/cudf/cudf/core/column/interval.py +++ b/python/cudf/cudf/core/column/interval.py @@ -123,7 +123,7 @@ def to_pandas( if nullable: raise NotImplementedError(f"{nullable=} is not implemented.") elif arrow_type: - raise NotImplementedError(f"{nullable=} is not implemented.") + raise NotImplementedError(f"{arrow_type=} is not implemented.") return pd.Series( self.dtype.to_pandas().__from_arrow__(self.to_arrow()), index=index ) diff --git a/python/cudf/cudf/core/column/lists.py b/python/cudf/cudf/core/column/lists.py index d1bf0b74d3c..1c2bcbef2ec 100644 --- a/python/cudf/cudf/core/column/lists.py +++ b/python/cudf/cudf/core/column/lists.py @@ -294,17 +294,23 @@ def to_pandas( *, index: Optional[pd.Index] = None, nullable: bool = False, + arrow_type: bool = False, ) -> pd.Series: # Can't rely on Column.to_pandas implementation for lists. # Need to perform `to_pylist` to preserve list types. + if arrow_type and nullable: + raise ValueError( + f"{arrow_type=} and {nullable=} cannot both be set." + ) if nullable: raise NotImplementedError(f"{nullable=} is not implemented.") - - pd_series = pd.Series(self.to_arrow().to_pylist(), dtype="object") - - if index is not None: - pd_series.index = index - return pd_series + pa_array = self.to_arrow() + if arrow_type: + return pd.Series( + pd.arrays.ArrowExtensionArray(pa_array), index=index + ) + else: + return pd.Series(pa_array.tolist(), dtype="object", index=index) class ListMethods(ColumnMethods): diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index 6b5c0406deb..e043f358bbe 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -2726,7 +2726,7 @@ def test_series_from_large_string(): def test_series_to_pandas_arrow_type_nullable_raises(scalar): pa_array = pa.array([scalar, None]) ser = cudf.Series(pa_array) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=".* cannot both be set"): ser.to_pandas(nullable=True, arrow_type=True)