Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
from lxml import etree
import pandas as pd
example_date = "2025-02-05 16:59:57"
default_format = "%Y-%m-%d %H:%M:%S"
xml_node = etree.XML(f"<date>{example_date}</date>")
example_date_from_xml = xml_node.xpath("/date/node()")[0]
assert isinstance(example_date, str)
assert isinstance(example_date_from_xml, str)
assert isinstance(example_date_from_xml, etree._ElementUnicodeResult)
assert not isinstance(example_date, etree._ElementUnicodeResult)
assert example_date_from_xml == example_date
pd.to_datetime(pd.Series([example_date])) # OK
pd.to_datetime(pd.Series([example_date_from_xml])) # OK
pd.to_datetime(pd.Series([example_date_from_xml]), format=default_format) # KO: TypeError: Expected unicode, got lxml.etree._ElementUnicodeResult
# Shorter way of doing this
pd.to_datetime(pd.Series([etree._ElementUnicodeResult(example_date)])) # OK
pd.to_datetime(pd.Series([etree._ElementUnicodeResult(example_date)]), format=default_format) # KO
Issue Description
Hello,
When trying to convert a string that comes from an XML file parsing with pandas.to_datetime
, I struggled with an unexpected TypeError
.
I managed to write a reproducible example that both works on the latest 2.2.3 version and 3.0.0dev
with Python 3.12.8.
It looks like when I'm trying to convert datetimes in a Series initialized from a list of lxml.etree._ElementUnicodeResult
with the argument format
, an error is raised.
Also, using Series.astype(str)
does not work (values are still lxml.etre._ElementUnicodeResult
).
Expected Behavior
No TypeError
.
Installed Versions
3.0.0dev
2.2.3
Comment From: rhshadrach
Thanks for the report! Series.astype(str)
behavior here appears okay to me, lxml.etre._ElementUnicodeResult
is an instance of str
, so I think it makes sense this is a no-op. If you really want to apply str
to values, you can use Series.map(str)
. This provides a workaround for this issue.
The error occurs when passing this string to string_to_dts
here:
https://github.com/pandas-dev/pandas/blob/6bcd30397d67c3887288c7a82c2c235ce8bc3c7f/pandas/_libs/tslibs/np_datetime.pyx#L341
Wrapping the argument of the caller here:
https://github.com/pandas-dev/pandas/blob/6bcd30397d67c3887288c7a82c2c235ce8bc3c7f/pandas/_libs/tslibs/strptime.pyx#L454
with str(val)
gives the expected result. However I'm not sure if we should need to be doing this - is Cython's str
not able to handle subclasses?
Further investigations welcome!
Comment From: rhshadrach
Looks like this is indeed expected behavior in Cython: https://github.com/cython/cython/issues/3256
We could either wrap val
as str(val)
as mention above or change the type-hints in the Cython to use Union[str]
. I worry about performance impact of each on typical usage in order to support proper subclasses which I suspect are more rare. If the performance impact is small, I'd be okay with supporting subclasses.