-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Handling conversion of empty categorical with dtype_backend='pyarrow' #59935
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
Changes from 5 commits
e6ef750
c005850
5ce426d
94769a1
8e879da
a5b3882
c1ed1f0
6747736
82cecd1
ae0a148
c0d8178
902601f
482080d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1143,6 +1143,7 @@ def convert_dtypes( | |
base_dtype.kind == "O" # type: ignore[union-attr] | ||
and input_array.size > 0 | ||
and isna(input_array).all() | ||
and not isinstance(input_array.dtype, CategoricalDtype) | ||
): | ||
import pyarrow as pa | ||
|
||
|
@@ -1151,6 +1152,7 @@ def convert_dtypes( | |
pa_type = to_pyarrow_type(base_dtype) | ||
if pa_type is not None: | ||
inferred_dtype = ArrowDtype(pa_type) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -297,3 +297,21 @@ 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): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
# GH#59934 | ||||||||
ser1 = pd.Series(pd.Categorical([None] * 5)) | ||||||||
converted1 = ser1.convert_dtypes(dtype_backend="pyarrow") | ||||||||
expected = ser1 | ||||||||
|
||||||||
tm.assert_series_equal(converted1, expected) | ||||||||
assert converted1.dtype == "category", "Series dtype is not 'category'" | ||||||||
assert converted1.cat.categories.empty, "Series categories are not empty" | ||||||||
Comment on lines
+310
to
+311
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar, can remove. |
||||||||
|
||||||||
ser2 = pd.Series(pd.Categorical([None] * 5, categories=["S1", "S2"])) | ||||||||
converted2 = ser2.convert_dtypes(dtype_backend="pyarrow") | ||||||||
assert converted2.cat.categories.__contains__( | ||||||||
"S1" | ||||||||
) and converted2.cat.categories.__contains__( | ||||||||
"S2" | ||||||||
), "Categories in ser2 doesn't contain adequate categories" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.