Skip to content

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

Closed
@GorAlexander

Description

@GorAlexander
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions