diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 415f13cb2cfb4..1ac61ea276cac 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -34,6 +34,11 @@ def _skip_if_no_scipy(): except ImportError: raise nose.SkipTest +def _skip_if_no_pytz(): + try: + import pytz + except ImportError: + raise nose.SkipTest #------------------------------------------------------------------------------- # Series test cases @@ -2545,7 +2550,6 @@ def test_asof(self): self.assert_(np.isnan(self.ts.asof(d))) def test_getitem_setitem_datetimeindex(self): - from pytz import timezone as tz from pandas import date_range N = 50 # testing with timezone, GH #2785 @@ -2624,6 +2628,16 @@ def test_getitem_setitem_datetimeindex(self): result["1990-01-02"] = ts[24:48] assert_series_equal(result, ts) + def test_getitem_setitem_datetime_tz(self): + _skip_if_no_pytz(); + from pytz import timezone as tz + + from pandas import date_range + N = 50 + # testing with timezone, GH #2785 + rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern') + ts = Series(np.random.randn(N), index=rng) + # also test Timestamp tz handling, GH #2789 result = ts.copy() result["1990-01-01 09:00:00+00:00"] = 0 diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index c0b500afef5ba..98d1d81b9aabc 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -617,7 +617,7 @@ cdef convert_to_tsobject(object ts, object tz): # sort of a temporary hack if ts.tzinfo is not None: if (hasattr(tz, 'normalize') and - hasattr(ts.tzinfo, '_utcoffset')): + hasattr(ts.tzinfo, '_utcoffset')): ts = tz.normalize(ts) obj.value = _pydatetime_to_dts(ts, &obj.dts) obj.tzinfo = ts.tzinfo @@ -631,9 +631,9 @@ cdef convert_to_tsobject(object ts, object tz): PANDAS_FR_ns, &obj.dts) obj.tzinfo = tz elif not _is_utc(tz): - if (hasattr(tz, 'localize')): + try: ts = tz.localize(ts) - else: + except AttributeError: ts = ts.replace(tzinfo=tz) obj.value = _pydatetime_to_dts(ts, &obj.dts) obj.tzinfo = ts.tzinfo