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: to_datetime() with errors='coerce' raises AttributeError on MacOS #54479

Open
3 tasks done
nhoover opened this issue Aug 10, 2023 · 7 comments
Open
3 tasks done

BUG: to_datetime() with errors='coerce' raises AttributeError on MacOS #54479

nhoover opened this issue Aug 10, 2023 · 7 comments
Labels
Bug OS X Related to Mac OS & hardware issues (M1) Timezones Timezone data dtype

Comments

@nhoover
Copy link

nhoover commented Aug 10, 2023

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

>>> 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 : 0f43794
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

@nhoover nhoover added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 10, 2023
@Cyddharth-Gupta
Copy link

take

@Cyddharth-Gupta
Copy link

Hey @nhoover, just checked the issue, the third case is also converting to NaT without throwing any exception. I am using the latest version of pandas and it isn't occurring there. Also, I tried it in an online compiler and it seemed to return the desired result, i.e., NaT.

@Cyddharth-Gupta Cyddharth-Gupta removed their assignment Aug 10, 2023
@asishm-wk
Copy link

Doesn't reproduce for me either (on WSL2).

With 2.0.3 I get DatetimeIndex(['2018-10-26 12:00:00', '2014-10-03 13:00:00'], dtype='datetime64[ns]', freq=None)

On main I get a deprecation warning with the same result as above.
FutureWarning: Parsed string "10-3-2014 13:00:00 PDT" included an un-recognized timezone "PDT". Dropping unrecognized timezones is deprecated; in a future version this will raise. Instead pass the string without the timezone, then use .tz_localize to convert to a recognized timezone.

@jenno-verdonck jenno-verdonck mentioned this issue Aug 10, 2023
3 tasks
@jenno-verdonck
Copy link

jenno-verdonck commented Aug 10, 2023

With 2.0.3 on windows I get different results depending on the input. I never seem to get the AttributeError .

Input: pd.to_datetime(['2018-10-26 12:00:00', '10-3-2014 13:00:00 PDT'], format='mixed', errors='coerce')
Output: DatetimeIndex(['2018-10-26 12:00:00', '2014-10-03 13:00:00'], dtype='datetime64[ns]', freq=None)

Input: pd.to_datetime(['10-3-2014 13:00:00 PDT'], errors='coerce')
Output: UnknownTimezoneWarning: tzname PDT identified but not understood. Pass 'tzinfos' argument in order to correctly return a timezone-aware datetime. In a future version, this will raise an exception. warnings.warn("tzname {tzname} identified but not understood. " DatetimeIndex(['2014-10-03 13:00:00'], dtype='datetime64[ns]', freq=None)

The same exception as reported above was also reported here #54481.

@nhoover nhoover changed the title BUG: to_datetime() with errors='coerce' raises AttributeError BUG: to_datetime() with errors='coerce' raises AttributeError on MacOS Aug 10, 2023
@nhoover
Copy link
Author

nhoover commented Aug 10, 2023

Well it's so hard to reproduce and interesting that the same issue was reported again a few hours after I did. So it's happening for at least 2 people. Running on Windows 10 and Windows 11 with same 3.11.4 python/2.0.3 pandas I get the same as @jenno-verdonck

I submitted the issue running on MacOS 13.5 so adjusted the issue title.

@nhoover
Copy link
Author

nhoover commented Aug 10, 2023

A friend tried this on an M1 Mac and also couldn't reproduce. So maybe it's only on Intel Mac. Mine is a 2019 Mac Book Pro: 2.4 GHz 8-Core Intel Core i9.

@lithomas1
Copy link
Member

I can't reproduce but,
If this happens utcoffset should be returning None at this line

nsecs = tz.utcoffset(None).total_seconds()

cc @MarcoGorelli

@lithomas1 lithomas1 added Timezones Timezone data dtype Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 12, 2023
@mroeschke mroeschke added OS X Related to Mac OS & hardware issues (M1) and removed Needs Info Clarification about behavior needed to assess issue labels Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug OS X Related to Mac OS & hardware issues (M1) Timezones Timezone data dtype
Projects
None yet
Development

No branches or pull requests

6 participants