Skip to content

Commit ba74fee

Browse files
authored
BUG: datetime.date objects don't get parsed for non-ISO formats (pandas-dev#50109)
* parse datetime.date in non-ISO path * reword Co-authored-by: MarcoGorelli <>
1 parent eb23512 commit ba74fee

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ Datetimelike
703703
- Bug in subtracting a ``datetime`` scalar from :class:`DatetimeIndex` failing to retain the original ``freq`` attribute (:issue:`48818`)
704704
- Bug in ``pandas.tseries.holiday.Holiday`` where a half-open date interval causes inconsistent return types from :meth:`USFederalHolidayCalendar.holidays` (:issue:`49075`)
705705
- Bug in rendering :class:`DatetimeIndex` and :class:`Series` and :class:`DataFrame` with timezone-aware dtypes with ``dateutil`` or ``zoneinfo`` timezones near daylight-savings transitions (:issue:`49684`)
706-
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing :class:`Timestamp`, ``datetime``, or ``np.datetime64`` objects with non-ISO8601 ``format`` (:issue:`49298`, :issue:`50036`)
706+
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing :class:`Timestamp`, ``datetime.datetime``, ``datetime.date``, or ``np.datetime64`` objects when non-ISO8601 ``format`` was passed (:issue:`49298`, :issue:`50036`)
707707
-
708708

709709
Timedelta

pandas/_libs/tslibs/strptime.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Strptime-related classes and functions.
22
"""
33
from cpython.datetime cimport (
4+
PyDate_Check,
45
PyDateTime_Check,
56
date,
67
import_datetime,
@@ -34,6 +35,7 @@ from pandas._libs.tslibs.np_datetime cimport (
3435
check_dts_bounds,
3536
npy_datetimestruct,
3637
npy_datetimestruct_to_datetime,
38+
pydate_to_dt64,
3739
pydatetime_to_dt64,
3840
)
3941
from pandas._libs.tslibs.timestamps cimport _Timestamp
@@ -173,6 +175,10 @@ def array_strptime(
173175
check_dts_bounds(&dts)
174176
result_timezone[i] = val.tzinfo
175177
continue
178+
elif PyDate_Check(val):
179+
iresult[i] = pydate_to_dt64(val, &dts)
180+
check_dts_bounds(&dts)
181+
continue
176182
elif is_datetime64_object(val):
177183
iresult[i] = get_datetime64_nanos(val, NPY_FR_ns)
178184
continue

pandas/tests/tools/test_to_datetime.py

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import calendar
44
from collections import deque
55
from datetime import (
6+
date,
67
datetime,
78
timedelta,
89
timezone,
@@ -469,6 +470,16 @@ def test_to_datetime_mixed_datetime_and_string(self):
469470
expected = to_datetime([d1, d2]).tz_convert(pytz.FixedOffset(-60))
470471
tm.assert_index_equal(res, expected)
471472

473+
@pytest.mark.parametrize(
474+
"format", ["%Y-%m-%d", "%Y-%d-%m"], ids=["ISO8601", "non-ISO8601"]
475+
)
476+
def test_to_datetime_mixed_date_and_string(self, format):
477+
# https://github.com/pandas-dev/pandas/issues/50108
478+
d1 = date(2020, 1, 2)
479+
res = to_datetime(["2020-01-01", d1], format=format)
480+
expected = DatetimeIndex(["2020-01-01", "2020-01-02"])
481+
tm.assert_index_equal(res, expected)
482+
472483
@pytest.mark.parametrize(
473484
"fmt",
474485
["%Y-%d-%m %H:%M:%S%z", "%Y-%m-%d %H:%M:%S%z"],

0 commit comments

Comments
 (0)