@@ -21,9 +21,11 @@ def test_insert_nat(self, tz, null):
21
21
@pytest .mark .parametrize ("tz" , [None , "UTC" , "US/Eastern" ])
22
22
def test_insert_invalid_na (self , tz ):
23
23
idx = DatetimeIndex (["2017-01-01" ], tz = tz )
24
- msg = "value should be a 'Timestamp' or 'NaT'. Got 'timedelta64' instead."
25
- with pytest .raises (TypeError , match = msg ):
26
- idx .insert (0 , np .timedelta64 ("NaT" ))
24
+
25
+ item = np .timedelta64 ("NaT" )
26
+ result = idx .insert (0 , item )
27
+ expected = Index ([item ] + list (idx ), dtype = object )
28
+ tm .assert_index_equal (result , expected )
27
29
28
30
def test_insert_empty_preserves_freq (self , tz_naive_fixture ):
29
31
# GH#33573
@@ -114,17 +116,6 @@ def test_insert(self):
114
116
assert result .name == expected .name
115
117
assert result .freq is None
116
118
117
- # see gh-7299
118
- idx = date_range ("1/1/2000" , periods = 3 , freq = "D" , tz = "Asia/Tokyo" , name = "idx" )
119
- with pytest .raises (TypeError , match = "Cannot compare tz-naive and tz-aware" ):
120
- idx .insert (3 , Timestamp ("2000-01-04" ))
121
- with pytest .raises (TypeError , match = "Cannot compare tz-naive and tz-aware" ):
122
- idx .insert (3 , datetime (2000 , 1 , 4 ))
123
- with pytest .raises (ValueError , match = "Timezones don't match" ):
124
- idx .insert (3 , Timestamp ("2000-01-04" , tz = "US/Eastern" ))
125
- with pytest .raises (ValueError , match = "Timezones don't match" ):
126
- idx .insert (3 , datetime (2000 , 1 , 4 , tzinfo = pytz .timezone ("US/Eastern" )))
127
-
128
119
for tz in ["US/Pacific" , "Asia/Singapore" ]:
129
120
idx = date_range ("1/1/2000 09:00" , periods = 6 , freq = "H" , tz = tz , name = "idx" )
130
121
# preserve freq
@@ -167,6 +158,48 @@ def test_insert(self):
167
158
assert result .tz == expected .tz
168
159
assert result .freq is None
169
160
161
+ # TODO: also changes DataFrame.__setitem__ with expansion
162
+ def test_insert_mismatched_tzawareness (self ):
163
+ # see GH#7299
164
+ idx = date_range ("1/1/2000" , periods = 3 , freq = "D" , tz = "Asia/Tokyo" , name = "idx" )
165
+
166
+ # mismatched tz-awareness
167
+ item = Timestamp ("2000-01-04" )
168
+ result = idx .insert (3 , item )
169
+ expected = Index (
170
+ list (idx [:3 ]) + [item ] + list (idx [3 :]), dtype = object , name = "idx"
171
+ )
172
+ tm .assert_index_equal (result , expected )
173
+
174
+ # mismatched tz-awareness
175
+ item = datetime (2000 , 1 , 4 )
176
+ result = idx .insert (3 , item )
177
+ expected = Index (
178
+ list (idx [:3 ]) + [item ] + list (idx [3 :]), dtype = object , name = "idx"
179
+ )
180
+ tm .assert_index_equal (result , expected )
181
+
182
+ # TODO: also changes DataFrame.__setitem__ with expansion
183
+ def test_insert_mismatched_tz (self ):
184
+ # see GH#7299
185
+ idx = date_range ("1/1/2000" , periods = 3 , freq = "D" , tz = "Asia/Tokyo" , name = "idx" )
186
+
187
+ # mismatched tz -> cast to object (could reasonably cast to same tz or UTC)
188
+ item = Timestamp ("2000-01-04" , tz = "US/Eastern" )
189
+ result = idx .insert (3 , item )
190
+ expected = Index (
191
+ list (idx [:3 ]) + [item ] + list (idx [3 :]), dtype = object , name = "idx"
192
+ )
193
+ tm .assert_index_equal (result , expected )
194
+
195
+ # mismatched tz -> cast to object (could reasonably cast to same tz)
196
+ item = datetime (2000 , 1 , 4 , tzinfo = pytz .timezone ("US/Eastern" ))
197
+ result = idx .insert (3 , item )
198
+ expected = Index (
199
+ list (idx [:3 ]) + [item ] + list (idx [3 :]), dtype = object , name = "idx"
200
+ )
201
+ tm .assert_index_equal (result , expected )
202
+
170
203
@pytest .mark .parametrize (
171
204
"item" , [0 , np .int64 (0 ), np .float64 (0 ), np .array (0 ), np .timedelta64 (456 )]
172
205
)
@@ -175,17 +208,36 @@ def test_insert_mismatched_types_raises(self, tz_aware_fixture, item):
175
208
tz = tz_aware_fixture
176
209
dti = date_range ("2019-11-04" , periods = 9 , freq = "-1D" , name = 9 , tz = tz )
177
210
178
- msg = "value should be a 'Timestamp' or 'NaT'. Got '.*' instead"
179
- with pytest .raises (TypeError , match = msg ):
180
- dti .insert (1 , item )
211
+ result = dti .insert (1 , item )
212
+
213
+ if isinstance (item , np .ndarray ):
214
+ # FIXME: without doing .item() here this segfaults
215
+ assert item .item () == 0
216
+ expected = Index ([dti [0 ], 0 ] + list (dti [1 :]), dtype = object , name = 9 )
217
+ else :
218
+ expected = Index ([dti [0 ], item ] + list (dti [1 :]), dtype = object , name = 9 )
219
+
220
+ tm .assert_index_equal (result , expected )
181
221
182
- def test_insert_object_casting (self , tz_aware_fixture ):
222
+ def test_insert_castable_str (self , tz_aware_fixture ):
183
223
# GH#33703
184
224
tz = tz_aware_fixture
185
225
dti = date_range ("2019-11-04" , periods = 3 , freq = "-1D" , name = 9 , tz = tz )
186
226
187
- # ATM we treat this as a string, but we could plausibly wrap it in Timestamp
188
227
value = "2019-11-05"
189
228
result = dti .insert (0 , value )
190
- expected = Index (["2019-11-05" ] + list (dti ), dtype = object , name = 9 )
229
+
230
+ ts = Timestamp (value ).tz_localize (tz )
231
+ expected = DatetimeIndex ([ts ] + list (dti ), dtype = dti .dtype , name = 9 )
232
+ tm .assert_index_equal (result , expected )
233
+
234
+ def test_insert_non_castable_str (self , tz_aware_fixture ):
235
+ # GH#33703
236
+ tz = tz_aware_fixture
237
+ dti = date_range ("2019-11-04" , periods = 3 , freq = "-1D" , name = 9 , tz = tz )
238
+
239
+ value = "foo"
240
+ result = dti .insert (0 , value )
241
+
242
+ expected = Index (["foo" ] + list (dti ), dtype = object , name = 9 )
191
243
tm .assert_index_equal (result , expected )
0 commit comments