Skip to content

Commit f60f84f

Browse files
committed
fix subclass of str for to_datetime
1 parent cc40732 commit f60f84f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,8 @@ Datetimelike
914914
- Bug in constructing arrays with :class:`ArrowDtype` with ``timestamp`` type incorrectly allowing ``Decimal("NaN")`` (:issue:`61773`)
915915
- Bug in constructing arrays with a timezone-aware :class:`ArrowDtype` from timezone-naive datetime objects incorrectly treating those as UTC times instead of wall times like :class:`DatetimeTZDtype` (:issue:`61775`)
916916
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
917+
- Bug in :func:`pandas.to_datetime` where passing an ``lxml.etree._ElementUnicodeResult`` together with ``format=...`` raised ``TypeError``. Now subclasses of ``str`` are handled. (:issue:`60933`, :pr:`62604`)
918+
917919

918920
Timedelta
919921
^^^^^^^^^

pandas/_libs/tslibs/strptime.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ def array_strptime(
405405
if len(val) == 0 or val in nat_strings:
406406
iresult[i] = NPY_NAT
407407
continue
408+
elif type(val) is not str:
409+
# GH#60933: normalize string subclasses
410+
# (e.g. lxml.etree._ElementUnicodeResult). The downstream Cython
411+
# path expects an exact `str`, so ensure we pass a plain str
412+
val = str(val)
408413
elif checknull_with_nat_and_na(val):
409414
iresult[i] = NPY_NAT
410415
continue

pandas/tests/tools/test_to_datetime.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,3 +3791,15 @@ def test_to_datetime_wrapped_datetime64_ps():
37913791
["1970-01-01 00:00:01.901901901"], dtype="datetime64[ns]", freq=None
37923792
)
37933793
tm.assert_index_equal(result, expected)
3794+
3795+
3796+
def test_to_datetime_lxml_elementunicoderesult_with_format(cache):
3797+
pytest.importorskip("lxml")
3798+
from lxml import etree # pyright: ignore[reportMissingImports]
3799+
3800+
s = "2025-02-05 16:59:57"
3801+
node = etree.XML(f"<date>{s}</date>")
3802+
val = node.xpath("/date/node()")[0] # _ElementUnicodeResult
3803+
3804+
out = to_datetime(Series([val]), format="%Y-%m-%d %H:%M:%S", cache=cache)
3805+
tm.assert_equal(out.iloc[0], Timestamp(s))

0 commit comments

Comments
 (0)