Skip to content

Commit 93abfb3

Browse files
authored
BUG: Fix convert_dtypes for all na column and arrow backend (#55346)
* 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
1 parent a0a6e04 commit 93abfb3

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Numeric
281281

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

287287
Strings

pandas/core/dtypes/cast.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,16 @@ def convert_dtypes(
11331133
base_dtype = np.dtype(str)
11341134
else:
11351135
base_dtype = inferred_dtype
1136-
pa_type = to_pyarrow_type(base_dtype)
1136+
if (
1137+
base_dtype.kind == "O" # type: ignore[union-attr]
1138+
and len(input_array) > 0
1139+
and isna(input_array).all()
1140+
):
1141+
import pyarrow as pa
1142+
1143+
pa_type = pa.null()
1144+
else:
1145+
pa_type = to_pyarrow_type(base_dtype)
11371146
if pa_type is not None:
11381147
inferred_dtype = ArrowDtype(pa_type)
11391148
elif dtype_backend == "numpy_nullable" and isinstance(inferred_dtype, ArrowDtype):

pandas/tests/series/methods/test_convert_dtypes.py

+8
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,11 @@ def test_convert_dtypes_pyarrow_to_np_nullable(self):
265265
result = ser.convert_dtypes(dtype_backend="numpy_nullable")
266266
expected = pd.Series(range(2), dtype="Int32")
267267
tm.assert_series_equal(result, expected)
268+
269+
def test_convert_dtypes_pyarrow_null(self):
270+
# GH#55346
271+
pa = pytest.importorskip("pyarrow")
272+
ser = pd.Series([None, None])
273+
result = ser.convert_dtypes(dtype_backend="pyarrow")
274+
expected = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
275+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)