From 67e525da2ed7852e26806094f42d8f3a509ff0a1 Mon Sep 17 00:00:00 2001 From: jreback Date: Fri, 16 May 2014 09:24:12 -0400 Subject: [PATCH] COMPAT/BUG: compat with pytz 2014.3, exposing several test that don't localize properly (GH7137) --- pandas/tests/test_series.py | 7 +++++-- pandas/tseries/index.py | 6 ++++-- pandas/tseries/tests/test_timezones.py | 16 +++++++++------- pandas/tseries/tools.py | 8 +++++++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index c0a3c9320e6bb..eee0c699b939a 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -4589,8 +4589,11 @@ def test_getitem_setitem_datetime_tz(self): assert_series_equal(result, ts) result = ts.copy() - result[datetime(1990, 1, 1, 3, tzinfo=tz('US/Central'))] = 0 - result[datetime(1990, 1, 1, 3, tzinfo=tz('US/Central'))] = ts[4] + + # comparison dates with datetime MUST be localized! + date = tz('US/Central').localize(datetime(1990, 1, 1, 3)) + result[date] = 0 + result[date] = ts[4] assert_series_equal(result, ts) def test_getitem_setitem_periodindex(self): diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index 61285528a9b36..c1a22f547a86c 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -257,7 +257,7 @@ def __new__(cls, data=None, if lib.is_string_array(values): subarr = _str_to_dt_array(values, freq, dayfirst=dayfirst, - yearfirst=yearfirst) + yearfirst=yearfirst) else: try: subarr = tools.to_datetime(data, box=False) @@ -350,7 +350,9 @@ def _generate(cls, start, end, periods, name, offset, 'different timezones') inferred_tz = tools._maybe_get_tz(inferred_tz) - tz = tools._maybe_get_tz(tz) + + # these may need to be localized + tz = tools._maybe_get_tz(tz, start or end) if tz is not None and inferred_tz is not None: if not inferred_tz == tz: diff --git a/pandas/tseries/tests/test_timezones.py b/pandas/tseries/tests/test_timezones.py index cc976488c19c1..f70cf65f34eba 100644 --- a/pandas/tseries/tests/test_timezones.py +++ b/pandas/tseries/tests/test_timezones.py @@ -317,8 +317,11 @@ def test_with_tz(self): # normalized central = dr.tz_convert(tz) + + # compare vs a localized tz + comp = tz.localize(dr[0].to_pydatetime().replace(tzinfo=None)).tzinfo self.assertIs(central.tz, tz) - self.assertIs(central[0].tz, tz) + self.assertIs(central[0].tz, comp) # datetimes with tzinfo set dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), @@ -393,9 +396,9 @@ def test_infer_tz(self): start = eastern.localize(_start) end = eastern.localize(_end) - assert(tools._infer_tzinfo(start, end) is eastern) - assert(tools._infer_tzinfo(start, None) is eastern) - assert(tools._infer_tzinfo(None, end) is eastern) + assert(tools._infer_tzinfo(start, end) is eastern.localize(_start).tzinfo) + assert(tools._infer_tzinfo(start, None) is eastern.localize(_start).tzinfo) + assert(tools._infer_tzinfo(None, end) is eastern.localize(_end).tzinfo) start = utc.localize(_start) end = utc.localize(_end) @@ -643,8 +646,7 @@ def test_getitem_pydatetime_tz(self): tz='Europe/Berlin') ts = Series(index=index, data=index.hour) time_pandas = Timestamp('2012-12-24 17:00', tz='Europe/Berlin') - time_datetime = datetime(2012, 12, 24, 17, 0, - tzinfo=pytz.timezone('Europe/Berlin')) + time_datetime = pytz.timezone('Europe/Berlin').localize(datetime(2012, 12, 24, 17, 0)) self.assertEqual(ts[time_pandas], ts[time_datetime]) def test_index_drop_dont_lose_tz(self): @@ -974,7 +976,7 @@ def test_tzaware_offset(self): self.assert_(offset.equals(expected)) offset = dates + timedelta(hours=5) self.assert_(offset.equals(expected)) - + def test_nat(self): # GH 5546 dates = [NaT] diff --git a/pandas/tseries/tools.py b/pandas/tseries/tools.py index d01ad56165880..4260705eadb03 100644 --- a/pandas/tseries/tools.py +++ b/pandas/tseries/tools.py @@ -56,13 +56,19 @@ def _infer(a, b): return tz -def _maybe_get_tz(tz): +def _maybe_get_tz(tz, date=None): if isinstance(tz, compat.string_types): import pytz tz = pytz.timezone(tz) if com.is_integer(tz): import pytz tz = pytz.FixedOffset(tz / 60) + + # localize and get the tz + if date is not None and tz is not None: + if date.tzinfo is not None and hasattr(tz,'localize'): + tz = tz.localize(date.replace(tzinfo=None)).tzinfo + return tz def _guess_datetime_format(dt_str, dayfirst=False,