@@ -863,6 +863,7 @@ def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self):
863
863
864
864
class TestTimeZones (tm .TestCase ):
865
865
_multiprocess_can_split_ = True
866
+ timezones = ['UTC' , 'Asia/Tokyo' , 'US/Eastern' , 'dateutil/US/Pacific' ]
866
867
867
868
def setUp (self ):
868
869
tm ._skip_if_no_pytz ()
@@ -882,6 +883,24 @@ def test_tz_localize_naive(self):
882
883
883
884
self .assertTrue (conv .equals (exp ))
884
885
886
+ def test_tz_localize_roundtrip (self ):
887
+ for tz in self .timezones :
888
+ idx1 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'M' )
889
+ idx2 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'D' )
890
+ idx3 = date_range (start = '2014-01-01' , end = '2014-03-01' , freq = 'H' )
891
+ idx4 = date_range (start = '2014-08-01' , end = '2014-10-31' , freq = 'T' )
892
+ for idx in [idx1 , idx2 , idx3 , idx4 ]:
893
+ localized = idx .tz_localize (tz )
894
+ expected = date_range (start = idx [0 ], end = idx [- 1 ], freq = idx .freq , tz = tz )
895
+ tm .assert_index_equal (localized , expected )
896
+
897
+ with tm .assertRaises (TypeError ):
898
+ localized .tz_localize (tz )
899
+
900
+ reset = localized .tz_localize (None )
901
+ tm .assert_index_equal (reset , idx )
902
+ self .assertTrue (reset .tzinfo is None )
903
+
885
904
def test_series_frame_tz_localize (self ):
886
905
887
906
rng = date_range ('1/1/2011' , periods = 100 , freq = 'H' )
@@ -930,6 +949,29 @@ def test_series_frame_tz_convert(self):
930
949
ts = Series (1 , index = rng )
931
950
tm .assertRaisesRegexp (TypeError , "Cannot convert tz-naive" , ts .tz_convert , 'US/Eastern' )
932
951
952
+ def test_tz_convert_roundtrip (self ):
953
+ for tz in self .timezones :
954
+ idx1 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'M' , tz = 'UTC' )
955
+ exp1 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'M' )
956
+
957
+ idx2 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'D' , tz = 'UTC' )
958
+ exp2 = date_range (start = '2014-01-01' , end = '2014-12-31' , freq = 'D' )
959
+
960
+ idx3 = date_range (start = '2014-01-01' , end = '2014-03-01' , freq = 'H' , tz = 'UTC' )
961
+ exp3 = date_range (start = '2014-01-01' , end = '2014-03-01' , freq = 'H' )
962
+
963
+ idx4 = date_range (start = '2014-08-01' , end = '2014-10-31' , freq = 'T' , tz = 'UTC' )
964
+ exp4 = date_range (start = '2014-08-01' , end = '2014-10-31' , freq = 'T' )
965
+
966
+
967
+ for idx , expected in [(idx1 , exp1 ), (idx2 , exp2 ), (idx3 , exp3 ), (idx4 , exp4 )]:
968
+ converted = idx .tz_convert (tz )
969
+ reset = converted .tz_convert (None )
970
+ tm .assert_index_equal (reset , expected )
971
+ self .assertTrue (reset .tzinfo is None )
972
+ tm .assert_index_equal (reset , converted .tz_convert ('UTC' ).tz_localize (None ))
973
+
974
+
933
975
def test_join_utc_convert (self ):
934
976
rng = date_range ('1/1/2011' , periods = 100 , freq = 'H' , tz = 'utc' )
935
977
0 commit comments