Skip to content

Commit 33cd18b

Browse files
committed
Finish the pr
1 parent 2ca6467 commit 33cd18b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ Bug fixes
636636
Categorical
637637
^^^^^^^^^^^
638638
- Bug in :func:`Series.apply` where ``nan`` was ignored for :class:`CategoricalDtype` (:issue:`59938`)
639+
- Bug in :meth:`Series.convert_dtypes` with ``dtype_backend="pyarrow"`` where empty categorical series raise error or get converted to ``null[pyarrow]`` (:issue:`59934`)
639640
-
640641

641642
Datetimelike

pandas/tests/frame/methods/test_convert_dtypes.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_convert_empty(self):
3737
empty_df = pd.DataFrame()
3838
tm.assert_frame_equal(empty_df, empty_df.convert_dtypes())
3939

40+
@td.skip_if_no("pyarrow")
4041
def test_convert_empty_categorical_to_pyarrow(self):
4142
# GH#59934
4243
df = pd.DataFrame(
@@ -49,13 +50,6 @@ def test_convert_empty_categorical_to_pyarrow(self):
4950
expected = df
5051
tm.assert_frame_equal(converted, expected)
5152

52-
assert converted.A.dtype == "category", "Dtype in column A is not 'category'"
53-
assert converted.B.dtype == "category", "Dtype in column B is not 'category'"
54-
assert converted.A.cat.categories.empty, "Categories in column A are not empty"
55-
assert converted.B.cat.categories.isin(
56-
["B1", "B2"]
57-
).all(), "Categories in column B doesn't contain adequate categories"
58-
5953
def test_convert_dtypes_retain_column_names(self):
6054
# GH#41435
6155
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})

pandas/tests/series/methods/test_convert_dtypes.py

+13
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ def test_convert_dtypes_pyarrow_null(self):
299299
expected = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
300300
tm.assert_series_equal(result, expected)
301301

302+
@td.skip_if_no("pyarrow")
303+
def test_convert_empty_categorical_to_pyarrow(self):
304+
# GH#59934
305+
ser1 = pd.Series(pd.Categorical([None] * 5))
306+
converted1 = ser1.convert_dtypes(dtype_backend="pyarrow")
307+
expected = ser1
308+
tm.assert_series_equal(converted1, expected)
309+
310+
ser2 = pd.Series(pd.Categorical([None] * 5, categories=["S1", "S2"]))
311+
converted2 = ser2.convert_dtypes(dtype_backend="pyarrow")
312+
expected = ser2
313+
tm.assert_series_equal(converted2, expected)
314+
302315
def test_convert_dtype_pyarrow_timezone_preserve(self):
303316
# GH 60237
304317
pytest.importorskip("pyarrow")

0 commit comments

Comments
 (0)