pandas
pandas copied to clipboard
BUG: to_datetime() with errors='coerce' raises AttributeError on MacOS
Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
>>> import pandas as pd
>>> pd.to_datetime(['2018-10-26 12:00:00', '10-3-2014 13:00'], format='mixed', errors='coerce')
DatetimeIndex(['2018-10-26 12:00:00', '2014-10-03 13:00:00'], dtype='datetime64[ns]', freq=None)
>>> pd.to_datetime(['2018-10-26 12:00:00', '10-3-2014 13:AA'], format='mixed', errors='coerce')
DatetimeIndex(['2018-10-26 12:00:00', 'NaT'], dtype='datetime64[ns]', freq=None)
>>> pd.to_datetime(['2018-10-26 12:00:00', '10-3-2014 13:00:00 PDT'], format='mixed', errors='coerce')
<stdin>:1: FutureWarning: Parsing '{res.tzname}' as tzlocal (dependent on system timezone) is deprecated and will raise in a future version. Pass the 'tz' keyword or call tz_localize after construction instead
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/nathanhoover/dev/nstrava/venv/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 1082, in to_datetime
result = convert_listlike(argc, format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathanhoover/dev/nstrava/venv/lib/python3.11/site-packages/pandas/core/tools/datetimes.py", line 455, in _convert_listlike_datetimes
result, tz_parsed = objects_to_datetime64ns(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nathanhoover/dev/nstrava/venv/lib/python3.11/site-packages/pandas/core/arrays/datetimes.py", line 2177, in objects_to_datetime64ns
result, tz_parsed = tslib.array_to_datetime(
^^^^^^^^^^^^^^^^^^^^^^^^
File "pandas/_libs/tslib.pyx", line 402, in pandas._libs.tslib.array_to_datetime
File "pandas/_libs/tslib.pyx", line 527, in pandas._libs.tslib.array_to_datetime
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Issue Description
When converting a date string with to_datetime(errors='coerce') an AttributeError is raised in one case where the input string contains a timezone.
Expected Behavior
In the above examples, the first case '10-3-2014 13:00' is properly converted as '2014-10-03 13:00:00' The second case '10-3-2014 13:AA' properly converts to 'NaT' with no exception The third case '10-3-2014 13:00:00 PDT' should either work or convert to NaT - either way no AttributeError should be raised.
Installed Versions
INSTALLED VERSIONS
commit : 0f437949513225922d851e9581723d82120684a6 python : 3.11.4.final.0 python-bits : 64 OS : Darwin OS-release : 22.6.0 Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8
pandas : 2.0.3 numpy : 1.25.0 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 65.5.1 pip : 23.2.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : None pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None