Skip to content

Commit b04980a

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#53651: BUG: convert_dtype(dtype_backend=nullable_numpy) with ArrowDtype
1 parent 71c9a17 commit b04980a

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

doc/source/whatsnew/v2.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121

2222
Bug fixes
2323
~~~~~~~~~
24+
- Bug in :func:`DataFrame.convert_dtype` and :func:`Series.convert_dtype` when trying to convert :class:`ArrowDtype` with ``dtype_backend="nullable_numpy"`` (:issue:`53648`)
2425
- Bug in :func:`RangeIndex.union` when using ``sort=True`` with another :class:`RangeIndex` (:issue:`53490`)
2526
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
2627
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)

pandas/core/dtypes/cast.py

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
notna,
9797
)
9898

99+
from pandas.io._util import _arrow_dtype_mapping
100+
99101
if TYPE_CHECKING:
100102
from pandas import Index
101103
from pandas.core.arrays import (
@@ -1147,6 +1149,9 @@ def convert_dtypes(
11471149
pa_type = to_pyarrow_type(base_dtype)
11481150
if pa_type is not None:
11491151
inferred_dtype = ArrowDtype(pa_type)
1152+
elif dtype_backend == "numpy_nullable" and isinstance(inferred_dtype, ArrowDtype):
1153+
# GH 53648
1154+
inferred_dtype = _arrow_dtype_mapping()[inferred_dtype.pyarrow_dtype]
11501155

11511156
# error: Incompatible return value type (got "Union[str, Union[dtype[Any],
11521157
# ExtensionDtype]]", expected "Union[dtype[Any], ExtensionDtype]")

pandas/tests/frame/methods/test_convert_dtypes.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_pyarrow_engine_lines_false(self):
146146
with pytest.raises(ValueError, match=msg):
147147
df.convert_dtypes(dtype_backend="numpy")
148148

149-
def test_pyarrow_backend_no_convesion(self):
149+
def test_pyarrow_backend_no_conversion(self):
150150
# GH#52872
151151
pytest.importorskip("pyarrow")
152152
df = pd.DataFrame({"a": [1, 2], "b": 1.5, "c": True, "d": "x"})
@@ -159,3 +159,11 @@ def test_pyarrow_backend_no_convesion(self):
159159
dtype_backend="pyarrow",
160160
)
161161
tm.assert_frame_equal(result, expected)
162+
163+
def test_convert_dtypes_pyarrow_to_np_nullable(self):
164+
# GH 53648
165+
pytest.importorskip("pyarrow")
166+
ser = pd.DataFrame(range(2), dtype="int32[pyarrow]")
167+
result = ser.convert_dtypes(dtype_backend="numpy_nullable")
168+
expected = pd.DataFrame(range(2), dtype="Int32")
169+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_convert_dtypes.py

+8
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,11 @@ def test_convert_dtype_object_with_na_float(self, infer_objects, dtype):
240240
result = ser.convert_dtypes(infer_objects=infer_objects)
241241
expected = pd.Series([1.5, pd.NA], dtype=dtype)
242242
tm.assert_series_equal(result, expected)
243+
244+
def test_convert_dtypes_pyarrow_to_np_nullable(self):
245+
# GH 53648
246+
pytest.importorskip("pyarrow")
247+
ser = pd.Series(range(2), dtype="int32[pyarrow]")
248+
result = ser.convert_dtypes(dtype_backend="numpy_nullable")
249+
expected = pd.Series(range(2), dtype="Int32")
250+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)