1
1
from datetime import datetime
2
2
3
3
import numpy as np
4
+ import pytest
5
+ import pytz
4
6
5
7
from pandas .core .dtypes .common import (
6
8
is_categorical_dtype ,
17
19
Timestamp ,
18
20
cut ,
19
21
date_range ,
20
- to_datetime ,
21
22
)
22
23
import pandas ._testing as tm
23
24
24
25
25
26
class 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
+ )
33
32
34
33
expected = Series (
35
34
np .array (
@@ -41,49 +40,76 @@ def test_convert_dti_to_series(self):
41
40
),
42
41
name = "B" ,
43
42
)
43
+ assert expected .dtype == idx .dtype
44
+ return idx , expected
44
45
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 ):
54
47
# convert to series while keeping the timezone
48
+ idx , expected = idx_expected
49
+
55
50
msg = "stop passing 'keep_tz'"
56
51
with tm .assert_produces_warning (FutureWarning ) as m :
57
52
result = idx .to_series (keep_tz = True , index = [0 , 1 ])
53
+ assert msg in str (m [0 ].message )
54
+
58
55
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"
59
64
assert msg in str (m [0 ].message )
60
65
66
+ def test_setitem_dt64series (self , idx_expected ):
61
67
# convert to utc
68
+ idx , expected = idx_expected
69
+ df = DataFrame (np .random .randn (2 , 1 ), columns = ["A" ])
70
+ df ["B" ] = idx
71
+
62
72
with tm .assert_produces_warning (FutureWarning ) as m :
63
73
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 )
67
74
msg = "do 'idx.tz_convert(None)' before calling"
68
75
assert msg in str (m [0 ].message )
69
76
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" ]
71
89
tm .assert_series_equal (result , expected )
72
90
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" ])
78
95
79
- # list of datetimes with a tz
96
+ # object array of datetimes with a tz
80
97
df ["B" ] = idx .to_pydatetime ()
81
98
result = df ["B" ]
82
99
tm .assert_series_equal (result , expected )
83
100
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 ):
84
111
# GH 6785
85
112
# set the index manually
86
- import pytz
87
113
88
114
df = DataFrame ([{"ts" : datetime (2014 , 4 , 1 , tzinfo = pytz .utc ), "foo" : 1 }])
89
115
expected = df .set_index ("ts" )
@@ -102,6 +128,7 @@ def test_dti_set_index_reindex(self):
102
128
df = df .reindex (idx2 )
103
129
tm .assert_index_equal (df .index , idx2 )
104
130
131
+ def test_dti_set_index_reindex_with_tz (self ):
105
132
# GH 11314
106
133
# with tz
107
134
index = date_range (
@@ -130,16 +157,16 @@ class TestIntervalIndex:
130
157
def test_setitem (self ):
131
158
132
159
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 )
135
162
136
163
# B & D end up as Categoricals
137
164
# the remainer are converted to in-line objects
138
165
# 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 )
143
170
144
171
assert is_categorical_dtype (df ["B" ].dtype )
145
172
assert is_interval_dtype (df ["B" ].cat .categories )
@@ -152,17 +179,17 @@ def test_setitem(self):
152
179
# they compare equal as Index
153
180
# when converted to numpy objects
154
181
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 ))
156
183
tm .assert_index_equal (c (df .B ), c (df .C ), check_names = False )
157
184
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 )
159
186
160
187
# 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" ])
162
189
tm .assert_series_equal (df ["B" ], df ["D" ], check_names = False )
163
190
164
191
# 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" ])
166
193
tm .assert_series_equal (df ["C" ], df ["E" ], check_names = False )
167
194
168
195
def test_set_reset_index (self ):
0 commit comments