From b6e5195b4dc96d260e46b9ae9911139183d680d0 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Fri, 28 Apr 2023 17:50:36 +0200 Subject: [PATCH 1/4] BUG fixing timedelta --- pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 563c2505a1812..9f4dd6f16fb65 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 From b1231539b5b86185cfb48556769ff8142b453463 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Mon, 1 May 2023 19:44:20 +0200 Subject: [PATCH 2/4] BUG timedelta raising w pd.NA --- doc/source/whatsnew/v2.0.2.rst | 3 ++- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/tools/test_to_timedelta.py | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.0.2.rst b/doc/source/whatsnew/v2.0.2.rst index adfebd857b390..15e7bb023601e 100644 --- a/doc/source/whatsnew/v2.0.2.rst +++ b/doc/source/whatsnew/v2.0.2.rst @@ -22,9 +22,10 @@ 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:`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 9f4dd6f16fb65..518a79c7c281a 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -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/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index b1ab449996685..3704f96ce503d 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -214,11 +214,15 @@ 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") + def test_to_timedelta_on_missing_values_list(self): + actual = to_timedelta([pd.NA]) + 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:] From 8fe3270dc9f62179b31b25ce1b05b41b96db216f Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Tue, 2 May 2023 11:25:49 +0200 Subject: [PATCH 3/4] BUG removed not needed fail on test .. ? --- pandas/tests/series/methods/test_astype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" ) From 33676898fccdcd4d42a7effa11ae5255c3e86489 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Tue, 2 May 2023 18:08:45 +0200 Subject: [PATCH 4/4] BUG Parametrised test --- pandas/tests/tools/test_to_timedelta.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index 3704f96ce503d..92db9af275340 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -219,8 +219,9 @@ def test_to_timedelta_on_missing_values_scalar(self, val): actual = to_timedelta(val) assert actual._value == np.timedelta64("NaT").astype("int64") - def test_to_timedelta_on_missing_values_list(self): - actual = to_timedelta([pd.NA]) + @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):