Skip to content

Commit b5cc2ef

Browse files
committed
Merge pull request #11301 from jreback/tz_convert
BUG: Bug in tz-conversions with an ambiguous time and .dt accessors, #11295
2 parents 4a182d3 + c095833 commit b5cc2ef

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

doc/source/whatsnew/v0.17.1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ Bug Fixes
5555

5656
- Bug in ``.to_latex()`` output broken when the index has a name (:issue: `10660`)
5757
- Bug in ``HDFStore.append`` with strings whose encoded length exceded the max unencoded length (:issue:`11234`)
58+
5859
- Bug in ``HDFStore.select`` when comparing with a numpy scalar in a where clause (:issue:`11283`)
5960

61+
- Bug in tz-conversions with an ambiguous time and ``.dt`` accessors (:issues:`11295`)
62+
63+
6064

6165

6266

pandas/tests/test_index.py

+9
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,15 @@ def test_construction_with_alt(self):
33483348
i2 = DatetimeIndex(i.tz_localize(None).asi8, dtype=i.dtype, tz=i.dtype.tz)
33493349
self.assert_index_equal(i, i2)
33503350

3351+
# localize into the provided tz
3352+
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz='UTC')
3353+
expected = i.tz_localize(None).tz_localize('UTC')
3354+
self.assert_index_equal(i2, expected)
3355+
3356+
i2 = DatetimeIndex(i, tz='UTC')
3357+
expected = i.tz_convert('UTC')
3358+
self.assert_index_equal(i2, expected)
3359+
33513360
# incompat tz/dtype
33523361
self.assertRaises(ValueError, lambda : DatetimeIndex(i.tz_localize(None).asi8, dtype=i.dtype, tz='US/Pacific'))
33533362

pandas/tests/test_series.py

+12
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ def get_dir(s):
224224
results = get_dir(s)
225225
tm.assert_almost_equal(results, list(sorted(set(ok_for_period + ok_for_period_methods))))
226226

227+
# 11295
228+
# ambiguous time error on the conversions
229+
s = Series(pd.date_range('2015-01-01', '2016-01-01', freq='T'))
230+
s = s.dt.tz_localize('UTC').dt.tz_convert('America/Chicago')
231+
results = get_dir(s)
232+
tm.assert_almost_equal(results, list(sorted(set(ok_for_dt + ok_for_dt_methods))))
233+
expected = Series(pd.date_range('2015-01-01',
234+
'2016-01-01',
235+
freq='T',
236+
tz='UTC').tz_convert('America/Chicago'))
237+
tm.assert_series_equal(s, expected)
238+
227239
# no setting allowed
228240
s = Series(date_range('20130101',periods=5,freq='D'))
229241
with tm.assertRaisesRegexp(ValueError, "modifications"):

pandas/tseries/common.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def maybe_to_datetimelike(data, copy=False):
4545
raise TypeError("cannot convert an object of type {0} to a datetimelike index".format(type(data)))
4646

4747
index = data.index
48-
if is_datetime64_dtype(data.dtype) or is_datetime64tz_dtype(data.dtype):
48+
if is_datetime64_dtype(data.dtype):
4949
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer'), index, name=data.name)
50+
elif is_datetime64tz_dtype(data.dtype):
51+
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer', ambiguous='infer'), index, name=data.name)
5052
elif is_timedelta64_dtype(data.dtype):
5153
return TimedeltaProperties(TimedeltaIndex(data, copy=copy, freq='infer'), index, name=data.name)
5254
else:

pandas/tseries/frequencies.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,11 @@ def infer_freq(index, warn=True):
851851
raise TypeError("cannot infer freq from a non-convertible index type {0}".format(type(index)))
852852
index = index.values
853853

854-
try:
855-
index = pd.DatetimeIndex(index)
856-
except AmbiguousTimeError:
857-
index = pd.DatetimeIndex(index.asi8)
854+
if not isinstance(index, pd.DatetimeIndex):
855+
try:
856+
index = pd.DatetimeIndex(index)
857+
except AmbiguousTimeError:
858+
index = pd.DatetimeIndex(index.asi8)
858859

859860
inferer = _FrequencyInferer(index, warn=warn)
860861
return inferer.get_freq()

pandas/tseries/index.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def _join_i8_wrapper(joinf, **kwargs):
194194
_datetimelike_ops = ['year','month','day','hour','minute','second',
195195
'weekofyear','week','dayofweek','weekday','dayofyear','quarter', 'days_in_month', 'daysinmonth',
196196
'date','time','microsecond','nanosecond','is_month_start','is_month_end',
197-
'is_quarter_start','is_quarter_end','is_year_start','is_year_end','tz','freq']
197+
'is_quarter_start','is_quarter_end','is_year_start','is_year_end',
198+
'tz','freq']
198199
_is_numeric_dtype = False
199200

200201

@@ -269,14 +270,7 @@ def __new__(cls, data=None,
269270
dayfirst=dayfirst,
270271
yearfirst=yearfirst)
271272

272-
if is_datetimetz(data):
273-
# extract the data whether a Series or Index
274-
if isinstance(data, ABCSeries):
275-
data = data._values
276-
tz = data.tz
277-
data = data.tz_localize(None, ambiguous='infer').values
278-
279-
if issubclass(data.dtype.type, np.datetime64):
273+
if issubclass(data.dtype.type, np.datetime64) or is_datetimetz(data):
280274
if isinstance(data, ABCSeries):
281275
data = data._values
282276
if isinstance(data, DatetimeIndex):

0 commit comments

Comments
 (0)