Skip to content

Commit a43d97d

Browse files
DeaMariaLeontopper-123
authored andcommitted
BUG to_timedelta was raising ValueError w pd.NA (pandas-dev#53022)
* BUG fixing timedelta * BUG timedelta raising w pd.NA * BUG removed not needed fail on test .. ? * BUG Parametrised test
1 parent 19d8614 commit a43d97d

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

doc/source/whatsnew/v2.0.2.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Bug fixes
2222
~~~~~~~~~
2323
- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`)
2424
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
25+
- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`)
2526
- Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`)
2627
- Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`)
2728
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)
28-
-
29+
2930

3031
.. ---------------------------------------------------------------------------
3132
.. _whatsnew_202.other:

pandas/_libs/tslibs/timedeltas.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import_datetime()
3434

3535

3636
cimport pandas._libs.tslibs.util as util
37+
from pandas._libs.missing cimport checknull_with_nat_and_na
3738
from pandas._libs.tslibs.base cimport ABCTimestamp
3839
from pandas._libs.tslibs.conversion cimport (
3940
cast_from_unit,
@@ -341,8 +342,7 @@ cdef convert_to_timedelta64(object ts, str unit):
341342
Return an ns based int64
342343
"""
343344
# Caller is responsible for checking unit not in ["Y", "y", "M"]
344-
345-
if checknull_with_nat(ts):
345+
if checknull_with_nat_and_na(ts):
346346
return np.timedelta64(NPY_NAT, "ns")
347347
elif isinstance(ts, _Timedelta):
348348
# already in the proper format
@@ -1808,7 +1808,7 @@ class Timedelta(_Timedelta):
18081808
# unit=None is de-facto 'ns'
18091809
unit = parse_timedelta_unit(unit)
18101810
value = convert_to_timedelta64(value, unit)
1811-
elif checknull_with_nat(value):
1811+
elif checknull_with_nat_and_na(value):
18121812
return NaT
18131813
else:
18141814
raise ValueError(

pandas/tests/series/methods/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class TestAstypeString:
471471
def test_astype_string_to_extension_dtype_roundtrip(
472472
self, data, dtype, request, nullable_string_dtype
473473
):
474-
if dtype == "boolean" or (dtype == "timedelta64[ns]" and NaT in data):
474+
if dtype == "boolean":
475475
mark = pytest.mark.xfail(
476476
reason="TODO StringArray.astype() with missing values #GH40566"
477477
)

pandas/tests/tools/test_to_timedelta.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,16 @@ def test_to_timedelta_on_missing_values(self):
214214
actual = to_timedelta(ser)
215215
tm.assert_series_equal(actual, expected)
216216

217-
@pytest.mark.parametrize("val", [np.nan, pd.NaT])
217+
@pytest.mark.parametrize("val", [np.nan, pd.NaT, pd.NA])
218218
def test_to_timedelta_on_missing_values_scalar(self, val):
219219
actual = to_timedelta(val)
220220
assert actual._value == np.timedelta64("NaT").astype("int64")
221221

222+
@pytest.mark.parametrize("val", [np.nan, pd.NaT, pd.NA])
223+
def test_to_timedelta_on_missing_values_list(self, val):
224+
actual = to_timedelta([val])
225+
assert actual[0]._value == np.timedelta64("NaT").astype("int64")
226+
222227
def test_to_timedelta_float(self):
223228
# https://github.com/pandas-dev/pandas/issues/25077
224229
arr = np.arange(0, 1, 1e-6)[-10:]

0 commit comments

Comments
 (0)