Skip to content

Commit b94cbb5

Browse files
phoflmroeschke
andauthored
BUG: to_timedelta raising for ea float (#48854)
* BUG: to_timedelta raising for ea float * Update pandas/tests/tools/test_to_timedelta.py Co-authored-by: Matthew Roeschke <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7ab6a10 commit b94cbb5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.6.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Datetimelike
171171

172172
Timedelta
173173
^^^^^^^^^
174-
-
174+
- Bug in :func:`to_timedelta` raising error when input has nullable dtype ``Float64`` (:issue:`48796`)
175175
-
176176

177177
Timezones

pandas/core/arrays/timedeltas.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from pandas.core.dtypes.common import (
4646
TD64NS_DTYPE,
4747
is_dtype_equal,
48+
is_extension_array_dtype,
4849
is_float_dtype,
4950
is_integer_dtype,
5051
is_object_dtype,
@@ -909,7 +910,11 @@ def sequence_to_td64ns(
909910
elif is_float_dtype(data.dtype):
910911
# cast the unit, multiply base/frac separately
911912
# to avoid precision issues from float -> int
912-
mask = np.isnan(data)
913+
if is_extension_array_dtype(data):
914+
mask = data._mask
915+
data = data._data
916+
else:
917+
mask = np.isnan(data)
913918
# The next few lines are effectively a vectorized 'cast_from_unit'
914919
m, p = precision_from_unit(unit or "ns")
915920
base = data.astype(np.int64)

pandas/tests/tools/test_to_timedelta.py

+7
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,10 @@ def test_to_timedelta_zerodim(self, fixed_now_ts):
278278
result = to_timedelta(arg2)
279279
assert isinstance(result, pd.Timedelta)
280280
assert result.value == dt64.view("i8")
281+
282+
def test_to_timedelta_numeric_ea(self, any_numeric_ea_dtype):
283+
# GH#48796
284+
ser = Series([1, pd.NA], dtype=any_numeric_ea_dtype)
285+
result = to_timedelta(ser)
286+
expected = Series([pd.Timedelta(1, unit="ns"), pd.NaT])
287+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)