From e8e77c781e68d05896e852007d17c12112931a33 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Tue, 13 Dec 2022 18:05:59 +0000 Subject: [PATCH] check for nan/nat in floats --- doc/source/whatsnew/v1.5.3.rst | 1 + pandas/_libs/tslibs/strptime.pyx | 12 +++++++++++- pandas/tests/tools/test_to_datetime.py | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index 581d28e10bd67..c0df15b67a4a7 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -19,6 +19,7 @@ Fixed regressions - Enforced reversion of ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` in function :meth:`DataFrame.plot.scatter` (:issue:`49732`) - Fixed regression in :meth:`SeriesGroupBy.apply` setting a ``name`` attribute on the result if the result was a :class:`DataFrame` (:issue:`49907`) - Fixed performance regression in setting with the :meth:`~DataFrame.at` indexer (:issue:`49771`) +- Fixed regression in :func:`to_datetime` raising ``ValueError`` when parsing array of ``float`` containing ``np.nan`` (:issue:`50237`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index 3736b21a85611..760364d1b86b4 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -42,7 +42,11 @@ from pandas._libs.tslibs.np_datetime cimport ( pydatetime_to_dt64, ) from pandas._libs.tslibs.timestamps cimport _Timestamp -from pandas._libs.util cimport is_datetime64_object +from pandas._libs.util cimport ( + is_datetime64_object, + is_float_object, + is_integer_object, +) cnp.import_array() @@ -185,6 +189,12 @@ def array_strptime( elif is_datetime64_object(val): iresult[i] = get_datetime64_nanos(val, NPY_FR_ns) continue + elif ( + (is_integer_object(val) or is_float_object(val)) + and (val != val or val == NPY_NAT) + ): + iresult[i] = NPY_NAT + continue else: val = str(val) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 48844beed30f4..c4c60b6c4231e 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -135,6 +135,17 @@ def test_to_datetime_format_YYYYMMDD_with_nat(self, cache): result = to_datetime(ser2, format="%Y%m%d", cache=cache) tm.assert_series_equal(result, expected) + def test_to_datetime_format_YYYYMM_with_nat(self, cache): + # https://github.com/pandas-dev/pandas/issues/50237 + ser = Series([198012, 198012] + [198101] * 5) + expected = Series( + [Timestamp("19801201"), Timestamp("19801201")] + [Timestamp("19810101")] * 5 + ) + expected[2] = np.nan + ser[2] = np.nan + result = to_datetime(ser, format="%Y%m", cache=cache) + tm.assert_series_equal(result, expected) + def test_to_datetime_format_YYYYMMDD_ignore(self, cache): # coercion # GH 7930