Skip to content

Commit a5cbd1e

Browse files
authored
REGR: to_datetime with non-ISO format, float, and nan fails on main (#50238)
check for nan/nat in floats Co-authored-by: MarcoGorelli <>
1 parent bf5ee72 commit a5cbd1e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Enforced reversion of ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` in function :meth:`DataFrame.plot.scatter` (:issue:`49732`)
2020
- Fixed regression in :meth:`SeriesGroupBy.apply` setting a ``name`` attribute on the result if the result was a :class:`DataFrame` (:issue:`49907`)
2121
- Fixed performance regression in setting with the :meth:`~DataFrame.at` indexer (:issue:`49771`)
22+
- Fixed regression in :func:`to_datetime` raising ``ValueError`` when parsing array of ``float`` containing ``np.nan`` (:issue:`50237`)
2223
-
2324

2425
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/strptime.pyx

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ from pandas._libs.tslibs.np_datetime cimport (
4242
pydatetime_to_dt64,
4343
)
4444
from pandas._libs.tslibs.timestamps cimport _Timestamp
45-
from pandas._libs.util cimport is_datetime64_object
45+
from pandas._libs.util cimport (
46+
is_datetime64_object,
47+
is_float_object,
48+
is_integer_object,
49+
)
4650

4751
cnp.import_array()
4852

@@ -185,6 +189,12 @@ def array_strptime(
185189
elif is_datetime64_object(val):
186190
iresult[i] = get_datetime64_nanos(val, NPY_FR_ns)
187191
continue
192+
elif (
193+
(is_integer_object(val) or is_float_object(val))
194+
and (val != val or val == NPY_NAT)
195+
):
196+
iresult[i] = NPY_NAT
197+
continue
188198
else:
189199
val = str(val)
190200

pandas/tests/tools/test_to_datetime.py

+11
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ def test_to_datetime_format_YYYYMMDD_with_nat(self, cache):
135135
result = to_datetime(ser2, format="%Y%m%d", cache=cache)
136136
tm.assert_series_equal(result, expected)
137137

138+
def test_to_datetime_format_YYYYMM_with_nat(self, cache):
139+
# https://github.com/pandas-dev/pandas/issues/50237
140+
ser = Series([198012, 198012] + [198101] * 5)
141+
expected = Series(
142+
[Timestamp("19801201"), Timestamp("19801201")] + [Timestamp("19810101")] * 5
143+
)
144+
expected[2] = np.nan
145+
ser[2] = np.nan
146+
result = to_datetime(ser, format="%Y%m", cache=cache)
147+
tm.assert_series_equal(result, expected)
148+
138149
def test_to_datetime_format_YYYYMMDD_ignore(self, cache):
139150
# coercion
140151
# GH 7930

0 commit comments

Comments
 (0)