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

INSTALLED VERSIONS ------------------ commit : 19ea997815d4dadf490d7052a0a3c289be898588 python : 3.12.8 python-bits : 64 OS : Linux OS-release : 5.15.146.1-microsoft-standard-WSL2 Version : #1 SMP Thu Jan 11 04:09:03 UTC 2024 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : C.UTF-8 LOCALE : C.UTF-8 pandas : 3.0.0.dev0+1943.g19ea997815 numpy : 2.3.0.dev0+git20250211.bbfb823 dateutil : 2.9.0.post0 pip : None Cython : None sphinx : None IPython : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : None lxml.etree : 5.3.1 matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : None python-calamine : None pytz : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlsxwriter : None zstandard : None tzdata : 2025.1 qtpy : None pyqt5 : None

2.2.3

INSTALLED VERSIONS ------------------ commit : 0691c5cf90477d3503834d983f69350f250a6ff7 python : 3.12.8 python-bits : 64 OS : Linux OS-release : 5.15.146.1-microsoft-standard-WSL2 Version : #1 SMP Thu Jan 11 04:09:03 UTC 2024 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : C.UTF-8 LOCALE : C.UTF-8 pandas : 2.2.3 numpy : 2.2.2 pytz : 2025.1 dateutil : 2.9.0.post0 pip : None Cython : None sphinx : 8.1.3 IPython : 8.32.0 adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.13.3 blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : 3.1.5 lxml.etree : 5.3.1 matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : 8.3.4 python-calamine : None pyxlsb : None s3fs : None scipy : 1.15.1 sqlalchemy : None tables : None tabulate : None xarray : 2025.1.2 xlrd : None xlsxwriter : None zstandard : None tzdata : 2025.1 qtpy : None pyqt5 : None

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.