6
6
import numpy as np
7
7
import pytest
8
8
9
- from pandas ._config import using_string_dtype
10
-
11
9
import pandas as pd
12
10
from pandas import (
13
11
DataFrame ,
@@ -30,7 +28,6 @@ def mix_abc() -> dict[str, list[float | str]]:
30
28
31
29
32
30
class TestDataFrameReplace :
33
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
34
31
def test_replace_inplace (self , datetime_frame , float_string_frame ):
35
32
datetime_frame .loc [datetime_frame .index [:5 ], "A" ] = np .nan
36
33
datetime_frame .loc [datetime_frame .index [- 5 :], "A" ] = np .nan
@@ -46,7 +43,9 @@ def test_replace_inplace(self, datetime_frame, float_string_frame):
46
43
mf .iloc [- 10 :, mf .columns .get_loc ("A" )] = np .nan
47
44
48
45
result = float_string_frame .replace (np .nan , 0 )
49
- expected = float_string_frame .fillna (value = 0 )
46
+ expected = float_string_frame .copy ()
47
+ expected ["foo" ] = expected ["foo" ].astype (object )
48
+ expected = expected .fillna (value = 0 )
50
49
tm .assert_frame_equal (result , expected )
51
50
52
51
tsframe = datetime_frame .copy ()
@@ -290,34 +289,39 @@ def test_regex_replace_dict_nested_non_first_character(self, any_string_dtype):
290
289
tm .assert_frame_equal (result , expected )
291
290
292
291
def test_regex_replace_dict_nested_gh4115 (self ):
293
- df = DataFrame ({"Type" : ["Q" , "T" , "Q" , "Q" , "T" ], "tmp" : 2 })
292
+ df = DataFrame (
293
+ {"Type" : Series (["Q" , "T" , "Q" , "Q" , "T" ], dtype = object ), "tmp" : 2 }
294
+ )
294
295
expected = DataFrame ({"Type" : [0 , 1 , 0 , 0 , 1 ], "tmp" : 2 })
295
296
msg = "Downcasting behavior in `replace`"
296
297
with tm .assert_produces_warning (FutureWarning , match = msg ):
297
298
result = df .replace ({"Type" : {"Q" : 0 , "T" : 1 }})
299
+
298
300
tm .assert_frame_equal (result , expected )
299
301
300
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
301
- def test_regex_replace_list_to_scalar (self , mix_abc ):
302
+ def test_regex_replace_list_to_scalar (self , mix_abc , using_infer_string ):
302
303
df = DataFrame (mix_abc )
303
304
expec = DataFrame (
304
305
{
305
306
"a" : mix_abc ["a" ],
306
- "b" : np . array ( [np .nan ] * 4 ) ,
307
+ "b" : [np .nan ] * 4 ,
307
308
"c" : [np .nan , np .nan , np .nan , "d" ],
308
309
}
309
310
)
311
+ if using_infer_string :
312
+ expec ["b" ] = expec ["b" ].astype ("str" )
310
313
msg = "Downcasting behavior in `replace`"
311
- 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 ):
312
316
res = df .replace ([r"\s*\.\s*" , "a|b" ], np .nan , regex = True )
313
317
res2 = df .copy ()
314
318
res3 = df .copy ()
315
- with tm .assert_produces_warning (FutureWarning , match = msg ):
319
+ with tm .assert_produces_warning (warn , match = msg ):
316
320
return_value = res2 .replace (
317
321
[r"\s*\.\s*" , "a|b" ], np .nan , regex = True , inplace = True
318
322
)
319
323
assert return_value is None
320
- with tm .assert_produces_warning (FutureWarning , match = msg ):
324
+ with tm .assert_produces_warning (warn , match = msg ):
321
325
return_value = res3 .replace (
322
326
regex = [r"\s*\.\s*" , "a|b" ], value = np .nan , inplace = True
323
327
)
@@ -326,7 +330,6 @@ def test_regex_replace_list_to_scalar(self, mix_abc):
326
330
tm .assert_frame_equal (res2 , expec )
327
331
tm .assert_frame_equal (res3 , expec )
328
332
329
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
330
333
def test_regex_replace_str_to_numeric (self , mix_abc ):
331
334
# what happens when you try to replace a numeric value with a regex?
332
335
df = DataFrame (mix_abc )
@@ -342,7 +345,6 @@ def test_regex_replace_str_to_numeric(self, mix_abc):
342
345
tm .assert_frame_equal (res2 , expec )
343
346
tm .assert_frame_equal (res3 , expec )
344
347
345
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
346
348
def test_regex_replace_regex_list_to_numeric (self , mix_abc ):
347
349
df = DataFrame (mix_abc )
348
350
res = df .replace ([r"\s*\.\s*" , "b" ], 0 , regex = True )
@@ -539,21 +541,28 @@ def test_replace_convert(self):
539
541
res = rep .dtypes
540
542
tm .assert_series_equal (expec , res )
541
543
542
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
543
544
def test_replace_mixed (self , float_string_frame ):
544
545
mf = float_string_frame
545
546
mf .iloc [5 :20 , mf .columns .get_loc ("foo" )] = np .nan
546
547
mf .iloc [- 10 :, mf .columns .get_loc ("A" )] = np .nan
547
548
548
549
result = float_string_frame .replace (np .nan , - 18 )
549
- expected = float_string_frame .fillna (value = - 18 )
550
+ expected = float_string_frame .copy ()
551
+ expected ["foo" ] = expected ["foo" ].astype (object )
552
+ expected = expected .fillna (value = - 18 )
550
553
tm .assert_frame_equal (result , expected )
551
- tm .assert_frame_equal (result .replace (- 18 , np .nan ), float_string_frame )
554
+ expected2 = float_string_frame .copy ()
555
+ expected2 ["foo" ] = expected2 ["foo" ].astype (object )
556
+ tm .assert_frame_equal (result .replace (- 18 , np .nan ), expected2 )
552
557
553
558
result = float_string_frame .replace (np .nan , - 1e8 )
554
- expected = float_string_frame .fillna (value = - 1e8 )
559
+ expected = float_string_frame .copy ()
560
+ expected ["foo" ] = expected ["foo" ].astype (object )
561
+ expected = expected .fillna (value = - 1e8 )
555
562
tm .assert_frame_equal (result , expected )
556
- tm .assert_frame_equal (result .replace (- 1e8 , np .nan ), float_string_frame )
563
+ expected2 = float_string_frame .copy ()
564
+ expected2 ["foo" ] = expected2 ["foo" ].astype (object )
565
+ tm .assert_frame_equal (result .replace (- 1e8 , np .nan ), expected2 )
557
566
558
567
def test_replace_mixed_int_block_upcasting (self ):
559
568
# int block upcasting
@@ -614,15 +623,11 @@ def test_replace_mixed2(self, using_infer_string):
614
623
615
624
expected = DataFrame (
616
625
{
617
- "A" : Series (["foo" , "bar" ]),
626
+ "A" : Series (["foo" , "bar" ], dtype = "object" ),
618
627
"B" : Series ([0 , "foo" ], dtype = "object" ),
619
628
}
620
629
)
621
- if using_infer_string :
622
- with tm .assert_produces_warning (FutureWarning , match = "Downcasting" ):
623
- result = df .replace ([1 , 2 ], ["foo" , "bar" ])
624
- else :
625
- result = df .replace ([1 , 2 ], ["foo" , "bar" ])
630
+ result = df .replace ([1 , 2 ], ["foo" , "bar" ])
626
631
tm .assert_frame_equal (result , expected )
627
632
628
633
def test_replace_mixed3 (self ):
@@ -931,15 +936,16 @@ def test_replace_limit(self):
931
936
# TODO
932
937
pass
933
938
934
- def test_replace_dict_no_regex (self ):
939
+ def test_replace_dict_no_regex (self , any_string_dtype ):
935
940
answer = Series (
936
941
{
937
942
0 : "Strongly Agree" ,
938
943
1 : "Agree" ,
939
944
2 : "Neutral" ,
940
945
3 : "Disagree" ,
941
946
4 : "Strongly Disagree" ,
942
- }
947
+ },
948
+ dtype = any_string_dtype ,
943
949
)
944
950
weights = {
945
951
"Agree" : 4 ,
@@ -954,15 +960,16 @@ def test_replace_dict_no_regex(self):
954
960
result = answer .replace (weights )
955
961
tm .assert_series_equal (result , expected )
956
962
957
- def test_replace_series_no_regex (self ):
963
+ def test_replace_series_no_regex (self , any_string_dtype ):
958
964
answer = Series (
959
965
{
960
966
0 : "Strongly Agree" ,
961
967
1 : "Agree" ,
962
968
2 : "Neutral" ,
963
969
3 : "Disagree" ,
964
970
4 : "Strongly Disagree" ,
965
- }
971
+ },
972
+ dtype = any_string_dtype ,
966
973
)
967
974
weights = Series (
968
975
{
@@ -1060,16 +1067,15 @@ def test_nested_dict_overlapping_keys_replace_str(self):
1060
1067
expected = df .replace ({"a" : dict (zip (astr , bstr ))})
1061
1068
tm .assert_frame_equal (result , expected )
1062
1069
1063
- @pytest .mark .xfail (using_string_dtype (), reason = "can't set float into string" )
1064
- def test_replace_swapping_bug (self , using_infer_string ):
1070
+ def test_replace_swapping_bug (self ):
1065
1071
df = DataFrame ({"a" : [True , False , True ]})
1066
1072
res = df .replace ({"a" : {True : "Y" , False : "N" }})
1067
- expect = DataFrame ({"a" : ["Y" , "N" , "Y" ]})
1073
+ expect = DataFrame ({"a" : ["Y" , "N" , "Y" ]}, dtype = object )
1068
1074
tm .assert_frame_equal (res , expect )
1069
1075
1070
1076
df = DataFrame ({"a" : [0 , 1 , 0 ]})
1071
1077
res = df .replace ({"a" : {0 : "Y" , 1 : "N" }})
1072
- expect = DataFrame ({"a" : ["Y" , "N" , "Y" ]})
1078
+ expect = DataFrame ({"a" : ["Y" , "N" , "Y" ]}, dtype = object )
1073
1079
tm .assert_frame_equal (res , expect )
1074
1080
1075
1081
def test_replace_period (self ):
@@ -1345,7 +1351,7 @@ def test_replace_commutative(self, df, to_replace, exp):
1345
1351
)
1346
1352
def test_replace_replacer_dtype (self , replacer ):
1347
1353
# GH26632
1348
- df = DataFrame (["a" ])
1354
+ df = DataFrame (["a" ], dtype = object )
1349
1355
msg = "Downcasting behavior in `replace` "
1350
1356
with tm .assert_produces_warning (FutureWarning , match = msg ):
1351
1357
result = df .replace ({"a" : replacer , "b" : replacer })
@@ -1462,6 +1468,7 @@ def test_replace_value_category_type(self):
1462
1468
input_df = input_df .replace ("obj1" , "obj9" )
1463
1469
result = input_df .replace ("cat2" , "catX" )
1464
1470
1471
+ result = result .astype ({"col1" : "int64" , "col3" : "float64" , "col5" : "str" })
1465
1472
tm .assert_frame_equal (result , expected )
1466
1473
1467
1474
def test_replace_dict_category_type (self ):
@@ -1503,13 +1510,11 @@ def test_replace_with_compiled_regex(self):
1503
1510
expected = DataFrame (["z" , "b" , "c" ])
1504
1511
tm .assert_frame_equal (result , expected )
1505
1512
1506
- def test_replace_intervals (self , using_infer_string ):
1513
+ def test_replace_intervals (self ):
1507
1514
# https://github.com/pandas-dev/pandas/issues/35931
1508
1515
df = DataFrame ({"a" : [pd .Interval (0 , 1 ), pd .Interval (0 , 1 )]})
1509
- warning = FutureWarning if using_infer_string else None
1510
- with tm .assert_produces_warning (warning , match = "Downcasting" ):
1511
- result = df .replace ({"a" : {pd .Interval (0 , 1 ): "x" }})
1512
- expected = DataFrame ({"a" : ["x" , "x" ]})
1516
+ result = df .replace ({"a" : {pd .Interval (0 , 1 ): "x" }})
1517
+ expected = DataFrame ({"a" : ["x" , "x" ]}, dtype = object )
1513
1518
tm .assert_frame_equal (result , expected )
1514
1519
1515
1520
def test_replace_unicode (self ):
0 commit comments