From 2594e5fed3b3bedb73c46b1d24416f9f1992deb6 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Sep 2022 14:43:26 +0200 Subject: [PATCH 1/2] BUG: to_timedelta raising for ea float --- doc/source/whatsnew/v1.6.0.rst | 2 +- pandas/core/arrays/timedeltas.py | 7 ++++++- pandas/tests/tools/test_to_timedelta.py | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index 9f793532e5e6b..0c4b5ac88a4ac 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -169,7 +169,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- +- Bug in :func:`to_timedelta` raising error when input has nullable dtype ``Float64`` (:issue:`48796`) - Timezones diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index 75e4ab6b37bf7..4c3e790c2879b 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -45,6 +45,7 @@ from pandas.core.dtypes.common import ( TD64NS_DTYPE, is_dtype_equal, + is_extension_array_dtype, is_float_dtype, is_integer_dtype, is_object_dtype, @@ -909,7 +910,11 @@ def sequence_to_td64ns( elif is_float_dtype(data.dtype): # cast the unit, multiply base/frac separately # to avoid precision issues from float -> int - mask = np.isnan(data) + if is_extension_array_dtype(data): + mask = data._mask + data = data._data + else: + mask = np.isnan(data) # The next few lines are effectively a vectorized 'cast_from_unit' m, p = precision_from_unit(unit or "ns") base = data.astype(np.int64) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index 488e266a1c5c1..16ccd87900a59 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -278,3 +278,10 @@ def test_to_timedelta_zerodim(self, fixed_now_ts): result = to_timedelta(arg2) assert isinstance(result, pd.Timedelta) assert result.value == dt64.view("i8") + + def test_tom_timedelta_float_ea(self, any_numeric_ea_dtype): + # GH#48796 + ser = Series([1, pd.NA], dtype=any_numeric_ea_dtype) + result = to_timedelta(ser) + expected = Series([pd.Timedelta(1, unit="ns"), pd.NaT]) + tm.assert_series_equal(result, expected) From 96a4be181f7f346f0952c401173cd874107d4c2b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 29 Sep 2022 21:27:45 +0200 Subject: [PATCH 2/2] Update pandas/tests/tools/test_to_timedelta.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/tools/test_to_timedelta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index 16ccd87900a59..13d663474aff0 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -279,7 +279,7 @@ def test_to_timedelta_zerodim(self, fixed_now_ts): assert isinstance(result, pd.Timedelta) assert result.value == dt64.view("i8") - def test_tom_timedelta_float_ea(self, any_numeric_ea_dtype): + def test_to_timedelta_numeric_ea(self, any_numeric_ea_dtype): # GH#48796 ser = Series([1, pd.NA], dtype=any_numeric_ea_dtype) result = to_timedelta(ser)