7
7
8
8
from pandas ._libs import NaT , Period , Timestamp , index as libindex , lib , tslib as libts
9
9
from pandas ._libs .tslibs import fields , parsing , timezones
10
+ from pandas ._typing import Label
10
11
from pandas .util ._decorators import cache_readonly
11
12
12
13
from pandas .core .dtypes .common import _NS_DTYPE , is_float , is_integer , is_scalar
13
- from pandas .core .dtypes .dtypes import DatetimeTZDtype
14
14
from pandas .core .dtypes .missing import is_valid_nat_for_dtype
15
15
16
- from pandas .core .arrays .datetimes import (
17
- DatetimeArray ,
18
- tz_to_dtype ,
19
- validate_tz_from_dtype ,
20
- )
16
+ from pandas .core .arrays .datetimes import DatetimeArray , tz_to_dtype
21
17
import pandas .core .common as com
22
18
from pandas .core .indexes .base import Index , InvalidIndexError , maybe_extract_name
23
19
from pandas .core .indexes .datetimelike import DatetimeTimedeltaMixin
@@ -36,7 +32,20 @@ def _new_DatetimeIndex(cls, d):
36
32
if "data" in d and not isinstance (d ["data" ], DatetimeIndex ):
37
33
# Avoid need to verify integrity by calling simple_new directly
38
34
data = d .pop ("data" )
39
- result = cls ._simple_new (data , ** d )
35
+ if not isinstance (data , DatetimeArray ):
36
+ # For backward compat with older pickles, we may need to construct
37
+ # a DatetimeArray to adapt to the newer _simple_new signature
38
+ tz = d .pop ("tz" )
39
+ freq = d .pop ("freq" )
40
+ dta = DatetimeArray ._simple_new (data , dtype = tz_to_dtype (tz ), freq = freq )
41
+ else :
42
+ dta = data
43
+ for key in ["tz" , "freq" ]:
44
+ # These are already stored in our DatetimeArray; if they are
45
+ # also in the pickle and don't match, we have a problem.
46
+ if key in d :
47
+ assert d .pop (key ) == getattr (dta , key )
48
+ result = cls ._simple_new (dta , ** d )
40
49
else :
41
50
with warnings .catch_warnings ():
42
51
# TODO: If we knew what was going in to **d, we might be able to
@@ -244,34 +253,16 @@ def __new__(
244
253
return subarr
245
254
246
255
@classmethod
247
- def _simple_new (cls , values , name = None , freq = None , tz = None , dtype = None ):
248
- """
249
- We require the we have a dtype compat for the values
250
- if we are passed a non-dtype compat, then coerce using the constructor
251
- """
252
- if isinstance (values , DatetimeArray ):
253
- if tz :
254
- tz = validate_tz_from_dtype (dtype , tz )
255
- dtype = DatetimeTZDtype (tz = tz )
256
- elif dtype is None :
257
- dtype = _NS_DTYPE
258
-
259
- values = DatetimeArray (values , freq = freq , dtype = dtype )
260
- tz = values .tz
261
- freq = values .freq
262
- values = values ._data
263
-
264
- dtype = tz_to_dtype (tz )
265
- dtarr = DatetimeArray ._simple_new (values , freq = freq , dtype = dtype )
266
- assert isinstance (dtarr , DatetimeArray )
256
+ def _simple_new (cls , values : DatetimeArray , name : Label = None ):
257
+ assert isinstance (values , DatetimeArray ), type (values )
267
258
268
259
result = object .__new__ (cls )
269
- result ._data = dtarr
260
+ result ._data = values
270
261
result .name = name
271
262
result ._cache = {}
272
263
result ._no_setting_name = False
273
264
# For groupby perf. See note in indexes/base about _index_data
274
- result ._index_data = dtarr ._data
265
+ result ._index_data = values ._data
275
266
result ._reset_identity ()
276
267
return result
277
268
0 commit comments