Skip to content

DOC: pd.to_datetime can also return TypeError #56087

Closed
@craigcitro

Description

@craigcitro

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html

Documentation problem

to_datetime can also return a TypeError when parsing fails. Example:

$ docker run -it --rm --entrypoint bash python:3.12
Unable to find image 'python:3.12' locally
3.12: Pulling from library/python
...
Digest: sha256:89f4c413ac0f36072211bced42ff7e8870cf5347c3cde4b84a67b5f87911b9a3
Status: Downloaded newer image for python:3.12
root@5fc68ee7d209:/# python3 -m pip install pandas
...
Successfully installed numpy-1.26.2 pandas-2.1.3 python-dateutil-2.8.2 pytz-2023.3.post1 six-1.16.0 tzdata-2023.3
root@5fc68ee7d209:/# python3
Python 3.12.0 (main, Nov  1 2023, 12:56:53) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.to_datetime([True, False])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/pandas/core/tools/datetimes.py", line 1144, in to_datetime
    result = convert_listlike(argc, format)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pandas/core/tools/datetimes.py", line 490, in _convert_listlike_datetimes
    result, tz_parsed = objects_to_datetime64ns(
                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pandas/core/arrays/datetimes.py", line 2346, in objects_to_datetime64ns
    result, tz_parsed = tslib.array_to_datetime(
                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "tslib.pyx", line 403, in pandas._libs.tslib.array_to_datetime
  File "tslib.pyx", line 552, in pandas._libs.tslib.array_to_datetime
  File "tslib.pyx", line 541, in pandas._libs.tslib.array_to_datetime
TypeError: <class 'bool'> is not convertible to datetime, at position 0
>>>

Suggested fix for documentation

add

TypeError
  When a type cannot be converted to datetime

to the list of possible exceptions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions