66
77import numpy as np
88
9+ import warnings
10+
911from pandas .core .common import (_NS_DTYPE , _INT64_DTYPE ,
1012 _values_from_object , _maybe_box ,
1113 ABCSeries )
1820from pandas .core .base import DatetimeIndexOpsMixin
1921from pandas .tseries .offsets import DateOffset , generate_range , Tick , CDay
2022from 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
2224import pandas .core .common as com
2325import pandas .tseries .offsets as offsets
2426import pandas .tseries .tools as tools
@@ -145,6 +147,15 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index):
145147 closed : string or None, default None
146148 Make the interval closed with respect to the given frequency to
147149 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
148159 name : object
149160 Name to be stored in the index
150161 """
@@ -180,15 +191,17 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index):
180191 'is_quarter_start' ,'is_quarter_end' ,'is_year_start' ,'is_year_end' ]
181192 _is_numeric_dtype = False
182193
194+
195+ @deprecate_kwarg (old_arg_name = 'infer_dst' , new_arg_name = 'ambiguous' ,
196+ mapping = {True : 'infer' , False : 'raise' })
183197 def __new__ (cls , data = None ,
184198 freq = None , start = None , end = None , periods = None ,
185199 copy = False , name = None , tz = None ,
186200 verify_integrity = True , normalize = False ,
187- closed = None , ** kwargs ):
201+ closed = None , ambiguous = 'raise' , ** kwargs ):
188202
189203 dayfirst = kwargs .pop ('dayfirst' , None )
190204 yearfirst = kwargs .pop ('yearfirst' , None )
191- infer_dst = kwargs .pop ('infer_dst' , False )
192205
193206 freq_infer = False
194207 if not isinstance (freq , DateOffset ):
@@ -214,7 +227,7 @@ def __new__(cls, data=None,
214227 if data is None :
215228 return cls ._generate (start , end , periods , name , freq ,
216229 tz = tz , normalize = normalize , closed = closed ,
217- infer_dst = infer_dst )
230+ ambiguous = ambiguous )
218231
219232 if not isinstance (data , (np .ndarray , Index , ABCSeries )):
220233 if np .isscalar (data ):
@@ -240,7 +253,7 @@ def __new__(cls, data=None,
240253 data .name = name
241254
242255 if tz is not None :
243- return data .tz_localize (tz , infer_dst = infer_dst )
256+ return data .tz_localize (tz , ambiguous = ambiguous )
244257
245258 return data
246259
@@ -309,7 +322,7 @@ def __new__(cls, data=None,
309322 # Convert tz-naive to UTC
310323 ints = subarr .view ('i8' )
311324 subarr = tslib .tz_localize_to_utc (ints , tz ,
312- infer_dst = infer_dst )
325+ ambiguous = ambiguous )
313326
314327 subarr = subarr .view (_NS_DTYPE )
315328
@@ -333,7 +346,7 @@ def __new__(cls, data=None,
333346
334347 @classmethod
335348 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 ):
337350 if com ._count_not_none (start , end , periods ) != 2 :
338351 raise ValueError ('Must specify two of start, end, or periods' )
339352
@@ -447,7 +460,7 @@ def _generate(cls, start, end, periods, name, offset,
447460
448461 if tz is not None and getattr (index , 'tz' , None ) is None :
449462 index = tslib .tz_localize_to_utc (com ._ensure_int64 (index ), tz ,
450- infer_dst = infer_dst )
463+ ambiguous = ambiguous )
451464 index = index .view (_NS_DTYPE )
452465
453466 index = cls ._simple_new (index , name = name , freq = offset , tz = tz )
@@ -1645,7 +1658,9 @@ def tz_convert(self, tz):
16451658 # No conversion since timestamps are all UTC to begin with
16461659 return self ._shallow_copy (tz = tz )
16471660
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' ):
16491664 """
16501665 Localize tz-naive DatetimeIndex to given time zone (using pytz/dateutil),
16511666 or remove timezone from tz-aware DatetimeIndex
@@ -1656,7 +1671,13 @@ def tz_localize(self, tz, infer_dst=False):
16561671 Time zone for time. Corresponding timestamps would be converted to
16571672 time zone of the TimeSeries.
16581673 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)
16601681 Attempt to infer fall dst-transition hours based on order
16611682
16621683 Returns
@@ -1671,7 +1692,9 @@ def tz_localize(self, tz, infer_dst=False):
16711692 else :
16721693 tz = tslib .maybe_get_tz (tz )
16731694 # 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 )
16751698 new_dates = new_dates .view (_NS_DTYPE )
16761699 return self ._shallow_copy (new_dates , tz = tz )
16771700
0 commit comments