Skip to content

Commit

Permalink
Small tweaks to PR #2785
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Feb 9, 2013
1 parent 9d58bbe commit eb505fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 15 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

1 comment on commit eb505fd

@stephenwlin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@changhiskhan hey, just curious, why is catching the exception preferred over checking for an attribute? I only changed that because there was the if (hasattr(tz, 'normalize') line above, and I thought it would be more consistent this way. Definitely let me know if there's something I'm missing though...

Please sign in to comment.