Skip to content
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

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

Open
3 tasks done
bentriom opened this issue Feb 14, 2025 · 2 comments
Labels
Bug datetime.date stdlib datetime.date support Needs Discussion Requires discussion from core team before further action

Comments

@bentriom
Copy link

bentriom commented Feb 14, 2025

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

@bentriom bentriom added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 14, 2025
@rhshadrach
Copy link
Member

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:

Wrapping the argument of the caller here:

val, &dts, &out_bestunit, &out_local,

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!

@rhshadrach
Copy link
Member

Looks like this is indeed expected behavior in Cython: cython/cython#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.

@rhshadrach rhshadrach added datetime.date stdlib datetime.date support Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug datetime.date stdlib datetime.date support Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

2 participants