-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Changes from 12 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 |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
import numpy as np | ||
import pytest | ||
|
||
import pandas.util._test_decorators as td | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
@@ -35,6 +37,26 @@ def test_convert_empty(self): | |
empty_df = pd.DataFrame() | ||
tm.assert_frame_equal(empty_df, empty_df.convert_dtypes()) | ||
|
||
@td.skip_if_no("pyarrow") | ||
def test_convert_empty_categorical_to_pyarrow(self): | ||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# GH#59934 | ||
df = pd.DataFrame( | ||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
"A": pd.Categorical([None] * 5), | ||
"B": pd.Categorical([None] * 5, categories=["B1", "B2"]), | ||
} | ||
) | ||
converted = df.convert_dtypes(dtype_backend="pyarrow") | ||
expected = df | ||
tm.assert_frame_equal(converted, expected) | ||
|
||
assert converted.A.dtype == "category", "Dtype in column A is not 'category'" | ||
assert converted.B.dtype == "category", "Dtype in column B is not 'category'" | ||
assert converted.A.cat.categories.empty, "Categories in column A are not empty" | ||
assert converted.B.cat.categories.isin( | ||
["B1", "B2"] | ||
).all(), "Categories in column B doesn't contain adequate categories" | ||
Comment on lines
+53
to
+58
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. These are all covered in |
||
|
||
def test_convert_dtypes_retain_column_names(self): | ||
# GH#41435 | ||
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||||
import pytest | ||||||||
|
||||||||
from pandas._libs import lib | ||||||||
import pandas.util._test_decorators as td | ||||||||
|
||||||||
import pandas as pd | ||||||||
import pandas._testing as tm | ||||||||
|
@@ -297,3 +298,20 @@ 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) | ||||||||
|
||||||||
@td.skip_if_no("pyarrow") | ||||||||
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.isin( | ||||||||
["S1", "S2"] | ||||||||
).all(), "Categories in ser2 doesn't contain adequate categories" | ||||||||
Comment on lines
+315
to
+317
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. |
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.
It looks like merge conflict-resolution went the wrong way here. I think these should likely all be removed.
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.
applied the changes, should be OK now