diff --git a/doc/source/whatsnew/v2.0.2.rst b/doc/source/whatsnew/v2.0.2.rst index 8322c8408a0e3..b6c6df8424ef1 100644 --- a/doc/source/whatsnew/v2.0.2.rst +++ b/doc/source/whatsnew/v2.0.2.rst @@ -22,10 +22,11 @@ Bug fixes ~~~~~~~~~ - Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`) - Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`) +- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`) - Bug in :meth:`DataFrame.convert_dtypes` ignores ``convert_*`` keywords when set to False ``dtype_backend="pyarrow"`` (:issue:`52872`) - Bug in :meth:`Series.describe` treating pyarrow-backed timestamps and timedeltas as categorical data (:issue:`53001`) - Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`) -- + .. --------------------------------------------------------------------------- .. _whatsnew_202.other: diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 563c2505a1812..518a79c7c281a 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -34,6 +34,7 @@ import_datetime() cimport pandas._libs.tslibs.util as util +from pandas._libs.missing cimport checknull_with_nat_and_na from pandas._libs.tslibs.base cimport ABCTimestamp from pandas._libs.tslibs.conversion cimport ( cast_from_unit, @@ -341,8 +342,7 @@ cdef convert_to_timedelta64(object ts, str unit): Return an ns based int64 """ # Caller is responsible for checking unit not in ["Y", "y", "M"] - - if checknull_with_nat(ts): + if checknull_with_nat_and_na(ts): return np.timedelta64(NPY_NAT, "ns") elif isinstance(ts, _Timedelta): # already in the proper format @@ -1808,7 +1808,7 @@ class Timedelta(_Timedelta): # unit=None is de-facto 'ns' unit = parse_timedelta_unit(unit) value = convert_to_timedelta64(value, unit) - elif checknull_with_nat(value): + elif checknull_with_nat_and_na(value): return NaT else: raise ValueError( diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 71ce8541de24b..d72c8599dfe5e 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -471,7 +471,7 @@ class TestAstypeString: def test_astype_string_to_extension_dtype_roundtrip( self, data, dtype, request, nullable_string_dtype ): - if dtype == "boolean" or (dtype == "timedelta64[ns]" and NaT in data): + if dtype == "boolean": mark = pytest.mark.xfail( reason="TODO StringArray.astype() with missing values #GH40566" ) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index b1ab449996685..92db9af275340 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -214,11 +214,16 @@ def test_to_timedelta_on_missing_values(self): actual = to_timedelta(ser) tm.assert_series_equal(actual, expected) - @pytest.mark.parametrize("val", [np.nan, pd.NaT]) + @pytest.mark.parametrize("val", [np.nan, pd.NaT, pd.NA]) def test_to_timedelta_on_missing_values_scalar(self, val): actual = to_timedelta(val) assert actual._value == np.timedelta64("NaT").astype("int64") + @pytest.mark.parametrize("val", [np.nan, pd.NaT, pd.NA]) + def test_to_timedelta_on_missing_values_list(self, val): + actual = to_timedelta([val]) + assert actual[0]._value == np.timedelta64("NaT").astype("int64") + def test_to_timedelta_float(self): # https://github.com/pandas-dev/pandas/issues/25077 arr = np.arange(0, 1, 1e-6)[-10:]