Skip to content

COMPAT/BUG: compat with pytz 2014.3, exposing several test that don't localize (GH7137) #7144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions pandas/tseries/tests/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down