File tree 2 files changed +18
-4
lines changed
2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,11 @@ def _skip_if_no_scipy():
34
34
except ImportError :
35
35
raise nose .SkipTest
36
36
37
+ def _skip_if_no_pytz ():
38
+ try :
39
+ import pytz
40
+ except ImportError :
41
+ raise nose .SkipTest
37
42
38
43
#-------------------------------------------------------------------------------
39
44
# Series test cases
@@ -2545,7 +2550,6 @@ def test_asof(self):
2545
2550
self .assert_ (np .isnan (self .ts .asof (d )))
2546
2551
2547
2552
def test_getitem_setitem_datetimeindex (self ):
2548
- from pytz import timezone as tz
2549
2553
from pandas import date_range
2550
2554
N = 50
2551
2555
# testing with timezone, GH #2785
@@ -2624,6 +2628,16 @@ def test_getitem_setitem_datetimeindex(self):
2624
2628
result ["1990-01-02" ] = ts [24 :48 ]
2625
2629
assert_series_equal (result , ts )
2626
2630
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
+
2627
2641
# also test Timestamp tz handling, GH #2789
2628
2642
result = ts .copy ()
2629
2643
result ["1990-01-01 09:00:00+00:00" ] = 0
Original file line number Diff line number Diff line change @@ -617,7 +617,7 @@ cdef convert_to_tsobject(object ts, object tz):
617
617
# sort of a temporary hack
618
618
if ts.tzinfo is not None :
619
619
if (hasattr (tz, ' normalize' ) and
620
- hasattr (ts.tzinfo, ' _utcoffset' )):
620
+ hasattr (ts.tzinfo, ' _utcoffset' )):
621
621
ts = tz.normalize(ts)
622
622
obj.value = _pydatetime_to_dts(ts, & obj.dts)
623
623
obj.tzinfo = ts.tzinfo
@@ -631,9 +631,9 @@ cdef convert_to_tsobject(object ts, object tz):
631
631
PANDAS_FR_ns, & obj.dts)
632
632
obj.tzinfo = tz
633
633
elif not _is_utc(tz):
634
- if ( hasattr (tz, ' localize ' )) :
634
+ try :
635
635
ts = tz.localize(ts)
636
- else :
636
+ except AttributeError :
637
637
ts = ts.replace(tzinfo = tz)
638
638
obj.value = _pydatetime_to_dts(ts, & obj.dts)
639
639
obj.tzinfo = ts.tzinfo
You can’t perform that action at this time.
0 commit comments