Skip to content

DOC: pd.to_datetime can also return TypeError #56087

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

Closed
1 task done
craigcitro opened this issue Nov 20, 2023 · 1 comment · Fixed by #56095
Closed
1 task done

DOC: pd.to_datetime can also return TypeError #56087

craigcitro opened this issue Nov 20, 2023 · 1 comment · Fixed by #56095
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@craigcitro
Copy link

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.

@craigcitro craigcitro added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 20, 2023
@craigcitro
Copy link
Author

(I'm happy to send a PR.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant