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..13d663474aff0 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_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) + expected = Series([pd.Timedelta(1, unit="ns"), pd.NaT]) + tm.assert_series_equal(result, expected)