14
14
15
15
import pandas as pd
16
16
from pandas import Categorical , DataFrame , Series , Timestamp , date_range
17
- from pandas .tests .frame .common import TestData , _check_mixed_float
17
+ from pandas .tests .frame .common import _check_mixed_float
18
18
import pandas .util .testing as tm
19
19
from pandas .util .testing import assert_frame_equal , assert_series_equal
20
20
@@ -34,15 +34,15 @@ def _skip_if_no_pchip():
34
34
pytest .skip ('scipy.interpolate.pchip missing' )
35
35
36
36
37
- class TestDataFrameMissingData (TestData ):
37
+ class TestDataFrameMissingData ():
38
38
39
- def test_dropEmptyRows (self ):
40
- N = len (self . frame .index )
39
+ def test_dropEmptyRows (self , float_frame ):
40
+ N = len (float_frame .index )
41
41
mat = np .random .randn (N )
42
42
mat [:5 ] = np .nan
43
43
44
- frame = DataFrame ({'foo' : mat }, index = self . frame .index )
45
- original = Series (mat , index = self . frame .index , name = 'foo' )
44
+ frame = DataFrame ({'foo' : mat }, index = float_frame .index )
45
+ original = Series (mat , index = float_frame .index , name = 'foo' )
46
46
expected = original .dropna ()
47
47
inplace_frame1 , inplace_frame2 = frame .copy (), frame .copy ()
48
48
@@ -58,30 +58,30 @@ def test_dropEmptyRows(self):
58
58
assert_series_equal (smaller_frame ['foo' ], expected )
59
59
assert_series_equal (inplace_frame2 ['foo' ], expected )
60
60
61
- def test_dropIncompleteRows (self ):
62
- N = len (self . frame .index )
61
+ def test_dropIncompleteRows (self , float_frame ):
62
+ N = len (float_frame .index )
63
63
mat = np .random .randn (N )
64
64
mat [:5 ] = np .nan
65
65
66
- frame = DataFrame ({'foo' : mat }, index = self . frame .index )
66
+ frame = DataFrame ({'foo' : mat }, index = float_frame .index )
67
67
frame ['bar' ] = 5
68
- original = Series (mat , index = self . frame .index , name = 'foo' )
68
+ original = Series (mat , index = float_frame .index , name = 'foo' )
69
69
inp_frame1 , inp_frame2 = frame .copy (), frame .copy ()
70
70
71
71
smaller_frame = frame .dropna ()
72
72
assert_series_equal (frame ['foo' ], original )
73
73
inp_frame1 .dropna (inplace = True )
74
74
75
- exp = Series (mat [5 :], index = self . frame .index [5 :], name = 'foo' )
75
+ exp = Series (mat [5 :], index = float_frame .index [5 :], name = 'foo' )
76
76
tm .assert_series_equal (smaller_frame ['foo' ], exp )
77
77
tm .assert_series_equal (inp_frame1 ['foo' ], exp )
78
78
79
79
samesize_frame = frame .dropna (subset = ['bar' ])
80
80
assert_series_equal (frame ['foo' ], original )
81
81
assert (frame ['bar' ] == 5 ).all ()
82
82
inp_frame2 .dropna (subset = ['bar' ], inplace = True )
83
- tm .assert_index_equal (samesize_frame .index , self . frame .index )
84
- tm .assert_index_equal (inp_frame2 .index , self . frame .index )
83
+ tm .assert_index_equal (samesize_frame .index , float_frame .index )
84
+ tm .assert_index_equal (inp_frame2 .index , float_frame .index )
85
85
86
86
@pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
87
87
def test_dropna (self ):
@@ -160,17 +160,17 @@ def test_drop_and_dropna_caching(self):
160
160
df2 ['A' ].drop ([1 ], inplace = True )
161
161
assert_series_equal (df2 ['A' ], original .drop ([1 ]))
162
162
163
- def test_dropna_corner (self ):
163
+ def test_dropna_corner (self , float_frame ):
164
164
# bad input
165
165
msg = "invalid how option: foo"
166
166
with pytest .raises (ValueError , match = msg ):
167
- self . frame .dropna (how = 'foo' )
167
+ float_frame .dropna (how = 'foo' )
168
168
msg = "must specify how or thresh"
169
169
with pytest .raises (TypeError , match = msg ):
170
- self . frame .dropna (how = None )
170
+ float_frame .dropna (how = None )
171
171
# non-existent column - 8303
172
172
with pytest .raises (KeyError , match = r"^\['X'\]$" ):
173
- self . frame .dropna (subset = ['A' , 'X' ])
173
+ float_frame .dropna (subset = ['A' , 'X' ])
174
174
175
175
def test_dropna_multiple_axes (self ):
176
176
df = DataFrame ([[1 , np .nan , 2 , 3 ],
@@ -215,42 +215,47 @@ def test_dropna_tz_aware_datetime(self):
215
215
index = [0 , 3 ])
216
216
assert_frame_equal (result , expected )
217
217
218
- def test_fillna (self ):
219
- tf = self . tsframe
218
+ def test_fillna_datetime (self , datetime_frame ):
219
+ tf = datetime_frame
220
220
tf .loc [tf .index [:5 ], 'A' ] = np .nan
221
221
tf .loc [tf .index [- 5 :], 'A' ] = np .nan
222
222
223
- zero_filled = self . tsframe .fillna (0 )
223
+ zero_filled = datetime_frame .fillna (0 )
224
224
assert (zero_filled .loc [zero_filled .index [:5 ], 'A' ] == 0 ).all ()
225
225
226
- padded = self . tsframe .fillna (method = 'pad' )
226
+ padded = datetime_frame .fillna (method = 'pad' )
227
227
assert np .isnan (padded .loc [padded .index [:5 ], 'A' ]).all ()
228
228
assert (padded .loc [padded .index [- 5 :], 'A' ] ==
229
229
padded .loc [padded .index [- 5 ], 'A' ]).all ()
230
230
231
- # mixed type
232
- mf = self .mixed_frame
233
- mf .loc [mf .index [5 :20 ], 'foo' ] = np .nan
234
- mf .loc [mf .index [- 10 :], 'A' ] = np .nan
235
- result = self .mixed_frame .fillna (value = 0 )
236
- result = self .mixed_frame .fillna (method = 'pad' )
237
-
238
231
msg = "Must specify a fill 'value' or 'method'"
239
232
with pytest .raises (ValueError , match = msg ):
240
- self . tsframe .fillna ()
233
+ datetime_frame .fillna ()
241
234
msg = "Cannot specify both 'value' and 'method'"
242
235
with pytest .raises (ValueError , match = msg ):
243
- self .tsframe .fillna (5 , method = 'ffill' )
236
+ datetime_frame .fillna (5 , method = 'ffill' )
237
+
238
+ def test_fillna_mixed_type (self , float_string_frame ):
239
+
240
+ mf = float_string_frame
241
+ mf .loc [mf .index [5 :20 ], 'foo' ] = np .nan
242
+ mf .loc [mf .index [- 10 :], 'A' ] = np .nan
243
+ # TODO: make stronger assertion here, GH 25640
244
+ mf .fillna (value = 0 )
245
+ mf .fillna (method = 'pad' )
246
+
247
+ def test_fillna_mixed_float (self , mixed_float_frame ):
244
248
245
249
# mixed numeric (but no float16)
246
- mf = self . mixed_float .reindex (columns = ['A' , 'B' , 'D' ])
250
+ mf = mixed_float_frame .reindex (columns = ['A' , 'B' , 'D' ])
247
251
mf .loc [mf .index [- 10 :], 'A' ] = np .nan
248
252
result = mf .fillna (value = 0 )
249
253
_check_mixed_float (result , dtype = dict (C = None ))
250
254
251
255
result = mf .fillna (method = 'pad' )
252
256
_check_mixed_float (result , dtype = dict (C = None ))
253
257
258
+ def test_fillna_other (self ):
254
259
# empty frame (GH #2778)
255
260
df = DataFrame (columns = ['x' ])
256
261
for m in ['pad' , 'backfill' ]:
@@ -464,19 +469,19 @@ def test_fillna_datetime_columns(self):
464
469
index = pd .date_range ('20130110' , periods = 3 ))
465
470
tm .assert_frame_equal (result , expected )
466
471
467
- def test_ffill (self ):
468
- self . tsframe ['A' ][:5 ] = np .nan
469
- self . tsframe ['A' ][- 5 :] = np .nan
472
+ def test_ffill (self , datetime_frame ):
473
+ datetime_frame ['A' ][:5 ] = np .nan
474
+ datetime_frame ['A' ][- 5 :] = np .nan
470
475
471
- assert_frame_equal (self . tsframe .ffill (),
472
- self . tsframe .fillna (method = 'ffill' ))
476
+ assert_frame_equal (datetime_frame .ffill (),
477
+ datetime_frame .fillna (method = 'ffill' ))
473
478
474
- def test_bfill (self ):
475
- self . tsframe ['A' ][:5 ] = np .nan
476
- self . tsframe ['A' ][- 5 :] = np .nan
479
+ def test_bfill (self , datetime_frame ):
480
+ datetime_frame ['A' ][:5 ] = np .nan
481
+ datetime_frame ['A' ][- 5 :] = np .nan
477
482
478
- assert_frame_equal (self . tsframe .bfill (),
479
- self . tsframe .fillna (method = 'bfill' ))
483
+ assert_frame_equal (datetime_frame .bfill (),
484
+ datetime_frame .fillna (method = 'bfill' ))
480
485
481
486
def test_frame_pad_backfill_limit (self ):
482
487
index = np .arange (10 )
@@ -602,24 +607,24 @@ def test_fillna_columns(self):
602
607
expected = df .astype (float ).fillna (method = 'ffill' , axis = 1 )
603
608
assert_frame_equal (result , expected )
604
609
605
- def test_fillna_invalid_method (self ):
610
+ def test_fillna_invalid_method (self , float_frame ):
606
611
with pytest .raises (ValueError , match = 'ffil' ):
607
- self . frame .fillna (method = 'ffil' )
612
+ float_frame .fillna (method = 'ffil' )
608
613
609
- def test_fillna_invalid_value (self ):
614
+ def test_fillna_invalid_value (self , float_frame ):
610
615
# list
611
616
msg = ("\" value\" parameter must be a scalar or dict, but you passed"
612
617
" a \" {}\" " )
613
618
with pytest .raises (TypeError , match = msg .format ('list' )):
614
- self . frame .fillna ([1 , 2 ])
619
+ float_frame .fillna ([1 , 2 ])
615
620
# tuple
616
621
with pytest .raises (TypeError , match = msg .format ('tuple' )):
617
- self . frame .fillna ((1 , 2 ))
622
+ float_frame .fillna ((1 , 2 ))
618
623
# frame with series
619
624
msg = ("\" value\" parameter must be a scalar, dict or Series, but you"
620
625
" passed a \" DataFrame\" " )
621
626
with pytest .raises (TypeError , match = msg ):
622
- self . frame . iloc [:, 0 ].fillna (self . frame )
627
+ float_frame . iloc [:, 0 ].fillna (float_frame )
623
628
624
629
def test_fillna_col_reordering (self ):
625
630
cols = ["COL." + str (i ) for i in range (5 , 0 , - 1 )]
@@ -628,16 +633,16 @@ def test_fillna_col_reordering(self):
628
633
filled = df .fillna (method = 'ffill' )
629
634
assert df .columns .tolist () == filled .columns .tolist ()
630
635
631
- def test_fill_corner (self ):
632
- mf = self . mixed_frame
636
+ def test_fill_corner (self , float_frame , float_string_frame ):
637
+ mf = float_string_frame
633
638
mf .loc [mf .index [5 :20 ], 'foo' ] = np .nan
634
639
mf .loc [mf .index [- 10 :], 'A' ] = np .nan
635
640
636
- filled = self . mixed_frame .fillna (value = 0 )
641
+ filled = float_string_frame .fillna (value = 0 )
637
642
assert (filled .loc [filled .index [5 :20 ], 'foo' ] == 0 ).all ()
638
- del self . mixed_frame ['foo' ]
643
+ del float_string_frame ['foo' ]
639
644
640
- empty_float = self . frame .reindex (columns = [])
645
+ empty_float = float_frame .reindex (columns = [])
641
646
642
647
# TODO(wesm): unused?
643
648
result = empty_float .fillna (value = 0 ) # noqa
@@ -652,7 +657,7 @@ def test_fill_value_when_combine_const(self):
652
657
assert_frame_equal (res , exp )
653
658
654
659
655
- class TestDataFrameInterpolate (TestData ):
660
+ class TestDataFrameInterpolate ():
656
661
657
662
def test_interp_basic (self ):
658
663
df = DataFrame ({'A' : [1 , 2 , np .nan , 4 ],
0 commit comments