6
6
7
7
import numpy as np
8
8
9
+ import warnings
10
+
9
11
from pandas .core .common import (_NS_DTYPE , _INT64_DTYPE ,
10
12
_values_from_object , _maybe_box ,
11
13
ABCSeries )
18
20
from pandas .core .base import DatetimeIndexOpsMixin
19
21
from pandas .tseries .offsets import DateOffset , generate_range , Tick , CDay
20
22
from pandas .tseries .tools import parse_time_string , normalize_date
21
- from pandas .util .decorators import cache_readonly
23
+ from pandas .util .decorators import cache_readonly , deprecate_kwarg
22
24
import pandas .core .common as com
23
25
import pandas .tseries .offsets as offsets
24
26
import pandas .tseries .tools as tools
@@ -145,6 +147,15 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index):
145
147
closed : string or None, default None
146
148
Make the interval closed with respect to the given frequency to
147
149
the 'left', 'right', or both sides (None)
150
+ tz : pytz.timezone or dateutil.tz.tzfile
151
+ ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
152
+ - 'infer' will attempt to infer fall dst-transition hours based on order
153
+ - bool-ndarray where True signifies a DST time, False signifies
154
+ a non-DST time (note that this flag is only applicable for ambiguous times)
155
+ - 'NaT' will return NaT where there are ambiguous times
156
+ - 'raise' will raise an AmbiguousTimeError if there are ambiguous times
157
+ infer_dst : boolean, default False (DEPRECATED)
158
+ Attempt to infer fall dst-transition hours based on order
148
159
name : object
149
160
Name to be stored in the index
150
161
"""
@@ -180,15 +191,17 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index):
180
191
'is_quarter_start' ,'is_quarter_end' ,'is_year_start' ,'is_year_end' ]
181
192
_is_numeric_dtype = False
182
193
194
+
195
+ @deprecate_kwarg (old_arg_name = 'infer_dst' , new_arg_name = 'ambiguous' ,
196
+ mapping = {True : 'infer' , False : 'raise' })
183
197
def __new__ (cls , data = None ,
184
198
freq = None , start = None , end = None , periods = None ,
185
199
copy = False , name = None , tz = None ,
186
200
verify_integrity = True , normalize = False ,
187
- closed = None , ** kwargs ):
201
+ closed = None , ambiguous = 'raise' , ** kwargs ):
188
202
189
203
dayfirst = kwargs .pop ('dayfirst' , None )
190
204
yearfirst = kwargs .pop ('yearfirst' , None )
191
- infer_dst = kwargs .pop ('infer_dst' , False )
192
205
193
206
freq_infer = False
194
207
if not isinstance (freq , DateOffset ):
@@ -214,7 +227,7 @@ def __new__(cls, data=None,
214
227
if data is None :
215
228
return cls ._generate (start , end , periods , name , freq ,
216
229
tz = tz , normalize = normalize , closed = closed ,
217
- infer_dst = infer_dst )
230
+ ambiguous = ambiguous )
218
231
219
232
if not isinstance (data , (np .ndarray , Index , ABCSeries )):
220
233
if np .isscalar (data ):
@@ -240,7 +253,7 @@ def __new__(cls, data=None,
240
253
data .name = name
241
254
242
255
if tz is not None :
243
- return data .tz_localize (tz , infer_dst = infer_dst )
256
+ return data .tz_localize (tz , ambiguous = ambiguous )
244
257
245
258
return data
246
259
@@ -309,7 +322,7 @@ def __new__(cls, data=None,
309
322
# Convert tz-naive to UTC
310
323
ints = subarr .view ('i8' )
311
324
subarr = tslib .tz_localize_to_utc (ints , tz ,
312
- infer_dst = infer_dst )
325
+ ambiguous = ambiguous )
313
326
314
327
subarr = subarr .view (_NS_DTYPE )
315
328
@@ -333,7 +346,7 @@ def __new__(cls, data=None,
333
346
334
347
@classmethod
335
348
def _generate (cls , start , end , periods , name , offset ,
336
- tz = None , normalize = False , infer_dst = False , closed = None ):
349
+ tz = None , normalize = False , ambiguous = 'raise' , closed = None ):
337
350
if com ._count_not_none (start , end , periods ) != 2 :
338
351
raise ValueError ('Must specify two of start, end, or periods' )
339
352
@@ -447,7 +460,7 @@ def _generate(cls, start, end, periods, name, offset,
447
460
448
461
if tz is not None and getattr (index , 'tz' , None ) is None :
449
462
index = tslib .tz_localize_to_utc (com ._ensure_int64 (index ), tz ,
450
- infer_dst = infer_dst )
463
+ ambiguous = ambiguous )
451
464
index = index .view (_NS_DTYPE )
452
465
453
466
index = cls ._simple_new (index , name = name , freq = offset , tz = tz )
@@ -1645,7 +1658,9 @@ def tz_convert(self, tz):
1645
1658
# No conversion since timestamps are all UTC to begin with
1646
1659
return self ._shallow_copy (tz = tz )
1647
1660
1648
- def tz_localize (self , tz , infer_dst = False ):
1661
+ @deprecate_kwarg (old_arg_name = 'infer_dst' , new_arg_name = 'ambiguous' ,
1662
+ mapping = {True : 'infer' , False : 'raise' })
1663
+ def tz_localize (self , tz , ambiguous = 'raise' ):
1649
1664
"""
1650
1665
Localize tz-naive DatetimeIndex to given time zone (using pytz/dateutil),
1651
1666
or remove timezone from tz-aware DatetimeIndex
@@ -1656,7 +1671,13 @@ def tz_localize(self, tz, infer_dst=False):
1656
1671
Time zone for time. Corresponding timestamps would be converted to
1657
1672
time zone of the TimeSeries.
1658
1673
None will remove timezone holding local time.
1659
- infer_dst : boolean, default False
1674
+ ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
1675
+ - 'infer' will attempt to infer fall dst-transition hours based on order
1676
+ - bool-ndarray where True signifies a DST time, False signifies
1677
+ a non-DST time (note that this flag is only applicable for ambiguous times)
1678
+ - 'NaT' will return NaT where there are ambiguous times
1679
+ - 'raise' will raise an AmbiguousTimeError if there are ambiguous times
1680
+ infer_dst : boolean, default False (DEPRECATED)
1660
1681
Attempt to infer fall dst-transition hours based on order
1661
1682
1662
1683
Returns
@@ -1671,7 +1692,9 @@ def tz_localize(self, tz, infer_dst=False):
1671
1692
else :
1672
1693
tz = tslib .maybe_get_tz (tz )
1673
1694
# Convert to UTC
1674
- new_dates = tslib .tz_localize_to_utc (self .asi8 , tz , infer_dst = infer_dst )
1695
+
1696
+ new_dates = tslib .tz_localize_to_utc (self .asi8 , tz ,
1697
+ ambiguous = ambiguous )
1675
1698
new_dates = new_dates .view (_NS_DTYPE )
1676
1699
return self ._shallow_copy (new_dates , tz = tz )
1677
1700
0 commit comments