Skip to content

BUG: to_timedelta/datetime with numeric ArrowExtensionArray #52473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :func:`to_datetime` and :func:`to_timedelta` when trying to convert numeric data with a :class:`ArrowDtype` (:issue:`52425`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
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 @@ -2209,10 +2210,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 @@ -3586,3 +3586,12 @@ def test_ignoring_unknown_tz_deprecated():
with tm.assert_produces_warning(FutureWarning):
res = to_datetime([dtstr])
tm.assert_index_equal(res, to_datetime([dtstr[:-5]]))


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)