@@ -299,7 +299,7 @@ def test_regex_replace_dict_nested_gh4115(self):
299
299
300
300
tm .assert_frame_equal (result , expected )
301
301
302
- def test_regex_replace_list_to_scalar (self , mix_abc ):
302
+ def test_regex_replace_list_to_scalar (self , mix_abc , using_infer_string ):
303
303
df = DataFrame (mix_abc )
304
304
expec = DataFrame (
305
305
{
@@ -308,17 +308,20 @@ def test_regex_replace_list_to_scalar(self, mix_abc):
308
308
"c" : [np .nan , np .nan , np .nan , "d" ],
309
309
}
310
310
)
311
+ if using_infer_string :
312
+ expec ["b" ] = expec ["b" ].astype ("str" )
311
313
msg = "Downcasting behavior in `replace`"
312
- with tm .assert_produces_warning (FutureWarning , match = msg ):
314
+ warn = None if using_infer_string else FutureWarning
315
+ with tm .assert_produces_warning (warn , match = msg ):
313
316
res = df .replace ([r"\s*\.\s*" , "a|b" ], np .nan , regex = True )
314
317
res2 = df .copy ()
315
318
res3 = df .copy ()
316
- with tm .assert_produces_warning (FutureWarning , match = msg ):
319
+ with tm .assert_produces_warning (warn , match = msg ):
317
320
return_value = res2 .replace (
318
321
[r"\s*\.\s*" , "a|b" ], np .nan , regex = True , inplace = True
319
322
)
320
323
assert return_value is None
321
- with tm .assert_produces_warning (FutureWarning , match = msg ):
324
+ with tm .assert_produces_warning (warn , match = msg ):
322
325
return_value = res3 .replace (
323
326
regex = [r"\s*\.\s*" , "a|b" ], value = np .nan , inplace = True
324
327
)
@@ -338,8 +341,6 @@ def test_regex_replace_str_to_numeric(self, mix_abc):
338
341
return_value = res3 .replace (regex = r"\s*\.\s*" , value = 0 , inplace = True )
339
342
assert return_value is None
340
343
expec = DataFrame ({"a" : mix_abc ["a" ], "b" : ["a" , "b" , 0 , 0 ], "c" : mix_abc ["c" ]})
341
- # TODO(infer_string)
342
- expec ["c" ] = expec ["c" ].astype (object )
343
344
tm .assert_frame_equal (res , expec )
344
345
tm .assert_frame_equal (res2 , expec )
345
346
tm .assert_frame_equal (res3 , expec )
@@ -626,11 +627,7 @@ def test_replace_mixed2(self, using_infer_string):
626
627
"B" : Series ([0 , "foo" ], dtype = "object" ),
627
628
}
628
629
)
629
- if using_infer_string :
630
- with tm .assert_produces_warning (FutureWarning , match = "Downcasting" ):
631
- result = df .replace ([1 , 2 ], ["foo" , "bar" ])
632
- else :
633
- result = df .replace ([1 , 2 ], ["foo" , "bar" ])
630
+ result = df .replace ([1 , 2 ], ["foo" , "bar" ])
634
631
tm .assert_frame_equal (result , expected )
635
632
636
633
def test_replace_mixed3 (self ):
@@ -1513,13 +1510,11 @@ def test_replace_with_compiled_regex(self):
1513
1510
expected = DataFrame (["z" , "b" , "c" ])
1514
1511
tm .assert_frame_equal (result , expected )
1515
1512
1516
- def test_replace_intervals (self , using_infer_string ):
1513
+ def test_replace_intervals (self ):
1517
1514
# https://github.com/pandas-dev/pandas/issues/35931
1518
1515
df = DataFrame ({"a" : [pd .Interval (0 , 1 ), pd .Interval (0 , 1 )]})
1519
- warning = FutureWarning if using_infer_string else None
1520
- with tm .assert_produces_warning (warning , match = "Downcasting" ):
1521
- result = df .replace ({"a" : {pd .Interval (0 , 1 ): "x" }})
1522
- expected = DataFrame ({"a" : ["x" , "x" ]})
1516
+ result = df .replace ({"a" : {pd .Interval (0 , 1 ): "x" }})
1517
+ expected = DataFrame ({"a" : ["x" , "x" ]}, dtype = object )
1523
1518
tm .assert_frame_equal (result , expected )
1524
1519
1525
1520
def test_replace_unicode (self ):
@@ -1620,7 +1615,7 @@ def test_regex_replace_scalar(
1620
1615
tm .assert_frame_equal (result , expected )
1621
1616
1622
1617
@pytest .mark .parametrize ("regex" , [False , True ])
1623
- def test_replace_regex_dtype_frame (self , regex ):
1618
+ def test_replace_regex_dtype_frame (self , regex , using_infer_string ):
1624
1619
# GH-48644
1625
1620
df1 = DataFrame ({"A" : ["0" ], "B" : ["0" ]})
1626
1621
expected_df1 = DataFrame ({"A" : [1 ], "B" : [1 ]})
0 commit comments