Skip to content

Commit b122f80

Browse files
mroeschkephofl
andauthored
BUG: to_timedelta/datetime with numeric ArrowExtensionArray (#52473)
* BUG: to_timedelta/datetime with numeric ArrowExtensionArray * Test compat --------- Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 166598f commit b122f80

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
@@ -2209,10 +2210,14 @@ def ensure_arraylike_for_datetimelike(data, copy: bool, cls_name: str):
22092210
else:
22102211
data = extract_array(data, extract_numpy=True)
22112212

2212-
if isinstance(data, IntegerArray):
2213+
if isinstance(data, IntegerArray) or (
2214+
isinstance(data, ArrowExtensionArray) and data.dtype.kind in "iu"
2215+
):
22132216
data = data.to_numpy("int64", na_value=iNaT)
22142217
copy = False
2215-
elif not isinstance(data, (np.ndarray, ExtensionArray)):
2218+
elif not isinstance(data, (np.ndarray, ExtensionArray)) or isinstance(
2219+
data, ArrowExtensionArray
2220+
):
22162221
# GH#24539 e.g. xarray, dask object
22172222
data = np.asarray(data)
22182223

pandas/tests/tools/test_to_datetime.py

+9
Original file line numberDiff line numberDiff line change
@@ -3586,3 +3586,12 @@ def test_ignoring_unknown_tz_deprecated():
35863586
with tm.assert_produces_warning(FutureWarning):
35873587
res = to_datetime([dtstr])
35883588
tm.assert_index_equal(res, to_datetime([dtstr[:-5]]))
3589+
3590+
3591+
def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
3592+
# GH 52425
3593+
pytest.importorskip("pyarrow")
3594+
ser = Series([1, 2], dtype=f"{any_numeric_ea_dtype.lower()}[pyarrow]")
3595+
result = to_datetime(ser)
3596+
expected = Series([1, 2], dtype="datetime64[ns]")
3597+
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)