Skip to content

BUG: DatetimeIndex de-serializing fails in PYTHONOPTIMIZE mode (python -O) #42866

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
3 tasks done
GorAlexander opened this issue Aug 3, 2021 · 2 comments · Fixed by #42871
Closed
3 tasks done

BUG: DatetimeIndex de-serializing fails in PYTHONOPTIMIZE mode (python -O) #42866

GorAlexander opened this issue Aug 3, 2021 · 2 comments · Fixed by #42871
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@GorAlexander
Copy link
Contributor

GorAlexander commented Aug 3, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

Run python as PYTHONOPTIMIZE, option -O:

python -O
import pickle
import pandas as pd

idx = pd.date_range('2021-01-01', '2021-01-02', freq='D')
idx_dump = pickle.dumps(idx)
pickle.loads(idx_dump)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.7/site-packages/pandas/core/indexes/datetimes.py", line 61, in _new_DatetimeIndex
    result = cls._simple_new(dta, **d)
TypeError: _simple_new() got an unexpected keyword argument 'tz'

Problem description

These dict operations go under the 'assert' statement:

# pandas/core/indexes/datetimes.py

def _new_DatetimeIndex(cls, d):
    ...
        if key in d:
            assert d.pop(key) == getattr(dta, key)

    ...

which are ignored in PYTHONOPTIMIZED mode.
As a fix, bring dict pop operation out of the assert statement.

Expected Output

DatetimeIndex(['2021-01-01', '2021-01-02'], dtype='datetime64[ns]', freq='D')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7c48ff4
python : 3.7.9.final.0
python-bits : 64
OS : Darwin
OS-release : 20.5.0
Version : Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.2.5
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.2
setuptools : 57.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : None
IPython : 7.20.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@GorAlexander GorAlexander added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2021
@MarcoGorelli
Copy link
Member

Thanks @GorAlexander for the report - want to submit a PR?

@MarcoGorelli MarcoGorelli added Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2021
@GorAlexander
Copy link
Contributor Author

Hi @MarcoGorelli , just created the PR GH42866. Not sure if guideline checkpoints would pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants