Skip to content

Commit 74f1e94

Browse files
authored
BUG: to_datetime fails with np.datetime64 and non-ISO format (#50038)
Co-authored-by: MarcoGorelli <>
1 parent 1d5ce5b commit 74f1e94

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ Datetimelike
658658
- Bug in subtracting a ``datetime`` scalar from :class:`DatetimeIndex` failing to retain the original ``freq`` attribute (:issue:`48818`)
659659
- Bug in ``pandas.tseries.holiday.Holiday`` where a half-open date interval causes inconsistent return types from :meth:`USFederalHolidayCalendar.holidays` (:issue:`49075`)
660660
- 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`)
661-
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing :class:`Timestamp` or ``datetime`` objects with non-ISO8601 ``format`` (:issue:`49298`)
661+
- 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`)
662662
-
663663

664664
Timedelta

pandas/_libs/tslibs/strptime.pyx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ from _thread import allocate_lock as _thread_allocate_lock
1414
import numpy as np
1515
import pytz
1616

17+
cimport numpy as cnp
1718
from numpy cimport (
1819
int64_t,
1920
ndarray,
2021
)
2122

2223
from pandas._libs.missing cimport checknull_with_nat_and_na
23-
from pandas._libs.tslibs.conversion cimport convert_timezone
24+
from pandas._libs.tslibs.conversion cimport (
25+
convert_timezone,
26+
get_datetime64_nanos,
27+
)
2428
from pandas._libs.tslibs.nattype cimport (
2529
NPY_NAT,
2630
c_nat_strings as nat_strings,
@@ -33,6 +37,9 @@ from pandas._libs.tslibs.np_datetime cimport (
3337
pydatetime_to_dt64,
3438
)
3539
from pandas._libs.tslibs.timestamps cimport _Timestamp
40+
from pandas._libs.util cimport is_datetime64_object
41+
42+
cnp.import_array()
3643

3744

3845
cdef dict _parse_code_table = {"y": 0,
@@ -166,6 +173,9 @@ def array_strptime(
166173
check_dts_bounds(&dts)
167174
result_timezone[i] = val.tzinfo
168175
continue
176+
elif is_datetime64_object(val):
177+
iresult[i] = get_datetime64_nanos(val, NPY_FR_ns)
178+
continue
169179
else:
170180
val = str(val)
171181

pandas/tests/tools/test_to_datetime.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,19 @@ def test_to_datetime_today_now_unicode_bytes(self, arg):
758758
def test_to_datetime_dt64s(self, cache, dt):
759759
assert to_datetime(dt, cache=cache) == Timestamp(dt)
760760

761+
@pytest.mark.parametrize(
762+
"arg, format",
763+
[
764+
("2001-01-01", "%Y-%m-%d"),
765+
("01-01-2001", "%d-%m-%Y"),
766+
],
767+
)
768+
def test_to_datetime_dt64s_and_str(self, arg, format):
769+
# https://github.com/pandas-dev/pandas/issues/50036
770+
result = to_datetime([arg, np.datetime64("2020-01-01")], format=format)
771+
expected = DatetimeIndex(["2001-01-01", "2020-01-01"])
772+
tm.assert_index_equal(result, expected)
773+
761774
@pytest.mark.parametrize(
762775
"dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")]
763776
)

0 commit comments

Comments
 (0)