Skip to content

Commit eb505fd

Browse files
committed
Small tweaks to PR #2785
1 parent 9d58bbe commit eb505fd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pandas/tests/test_series.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def _skip_if_no_scipy():
3434
except ImportError:
3535
raise nose.SkipTest
3636

37+
def _skip_if_no_pytz():
38+
try:
39+
import pytz
40+
except ImportError:
41+
raise nose.SkipTest
3742

3843
#-------------------------------------------------------------------------------
3944
# Series test cases
@@ -2545,7 +2550,6 @@ def test_asof(self):
25452550
self.assert_(np.isnan(self.ts.asof(d)))
25462551

25472552
def test_getitem_setitem_datetimeindex(self):
2548-
from pytz import timezone as tz
25492553
from pandas import date_range
25502554
N = 50
25512555
# testing with timezone, GH #2785
@@ -2624,6 +2628,16 @@ def test_getitem_setitem_datetimeindex(self):
26242628
result["1990-01-02"] = ts[24:48]
26252629
assert_series_equal(result, ts)
26262630

2631+
def test_getitem_setitem_datetime_tz(self):
2632+
_skip_if_no_pytz();
2633+
from pytz import timezone as tz
2634+
2635+
from pandas import date_range
2636+
N = 50
2637+
# testing with timezone, GH #2785
2638+
rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern')
2639+
ts = Series(np.random.randn(N), index=rng)
2640+
26272641
# also test Timestamp tz handling, GH #2789
26282642
result = ts.copy()
26292643
result["1990-01-01 09:00:00+00:00"] = 0

pandas/tslib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ cdef convert_to_tsobject(object ts, object tz):
617617
# sort of a temporary hack
618618
if ts.tzinfo is not None:
619619
if (hasattr(tz, 'normalize') and
620-
hasattr(ts.tzinfo, '_utcoffset')):
620+
hasattr(ts.tzinfo, '_utcoffset')):
621621
ts = tz.normalize(ts)
622622
obj.value = _pydatetime_to_dts(ts, &obj.dts)
623623
obj.tzinfo = ts.tzinfo
@@ -631,9 +631,9 @@ cdef convert_to_tsobject(object ts, object tz):
631631
PANDAS_FR_ns, &obj.dts)
632632
obj.tzinfo = tz
633633
elif not _is_utc(tz):
634-
if (hasattr(tz, 'localize')):
634+
try:
635635
ts = tz.localize(ts)
636-
else:
636+
except AttributeError:
637637
ts = ts.replace(tzinfo=tz)
638638
obj.value = _pydatetime_to_dts(ts, &obj.dts)
639639
obj.tzinfo = ts.tzinfo

0 commit comments

Comments
 (0)