Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling conversion of empty categorical with dtype_backend='pyarrow' #59935

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ def convert_dtypes(

else:
inferred_dtype = input_array.dtype

if dtype_backend == "pyarrow":
from pandas.core.arrays.arrow.array import to_pyarrow_type
from pandas.core.arrays.string_ import StringDtype
Expand Down Expand Up @@ -1145,12 +1145,16 @@ def convert_dtypes(
and isna(input_array).all()
):
import pyarrow as pa

pa_type = pa.null()
else:
pa_type = to_pyarrow_type(base_dtype)
if pa_type is not None:
inferred_dtype = ArrowDtype(pa_type)
if isna(input_array).all() and hasattr(input_array, 'categories'):
inferred_dtype = input_array.dtype
else:
inferred_dtype = ArrowDtype(pa_type)
Copy link
Member

@rhshadrach rhshadrach Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, can you add the check not isinstance(input_array.dtype, CategoricalDtype) to the if statement that starts on L1142. Then in the case to categorical dtypes, to_pyarrow_type returns None on L1151 and we do not modify inferred_dtype.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point!


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert the addition of this newline

elif dtype_backend == "numpy_nullable" and isinstance(inferred_dtype, ArrowDtype):
# GH 53648
inferred_dtype = _arrow_dtype_mapping()[inferred_dtype.pyarrow_dtype]
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def test_convert_empty(self):
# Empty DataFrame can pass convert_dtypes, see GH#40393
empty_df = pd.DataFrame()
tm.assert_frame_equal(empty_df, empty_df.convert_dtypes())

def test_convert_empty_categorical_to_pyarrow(self):
rhshadrach marked this conversation as resolved.
Show resolved Hide resolved
df = pd.DataFrame(
rhshadrach marked this conversation as resolved.
Show resolved Hide resolved
{
"A": pd.Series(pd.Categorical([None] * 5)),
"B": pd.Series(pd.Categorical([None] * 5, categories=["B1", "B2"])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to wrap in pd.Series here; supplying pd.Cateogrical is sufficient in the constructor.

}
)
converted = df.convert_dtypes(dtype_backend="pyarrow")
expected = df
tm.assert_frame_equal(converted, expected)

assert df.A.dtype == "category", "Dtype in column A is not 'category'"
assert df.B.dtype == "category", "Dtype in column B is not 'category'"
assert df.A.cat.categories.empty, "Categories in column A are not empty"
assert (df.B.cat.categories == ["B1", "B2"]).all(), "Categories in column A are not empty"

def test_convert_dtypes_retain_column_names(self):
# GH#41435
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,16 @@ def test_convert_dtypes_pyarrow_null(self):
result = ser.convert_dtypes(dtype_backend="pyarrow")
expected = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
tm.assert_series_equal(result, expected)

def test_convert_empty_categorical_to_pyarrow(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_convert_empty_categorical_to_pyarrow(self):
def test_convert_empty_categorical_to_pyarrow(self):
pytest.importorskip("pyarrow")

ser = pd.Series(pd.Series(pd.Categorical([None] * 5)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar - You can remove one pd.Series here and can you add # GH#59934 as the first line of this test.


converted = ser.convert_dtypes(dtype_backend="pyarrow")
expected = ser
tm.assert_series_equal(converted, expected)

assert ser.dtype == "category", "Series dtype is not 'category'"
assert ser.cat.categories.empty, "Series categories are not empty"

ser2 = pd.Series(pd.Series(pd.Categorical([None] * 5, categories=["S1", "S2"])))
assert (ser2.cat.categories == ["S1", "S2"]).all(), "Series categories are not empty"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this testing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we need to make sure that the set categories in empty categorical are propagated after conversion... i've adapted this in new pr

Loading