Skip to content

BUG: Converting string of type lxml.etree._ElementUnicodeResult to a datetime using pandas.to_datetime results in a TypeError. #60933

Open
@bentriom

Description

@bentriom

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • 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 : 19ea997 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 : 0691c5c 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions