11from datetime import datetime
22
33import numpy as np
4+ import pytest
5+ import pytz
46
57from pandas .core .dtypes .common import (
68 is_categorical_dtype ,
1719 Timestamp ,
1820 cut ,
1921 date_range ,
20- to_datetime ,
2122)
2223import pandas ._testing as tm
2324
2425
2526class TestDataFrameAlterAxes :
26- def test_convert_dti_to_series (self ):
27- # don't cast a DatetimeIndex WITH a tz, leave as object
28- # GH 6032
29- idx = DatetimeIndex (
30- to_datetime (["2013-1-1 13:00" , "2013-1-2 14:00" ]), name = "B"
31- ).tz_localize ("US/Pacific" )
32- df = DataFrame (np .random .randn (2 , 1 ), columns = ["A" ])
27+ @pytest .fixture
28+ def idx_expected (self ):
29+ idx = DatetimeIndex (["2013-1-1 13:00" , "2013-1-2 14:00" ], name = "B" ).tz_localize (
30+ "US/Pacific"
31+ )
3332
3433 expected = Series (
3534 np .array (
@@ -41,49 +40,76 @@ def test_convert_dti_to_series(self):
4140 ),
4241 name = "B" ,
4342 )
43+ assert expected .dtype == idx .dtype
44+ return idx , expected
4445
45- # convert index to series
46- result = Series (idx )
47- tm .assert_series_equal (result , expected )
48-
49- # assign to frame
50- df ["B" ] = idx
51- result = df ["B" ]
52- tm .assert_series_equal (result , expected )
53-
46+ def test_to_series_keep_tz_deprecated_true (self , idx_expected ):
5447 # convert to series while keeping the timezone
48+ idx , expected = idx_expected
49+
5550 msg = "stop passing 'keep_tz'"
5651 with tm .assert_produces_warning (FutureWarning ) as m :
5752 result = idx .to_series (keep_tz = True , index = [0 , 1 ])
53+ assert msg in str (m [0 ].message )
54+
5855 tm .assert_series_equal (result , expected )
56+
57+ def test_to_series_keep_tz_deprecated_false (self , idx_expected ):
58+ idx , expected = idx_expected
59+
60+ with tm .assert_produces_warning (FutureWarning ) as m :
61+ result = idx .to_series (keep_tz = False , index = [0 , 1 ])
62+ tm .assert_series_equal (result , expected .dt .tz_convert (None ))
63+ msg = "do 'idx.tz_convert(None)' before calling"
5964 assert msg in str (m [0 ].message )
6065
66+ def test_setitem_dt64series (self , idx_expected ):
6167 # convert to utc
68+ idx , expected = idx_expected
69+ df = DataFrame (np .random .randn (2 , 1 ), columns = ["A" ])
70+ df ["B" ] = idx
71+
6272 with tm .assert_produces_warning (FutureWarning ) as m :
6373 df ["B" ] = idx .to_series (keep_tz = False , index = [0 , 1 ])
64- result = df ["B" ]
65- comp = Series (DatetimeIndex (expected .values ).tz_localize (None ), name = "B" )
66- tm .assert_series_equal (result , comp )
6774 msg = "do 'idx.tz_convert(None)' before calling"
6875 assert msg in str (m [0 ].message )
6976
70- result = idx .to_series (index = [0 , 1 ])
77+ result = df ["B" ]
78+ comp = Series (idx .tz_convert ("UTC" ).tz_localize (None ), name = "B" )
79+ tm .assert_series_equal (result , comp )
80+
81+ def test_setitem_datetimeindex (self , idx_expected ):
82+ # setting a DataFrame column with a tzaware DTI retains the dtype
83+ idx , expected = idx_expected
84+ df = DataFrame (np .random .randn (2 , 1 ), columns = ["A" ])
85+
86+ # assign to frame
87+ df ["B" ] = idx
88+ result = df ["B" ]
7189 tm .assert_series_equal (result , expected )
7290
73- with tm .assert_produces_warning (FutureWarning ) as m :
74- result = idx .to_series (keep_tz = False , index = [0 , 1 ])
75- tm .assert_series_equal (result , expected .dt .tz_convert (None ))
76- msg = "do 'idx.tz_convert(None)' before calling"
77- assert msg in str (m [0 ].message )
91+ def test_setitem_object_array_of_tzaware_datetimes (self , idx_expected ):
92+ # setting a DataFrame column with a tzaware DTI retains the dtype
93+ idx , expected = idx_expected
94+ df = DataFrame (np .random .randn (2 , 1 ), columns = ["A" ])
7895
79- # list of datetimes with a tz
96+ # object array of datetimes with a tz
8097 df ["B" ] = idx .to_pydatetime ()
8198 result = df ["B" ]
8299 tm .assert_series_equal (result , expected )
83100
101+ def test_constructor_from_tzaware_datetimeindex (self , idx_expected ):
102+ # don't cast a DatetimeIndex WITH a tz, leave as object
103+ # GH 6032
104+ idx , expected = idx_expected
105+
106+ # convert index to series
107+ result = Series (idx )
108+ tm .assert_series_equal (result , expected )
109+
110+ def test_set_axis_setattr_index (self ):
84111 # GH 6785
85112 # set the index manually
86- import pytz
87113
88114 df = DataFrame ([{"ts" : datetime (2014 , 4 , 1 , tzinfo = pytz .utc ), "foo" : 1 }])
89115 expected = df .set_index ("ts" )
@@ -102,6 +128,7 @@ def test_dti_set_index_reindex(self):
102128 df = df .reindex (idx2 )
103129 tm .assert_index_equal (df .index , idx2 )
104130
131+ def test_dti_set_index_reindex_with_tz (self ):
105132 # GH 11314
106133 # with tz
107134 index = date_range (
@@ -130,16 +157,16 @@ class TestIntervalIndex:
130157 def test_setitem (self ):
131158
132159 df = DataFrame ({"A" : range (10 )})
133- s = cut (df . A , 5 )
134- assert isinstance (s .cat .categories , IntervalIndex )
160+ ser = cut (df [ "A" ] , 5 )
161+ assert isinstance (ser .cat .categories , IntervalIndex )
135162
136163 # B & D end up as Categoricals
137164 # the remainer are converted to in-line objects
138165 # contining an IntervalIndex.values
139- df ["B" ] = s
140- df ["C" ] = np .array (s )
141- df ["D" ] = s .values
142- df ["E" ] = np .array (s .values )
166+ df ["B" ] = ser
167+ df ["C" ] = np .array (ser )
168+ df ["D" ] = ser .values
169+ df ["E" ] = np .array (ser .values )
143170
144171 assert is_categorical_dtype (df ["B" ].dtype )
145172 assert is_interval_dtype (df ["B" ].cat .categories )
@@ -152,17 +179,17 @@ def test_setitem(self):
152179 # they compare equal as Index
153180 # when converted to numpy objects
154181 c = lambda x : Index (np .array (x ))
155- tm .assert_index_equal (c (df .B ), c (df .B ), check_names = False )
182+ tm .assert_index_equal (c (df .B ), c (df .B ))
156183 tm .assert_index_equal (c (df .B ), c (df .C ), check_names = False )
157184 tm .assert_index_equal (c (df .B ), c (df .D ), check_names = False )
158- tm .assert_index_equal (c (df .B ), c (df .D ), check_names = False )
185+ tm .assert_index_equal (c (df .C ), c (df .D ), check_names = False )
159186
160187 # B & D are the same Series
161- tm .assert_series_equal (df ["B" ], df ["B" ], check_names = False )
188+ tm .assert_series_equal (df ["B" ], df ["B" ])
162189 tm .assert_series_equal (df ["B" ], df ["D" ], check_names = False )
163190
164191 # C & E are the same Series
165- tm .assert_series_equal (df ["C" ], df ["C" ], check_names = False )
192+ tm .assert_series_equal (df ["C" ], df ["C" ])
166193 tm .assert_series_equal (df ["C" ], df ["E" ], check_names = False )
167194
168195 def test_set_reset_index (self ):
0 commit comments