Skip to content

Commit b063771

Browse files
MarcoGorelliMarcoGorelli
authored and
MarcoGorelli
committed
Backport PR pandas-dev#50238: REGR: to_datetime with non-ISO format, float, and nan fails on main
1 parent f9566aa commit b063771

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
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

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ from pandas._libs.tslibs.np_datetime cimport (
3030
dtstruct_to_dt64,
3131
npy_datetimestruct,
3232
)
33+
from pandas._libs.util cimport (
34+
is_float_object,
35+
is_integer_object,
36+
)
3337

3438

3539
cdef dict _parse_code_table = {'y': 0,
@@ -133,6 +137,12 @@ def array_strptime(ndarray[object] values, str fmt, bint exact=True, errors='rai
133137
if val in nat_strings:
134138
iresult[i] = NPY_NAT
135139
continue
140+
elif (
141+
(is_integer_object(val) or is_float_object(val))
142+
and (val != val or val == NPY_NAT)
143+
):
144+
iresult[i] = NPY_NAT
145+
continue
136146
else:
137147
if checknull_with_nat_and_na(val):
138148
iresult[i] = NPY_NAT

pandas/tests/tools/test_to_datetime.py

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

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

0 commit comments

Comments
 (0)