1
1
import numpy as np
2
2
import pytest
3
3
4
- from pandas ._config import using_string_dtype
5
-
6
4
from pandas import (
7
5
Categorical ,
8
6
DataFrame ,
@@ -65,15 +63,20 @@ def test_fillna_datetime(self, datetime_frame):
65
63
with pytest .raises (TypeError , match = msg ):
66
64
datetime_frame .fillna ()
67
65
68
- # TODO(infer_string) test as actual error instead of xfail
69
- @pytest .mark .xfail (using_string_dtype (), reason = "can't fill 0 in string" )
70
- def test_fillna_mixed_type (self , float_string_frame ):
66
+ def test_fillna_mixed_type (self , float_string_frame , using_infer_string ):
71
67
mf = float_string_frame
72
68
mf .loc [mf .index [5 :20 ], "foo" ] = np .nan
73
69
mf .loc [mf .index [- 10 :], "A" ] = np .nan
74
- # TODO: make stronger assertion here, GH 25640
75
- mf .fillna (value = 0 )
76
- mf .ffill ()
70
+
71
+ result = mf .ffill ()
72
+ assert (
73
+ result .loc [result .index [- 10 :], "A" ] == result .loc [result .index [- 11 ], "A" ]
74
+ ).all ()
75
+ assert (result .loc [result .index [5 :20 ], "foo" ] == "bar" ).all ()
76
+
77
+ result = mf .fillna (value = 0 )
78
+ assert (result .loc [result .index [- 10 :], "A" ] == 0 ).all ()
79
+ assert (result .loc [result .index [5 :20 ], "foo" ] == 0 ).all ()
77
80
78
81
def test_fillna_mixed_float (self , mixed_float_frame ):
79
82
# mixed numeric (but no float16)
@@ -84,28 +87,21 @@ def test_fillna_mixed_float(self, mixed_float_frame):
84
87
result = mf .ffill ()
85
88
_check_mixed_float (result , dtype = {"C" : None })
86
89
87
- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" )
88
- def test_fillna_different_dtype (self , using_infer_string ):
90
+ def test_fillna_different_dtype (self ):
89
91
# with different dtype (GH#3386)
90
92
df = DataFrame (
91
93
[["a" , "a" , np .nan , "a" ], ["b" , "b" , np .nan , "b" ], ["c" , "c" , np .nan , "c" ]]
92
94
)
93
95
94
- if using_infer_string :
95
- with tm .assert_produces_warning (FutureWarning , match = "Downcasting" ):
96
- result = df .fillna ({2 : "foo" })
97
- else :
98
- result = df .fillna ({2 : "foo" })
96
+ result = df .fillna ({2 : "foo" })
99
97
expected = DataFrame (
100
98
[["a" , "a" , "foo" , "a" ], ["b" , "b" , "foo" , "b" ], ["c" , "c" , "foo" , "c" ]]
101
99
)
100
+ # column is originally float (all-NaN) -> filling with string gives object dtype
101
+ expected [2 ] = expected [2 ].astype ("object" )
102
102
tm .assert_frame_equal (result , expected )
103
103
104
- if using_infer_string :
105
- with tm .assert_produces_warning (FutureWarning , match = "Downcasting" ):
106
- return_value = df .fillna ({2 : "foo" }, inplace = True )
107
- else :
108
- return_value = df .fillna ({2 : "foo" }, inplace = True )
104
+ return_value = df .fillna ({2 : "foo" }, inplace = True )
109
105
tm .assert_frame_equal (df , expected )
110
106
assert return_value is None
111
107
@@ -276,8 +272,7 @@ def test_fillna_dictlike_value_duplicate_colnames(self, columns):
276
272
expected ["A" ] = 0.0
277
273
tm .assert_frame_equal (result , expected )
278
274
279
- @pytest .mark .xfail (using_string_dtype (), reason = "TODO(infer_string)" )
280
- def test_fillna_dtype_conversion (self , using_infer_string ):
275
+ def test_fillna_dtype_conversion (self ):
281
276
# make sure that fillna on an empty frame works
282
277
df = DataFrame (index = ["A" , "B" , "C" ], columns = [1 , 2 , 3 , 4 , 5 ])
283
278
result = df .dtypes
@@ -292,7 +287,7 @@ def test_fillna_dtype_conversion(self, using_infer_string):
292
287
# empty block
293
288
df = DataFrame (index = range (3 ), columns = ["A" , "B" ], dtype = "float64" )
294
289
result = df .fillna ("nan" )
295
- expected = DataFrame ("nan" , index = range (3 ), columns = ["A" , "B" ])
290
+ expected = DataFrame ("nan" , dtype = "object" , index = range (3 ), columns = ["A" , "B" ])
296
291
tm .assert_frame_equal (result , expected )
297
292
298
293
@pytest .mark .parametrize ("val" , ["" , 1 , np .nan , 1.0 ])
@@ -540,18 +535,10 @@ def test_fillna_col_reordering(self):
540
535
filled = df .ffill ()
541
536
assert df .columns .tolist () == filled .columns .tolist ()
542
537
543
- # TODO(infer_string) test as actual error instead of xfail
544
- @pytest .mark .xfail (using_string_dtype (), reason = "can't fill 0 in string" )
545
- def test_fill_corner (self , float_frame , float_string_frame ):
546
- mf = float_string_frame
547
- mf .loc [mf .index [5 :20 ], "foo" ] = np .nan
548
- mf .loc [mf .index [- 10 :], "A" ] = np .nan
549
-
550
- filled = float_string_frame .fillna (value = 0 )
551
- assert (filled .loc [filled .index [5 :20 ], "foo" ] == 0 ).all ()
552
- del float_string_frame ["foo" ]
553
-
554
- float_frame .reindex (columns = []).fillna (value = 0 )
538
+ def test_fill_empty (self , float_frame ):
539
+ df = float_frame .reindex (columns = [])
540
+ result = df .fillna (value = 0 )
541
+ tm .assert_frame_equal (result , df )
555
542
556
543
def test_fillna_with_columns_and_limit (self ):
557
544
# GH40989
0 commit comments