Skip to content

Commit

Permalink
BUG: Fix convert_dtypes for all na column and arrow backend (#55346)
Browse files Browse the repository at this point in the history
* BUG: Fix convert_dtypes for all na column and arrow backend

BUG: Fix convert_dtypes for all na column and arrow backend

* Add test

* Update cast.py

* Fix

* Fix typing
  • Loading branch information
phofl authored Oct 2, 2023
1 parent a0a6e04 commit 93abfb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Numeric

Conversion
^^^^^^^^^^
-
- Bug in :meth:`Series.convert_dtypes` not converting all NA column to ``null[pyarrow]`` (:issue:`55346`)
-

Strings
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,16 @@ def convert_dtypes(
base_dtype = np.dtype(str)
else:
base_dtype = inferred_dtype
pa_type = to_pyarrow_type(base_dtype)
if (
base_dtype.kind == "O" # type: ignore[union-attr]
and len(input_array) > 0
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)
elif dtype_backend == "numpy_nullable" and isinstance(inferred_dtype, ArrowDtype):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,11 @@ def test_convert_dtypes_pyarrow_to_np_nullable(self):
result = ser.convert_dtypes(dtype_backend="numpy_nullable")
expected = pd.Series(range(2), dtype="Int32")
tm.assert_series_equal(result, expected)

def test_convert_dtypes_pyarrow_null(self):
# GH#55346
pa = pytest.importorskip("pyarrow")
ser = pd.Series([None, None])
result = ser.convert_dtypes(dtype_backend="pyarrow")
expected = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
tm.assert_series_equal(result, expected)

0 comments on commit 93abfb3

Please sign in to comment.