Skip to content

Commit

Permalink
Backport PR #52473: BUG: to_timedelta/datetime with numeric ArrowExtn…
Browse files Browse the repository at this point in the history
…sionArray (#52507)

Backport PR #52473: BUG: to_timedelta/datetime with numeric ArrowExtensionArray
  • Loading branch information
mroeschke committed Apr 7, 2023
1 parent 84423d8 commit 49c020a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`)
- Bug in :func:`to_datetime` and :func:`to_timedelta` when trying to convert numeric data with a :class:`ArrowDtype` (:issue:`52425`)
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)

Expand Down
9 changes: 7 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
NDArrayBackedExtensionArray,
ravel_compat,
)
from pandas.core.arrays.arrow.array import ArrowExtensionArray
from pandas.core.arrays.base import ExtensionArray
from pandas.core.arrays.integer import IntegerArray
import pandas.core.common as com
Expand Down Expand Up @@ -2118,10 +2119,14 @@ def ensure_arraylike_for_datetimelike(data, copy: bool, cls_name: str):
else:
data = extract_array(data, extract_numpy=True)

if isinstance(data, IntegerArray):
if isinstance(data, IntegerArray) or (
isinstance(data, ArrowExtensionArray) and data.dtype.kind in "iu"
):
data = data.to_numpy("int64", na_value=iNaT)
copy = False
elif not isinstance(data, (np.ndarray, ExtensionArray)):
elif not isinstance(data, (np.ndarray, ExtensionArray)) or isinstance(
data, ArrowExtensionArray
):
# GH#24539 e.g. xarray, dask object
data = np.asarray(data)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3570,3 +3570,12 @@ def test_to_datetime_mixed_not_necessarily_iso8601_coerce(errors, expected):
# https://github.com/pandas-dev/pandas/issues/50411
result = to_datetime(["2020-01-01", "01-01-2000"], format="ISO8601", errors=errors)
tm.assert_index_equal(result, expected)


def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
# GH 52425
pytest.importorskip("pyarrow")
ser = Series([1, 2], dtype=f"{any_numeric_ea_dtype.lower()}[pyarrow]")
result = to_datetime(ser)
expected = Series([1, 2], dtype="datetime64[ns]")
tm.assert_series_equal(result, expected)
9 changes: 9 additions & 0 deletions pandas/tests/tools/test_to_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,12 @@ def test_to_timedelta_numeric_ea(self, any_numeric_ea_dtype):
result = to_timedelta(ser)
expected = Series([pd.Timedelta(1, unit="ns"), pd.NaT])
tm.assert_series_equal(result, expected)


def test_from_numeric_arrow_dtype(any_numeric_ea_dtype):
# GH 52425
pytest.importorskip("pyarrow")
ser = Series([1, 2], dtype=f"{any_numeric_ea_dtype.lower()}[pyarrow]")
result = to_timedelta(ser)
expected = Series([1, 2], dtype="timedelta64[ns]")
tm.assert_series_equal(result, expected)

0 comments on commit 49c020a

Please sign in to comment.