@@ -184,3 +184,55 @@ def test_update_dt_column_with_NaT_create_column(self):
184
184
{"A" : [1.0 , 3.0 ], "B" : [pd .NaT , pd .to_datetime ("2016-01-01" )]}
185
185
)
186
186
tm .assert_frame_equal (df , expected )
187
+
188
+ @pytest .mark .parametrize (
189
+ "value_df, value_other, dtype" ,
190
+ [
191
+ (True , False , bool ),
192
+ (1 , 2 , int ),
193
+ (1.0 , 2.0 , float ),
194
+ (1.0 + 1j , 2.0 + 2j , complex ),
195
+ (np .uint64 (1 ), np .uint (2 ), np .dtype ("ubyte" )),
196
+ (np .uint64 (1 ), np .uint (2 ), np .dtype ("intc" )),
197
+ ("a" , "b" , pd .StringDtype ()),
198
+ (
199
+ pd .to_timedelta ("1 ms" ),
200
+ pd .to_timedelta ("2 ms" ),
201
+ np .dtype ("timedelta64[ns]" ),
202
+ ),
203
+ (
204
+ np .datetime64 ("2000-01-01T00:00:00" ),
205
+ np .datetime64 ("2000-01-02T00:00:00" ),
206
+ np .dtype ("datetime64[ns]" ),
207
+ ),
208
+ ],
209
+ )
210
+ def test_update_preserve_dtype (self , value_df , value_other , dtype ):
211
+ # GH#55509
212
+ df = DataFrame ({"a" : [value_df ] * 2 }, index = [1 , 2 ], dtype = dtype )
213
+ other = DataFrame ({"a" : [value_other ]}, index = [1 ], dtype = dtype )
214
+ expected = DataFrame ({"a" : [value_other , value_df ]}, index = [1 , 2 ], dtype = dtype )
215
+ df .update (other )
216
+ tm .assert_frame_equal (df , expected )
217
+
218
+ def test_update_raises_on_duplicate_argument_index (self ):
219
+ # GH#55509
220
+ df = DataFrame ({"a" : [1 , 1 ]}, index = [1 , 2 ])
221
+ other = DataFrame ({"a" : [2 , 3 ]}, index = [1 , 1 ])
222
+ with pytest .raises (ValueError , match = "duplicate index" ):
223
+ df .update (other )
224
+
225
+ def test_update_raises_without_intersection (self ):
226
+ # GH#55509
227
+ df = DataFrame ({"a" : [1 ]}, index = [1 ])
228
+ other = DataFrame ({"a" : [2 ]}, index = [2 ])
229
+ with pytest .raises (ValueError , match = "no intersection" ):
230
+ df .update (other )
231
+
232
+ def test_update_on_duplicate_frame_unique_argument_index (self ):
233
+ # GH#55509
234
+ df = DataFrame ({"a" : [1 , 1 , 1 ]}, index = [1 , 1 , 2 ], dtype = np .dtype ("intc" ))
235
+ other = DataFrame ({"a" : [2 , 3 ]}, index = [1 , 2 ], dtype = np .dtype ("intc" ))
236
+ expected = DataFrame ({"a" : [2 , 2 , 3 ]}, index = [1 , 1 , 2 ], dtype = np .dtype ("intc" ))
237
+ df .update (other )
238
+ tm .assert_frame_equal (df , expected )
0 commit comments