Skip to content

Commit 67e525d

Browse files
committed
COMPAT/BUG: compat with pytz 2014.3, exposing several test that don't localize
properly (GH7137)
1 parent 592a537 commit 67e525d

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

pandas/tests/test_series.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4589,8 +4589,11 @@ def test_getitem_setitem_datetime_tz(self):
45894589
assert_series_equal(result, ts)
45904590

45914591
result = ts.copy()
4592-
result[datetime(1990, 1, 1, 3, tzinfo=tz('US/Central'))] = 0
4593-
result[datetime(1990, 1, 1, 3, tzinfo=tz('US/Central'))] = ts[4]
4592+
4593+
# comparison dates with datetime MUST be localized!
4594+
date = tz('US/Central').localize(datetime(1990, 1, 1, 3))
4595+
result[date] = 0
4596+
result[date] = ts[4]
45944597
assert_series_equal(result, ts)
45954598

45964599
def test_getitem_setitem_periodindex(self):

pandas/tseries/index.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def __new__(cls, data=None,
257257

258258
if lib.is_string_array(values):
259259
subarr = _str_to_dt_array(values, freq, dayfirst=dayfirst,
260-
yearfirst=yearfirst)
260+
yearfirst=yearfirst)
261261
else:
262262
try:
263263
subarr = tools.to_datetime(data, box=False)
@@ -350,7 +350,9 @@ def _generate(cls, start, end, periods, name, offset,
350350
'different timezones')
351351

352352
inferred_tz = tools._maybe_get_tz(inferred_tz)
353-
tz = tools._maybe_get_tz(tz)
353+
354+
# these may need to be localized
355+
tz = tools._maybe_get_tz(tz, start or end)
354356

355357
if tz is not None and inferred_tz is not None:
356358
if not inferred_tz == tz:

pandas/tseries/tests/test_timezones.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ def test_with_tz(self):
317317

318318
# normalized
319319
central = dr.tz_convert(tz)
320+
321+
# compare vs a localized tz
322+
comp = tz.localize(dr[0].to_pydatetime().replace(tzinfo=None)).tzinfo
320323
self.assertIs(central.tz, tz)
321-
self.assertIs(central[0].tz, tz)
324+
self.assertIs(central[0].tz, comp)
322325

323326
# datetimes with tzinfo set
324327
dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc),
@@ -393,9 +396,9 @@ def test_infer_tz(self):
393396

394397
start = eastern.localize(_start)
395398
end = eastern.localize(_end)
396-
assert(tools._infer_tzinfo(start, end) is eastern)
397-
assert(tools._infer_tzinfo(start, None) is eastern)
398-
assert(tools._infer_tzinfo(None, end) is eastern)
399+
assert(tools._infer_tzinfo(start, end) is eastern.localize(_start).tzinfo)
400+
assert(tools._infer_tzinfo(start, None) is eastern.localize(_start).tzinfo)
401+
assert(tools._infer_tzinfo(None, end) is eastern.localize(_end).tzinfo)
399402

400403
start = utc.localize(_start)
401404
end = utc.localize(_end)
@@ -643,8 +646,7 @@ def test_getitem_pydatetime_tz(self):
643646
tz='Europe/Berlin')
644647
ts = Series(index=index, data=index.hour)
645648
time_pandas = Timestamp('2012-12-24 17:00', tz='Europe/Berlin')
646-
time_datetime = datetime(2012, 12, 24, 17, 0,
647-
tzinfo=pytz.timezone('Europe/Berlin'))
649+
time_datetime = pytz.timezone('Europe/Berlin').localize(datetime(2012, 12, 24, 17, 0))
648650
self.assertEqual(ts[time_pandas], ts[time_datetime])
649651

650652
def test_index_drop_dont_lose_tz(self):
@@ -974,7 +976,7 @@ def test_tzaware_offset(self):
974976
self.assert_(offset.equals(expected))
975977
offset = dates + timedelta(hours=5)
976978
self.assert_(offset.equals(expected))
977-
979+
978980
def test_nat(self):
979981
# GH 5546
980982
dates = [NaT]

pandas/tseries/tools.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ def _infer(a, b):
5656
return tz
5757

5858

59-
def _maybe_get_tz(tz):
59+
def _maybe_get_tz(tz, date=None):
6060
if isinstance(tz, compat.string_types):
6161
import pytz
6262
tz = pytz.timezone(tz)
6363
if com.is_integer(tz):
6464
import pytz
6565
tz = pytz.FixedOffset(tz / 60)
66+
67+
# localize and get the tz
68+
if date is not None and tz is not None:
69+
if date.tzinfo is not None and hasattr(tz,'localize'):
70+
tz = tz.localize(date.replace(tzinfo=None)).tzinfo
71+
6672
return tz
6773

6874
def _guess_datetime_format(dt_str, dayfirst=False,

0 commit comments

Comments
 (0)