-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Do not fail when parsing pydatetime objects in pd.to_datetime #49298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @sv1990 Thanks for your report - it might make sense to skip Otherwise
|
Seems like the exception comes from pandas/pandas/core/tools/datetimes.py Lines 1070 to 1079 in bbb1cdf
and then from pandas/pandas/core/tools/datetimes.py Lines 431 to 436 in bbb1cdf
which uses pandas/pandas/core/tools/datetimes.py Lines 539 to 542 in bbb1cdf
, a wrapper around pandas/pandas/core/tools/datetimes.py Lines 474 to 475 in bbb1cdf
From there, |
Ok, this change seems to work: diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx
index 6287c2fbc5..20452bf75e 100644
--- a/pandas/_libs/tslibs/strptime.pyx
+++ b/pandas/_libs/tslibs/strptime.pyx
@@ -2,6 +2,7 @@
"""
from cpython.datetime cimport (
date,
+ datetime,
tzinfo,
)
@@ -129,6 +130,19 @@ def array_strptime(ndarray[object] values, str fmt, bint exact=True, errors='rai
if val in nat_strings:
iresult[i] = NPY_NAT
continue
+ elif isinstance(val, datetime):
+ dts.year = val.year
+ dts.month = val.month
+ dts.day = val.day
+ dts.hour = val.hour
+ dts.min = val.minute
+ dts.sec = val.second
+ dts.us = val.microsecond
+ dts.ps = 0 # Not enough precision in datetime objects (https://github.com/python/cpython/issues/59648)
+
+ iresult[i] = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts)
+ result_timezone[i] = val.tzname()
+ continue
else:
if checknull_with_nat_and_na(val):
iresult[i] = NPY_NAT
I just replicated the processing applied in the same function a couple of lines below: pandas/pandas/_libs/tslibs/strptime.pyx Lines 324 to 342 in 953921f
I'll prepare a PR and proper tests if that's ok, so we can move discuss about implementation there. |
Thanks for looking into this - if it's the same, can it be factored out into a function? |
datetime.datetime
objects in pd.to_datetime
datetime.datetime
objects in pd.to_datetime
datetime.datetime
objects in pd.to_datetime
Uh oh!
There was an error while loading. Please reload this page.
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
When reading excel files I often get date columns looking like
When trying to parse those columns using
the Exception
ValueError("time data '2001-02-02 00:00:00' does not match format '%d/%m/%y' (match)")
is raised.The origin of this issue is that the already parsed
datetime
is converted to a string in isoformat and then an attempt to parse it in the given format is made.Using
pd.to_datetime
without a format leads to wrong results since the format is ambiguous.Feature Description
pd.to_datetime
should either get an option to handledatetime.datetime
objects differently or do so by default.Alternative Solutions
The only alternative solution I could think off currently is a raw loop and checking the type of each element individually.
Additional Context
No response
The text was updated successfully, but these errors were encountered: