Skip to content

Commit 49c020a

Browse files
authored
Backport PR #52473: BUG: to_timedelta/datetime with numeric ArrowExtnsionArray (#52507)
Backport PR #52473: BUG: to_timedelta/datetime with numeric ArrowExtensionArray
1 parent 84423d8 commit 49c020a

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
Bug fixes
2222
~~~~~~~~~
2323
- Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`)
24+
- Bug in :func:`to_datetime` and :func:`to_timedelta` when trying to convert numeric data with a :class:`ArrowDtype` (:issue:`52425`)
2425
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
2526
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
2627

pandas/core/arrays/datetimelike.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
NDArrayBackedExtensionArray,
128128
ravel_compat,
129129
)
130+
from pandas.core.arrays.arrow.array import ArrowExtensionArray
130131
from pandas.core.arrays.base import ExtensionArray
131132
from pandas.core.arrays.integer import IntegerArray
132133
import pandas.core.common as com
@@ -2118,10 +2119,14 @@ def ensure_arraylike_for_datetimelike(data, copy: bool, cls_name: str):
21182119
else:
21192120
data = extract_array(data, extract_numpy=True)
21202121

2121-
if isinstance(data, IntegerArray):
2122+
if isinstance(data, IntegerArray) or (
2123+
isinstance(data, ArrowExtensionArray) and data.dtype.kind in "iu"
2124+
):
21222125
data = data.to_numpy("int64", na_value=iNaT)
21232126
copy = False
2124-
elif not isinstance(data, (np.ndarray, ExtensionArray)):
2127+
elif not isinstance(data, (np.ndarray, ExtensionArray)) or isinstance(
2128+
data, ArrowExtensionArray
2129+
):
21252130
# GH#24539 e.g. xarray, dask object
21262131
data = np.asarray(data)
21272132

pandas/tests/tools/test_to_datetime.py

+9
Original file line numberDiff line numberDiff line change
@@ -3570,3 +3570,12 @@ def test_to_datetime_mixed_not_necessarily_iso8601_coerce(errors, expected):
35703570
# https://github.com/pandas-dev/pandas/issues/50411
35713571
result = to_datetime(["2020-01-01", "01-01-2000"], format="ISO8601", errors=errors)
35723572
tm.assert_index_equal(result, expected)
3573+
3574+
3575+
def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
3576+
# GH 52425
3577+
pytest.importorskip("pyarrow")
3578+
ser = Series([1, 2], dtype=f"{any_numeric_ea_dtype.lower()}[pyarrow]")
3579+
result = to_datetime(ser)
3580+
expected = Series([1, 2], dtype="datetime64[ns]")
3581+
tm.assert_series_equal(result, expected)

pandas/tests/tools/test_to_timedelta.py

+9
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,12 @@ def test_to_timedelta_numeric_ea(self, any_numeric_ea_dtype):
287287
result = to_timedelta(ser)
288288
expected = Series([pd.Timedelta(1, unit="ns"), pd.NaT])
289289
tm.assert_series_equal(result, expected)
290+
291+
292+
def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
293+
# GH 52425
294+
pytest.importorskip("pyarrow")
295+
ser = Series([1, 2], dtype=f"{any_numeric_ea_dtype.lower()}[pyarrow]")
296+
result = to_timedelta(ser)
297+
expected = Series([1, 2], dtype="timedelta64[ns]")
298+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)