diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index 4e6ea85e2ff1d..bcb096e630d85 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -33,6 +33,8 @@ Bug fixes - Bug in :meth:`pandas.read_excel` modifies the dtypes dictionary when reading a file with duplicate columns (:issue:`42462`) - 1D slices over extension types turn into N-dimensional slices over ExtensionArrays (:issue:`42430`) - :meth:`.Styler.hide_columns` now hides the index name header row as well as column headers (:issue:`42101`) +- Bug in de-serializing datetime indexes in PYTHONOPTIMIZED mode (:issue:`42866`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index fbfee9a1f524c..ff9a273267306 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -92,7 +92,8 @@ def _new_DatetimeIndex(cls, d): # These are already stored in our DatetimeArray; if they are # also in the pickle and don't match, we have a problem. if key in d: - assert d.pop(key) == getattr(dta, key) + assert d[key] == getattr(dta, key) + d.pop(key) result = cls._simple_new(dta, **d) else: with warnings.catch_warnings(): diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index ea95f90d3a2cb..e34ca9e7f9e27 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -69,6 +69,21 @@ def test_oo_optimizable(): subprocess.check_call([sys.executable, "-OO", "-c", "import pandas"]) +def test_oo_optimized_datetime_index_unpickle(): + # GH 42866 + subprocess.check_call( + [ + sys.executable, + "-OO", + "-c", + ( + "import pandas as pd, pickle; " + "pickle.loads(pickle.dumps(pd.date_range('2021-01-01', periods=1)))" + ), + ] + ) + + @tm.network # Cython import warning @pytest.mark.filterwarnings("ignore:pandas.util.testing is deprecated")