Skip to content

Commit 5bdb0ac

Browse files
h-vetinariWillAyd
authored andcommitted
TST: Fixturize tests/frame/test_missing.py (pandas-dev#25640)
1 parent 406e7ea commit 5bdb0ac

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

pandas/tests/frame/test_missing.py

+58-53
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pandas as pd
1616
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
1818
import pandas.util.testing as tm
1919
from pandas.util.testing import assert_frame_equal, assert_series_equal
2020

@@ -34,15 +34,15 @@ def _skip_if_no_pchip():
3434
pytest.skip('scipy.interpolate.pchip missing')
3535

3636

37-
class TestDataFrameMissingData(TestData):
37+
class TestDataFrameMissingData():
3838

39-
def test_dropEmptyRows(self):
40-
N = len(self.frame.index)
39+
def test_dropEmptyRows(self, float_frame):
40+
N = len(float_frame.index)
4141
mat = np.random.randn(N)
4242
mat[:5] = np.nan
4343

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')
4646
expected = original.dropna()
4747
inplace_frame1, inplace_frame2 = frame.copy(), frame.copy()
4848

@@ -58,30 +58,30 @@ def test_dropEmptyRows(self):
5858
assert_series_equal(smaller_frame['foo'], expected)
5959
assert_series_equal(inplace_frame2['foo'], expected)
6060

61-
def test_dropIncompleteRows(self):
62-
N = len(self.frame.index)
61+
def test_dropIncompleteRows(self, float_frame):
62+
N = len(float_frame.index)
6363
mat = np.random.randn(N)
6464
mat[:5] = np.nan
6565

66-
frame = DataFrame({'foo': mat}, index=self.frame.index)
66+
frame = DataFrame({'foo': mat}, index=float_frame.index)
6767
frame['bar'] = 5
68-
original = Series(mat, index=self.frame.index, name='foo')
68+
original = Series(mat, index=float_frame.index, name='foo')
6969
inp_frame1, inp_frame2 = frame.copy(), frame.copy()
7070

7171
smaller_frame = frame.dropna()
7272
assert_series_equal(frame['foo'], original)
7373
inp_frame1.dropna(inplace=True)
7474

75-
exp = Series(mat[5:], index=self.frame.index[5:], name='foo')
75+
exp = Series(mat[5:], index=float_frame.index[5:], name='foo')
7676
tm.assert_series_equal(smaller_frame['foo'], exp)
7777
tm.assert_series_equal(inp_frame1['foo'], exp)
7878

7979
samesize_frame = frame.dropna(subset=['bar'])
8080
assert_series_equal(frame['foo'], original)
8181
assert (frame['bar'] == 5).all()
8282
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)
8585

8686
@pytest.mark.skipif(PY2, reason="pytest.raises match regex fails")
8787
def test_dropna(self):
@@ -160,17 +160,17 @@ def test_drop_and_dropna_caching(self):
160160
df2['A'].drop([1], inplace=True)
161161
assert_series_equal(df2['A'], original.drop([1]))
162162

163-
def test_dropna_corner(self):
163+
def test_dropna_corner(self, float_frame):
164164
# bad input
165165
msg = "invalid how option: foo"
166166
with pytest.raises(ValueError, match=msg):
167-
self.frame.dropna(how='foo')
167+
float_frame.dropna(how='foo')
168168
msg = "must specify how or thresh"
169169
with pytest.raises(TypeError, match=msg):
170-
self.frame.dropna(how=None)
170+
float_frame.dropna(how=None)
171171
# non-existent column - 8303
172172
with pytest.raises(KeyError, match=r"^\['X'\]$"):
173-
self.frame.dropna(subset=['A', 'X'])
173+
float_frame.dropna(subset=['A', 'X'])
174174

175175
def test_dropna_multiple_axes(self):
176176
df = DataFrame([[1, np.nan, 2, 3],
@@ -215,42 +215,47 @@ def test_dropna_tz_aware_datetime(self):
215215
index=[0, 3])
216216
assert_frame_equal(result, expected)
217217

218-
def test_fillna(self):
219-
tf = self.tsframe
218+
def test_fillna_datetime(self, datetime_frame):
219+
tf = datetime_frame
220220
tf.loc[tf.index[:5], 'A'] = np.nan
221221
tf.loc[tf.index[-5:], 'A'] = np.nan
222222

223-
zero_filled = self.tsframe.fillna(0)
223+
zero_filled = datetime_frame.fillna(0)
224224
assert (zero_filled.loc[zero_filled.index[:5], 'A'] == 0).all()
225225

226-
padded = self.tsframe.fillna(method='pad')
226+
padded = datetime_frame.fillna(method='pad')
227227
assert np.isnan(padded.loc[padded.index[:5], 'A']).all()
228228
assert (padded.loc[padded.index[-5:], 'A'] ==
229229
padded.loc[padded.index[-5], 'A']).all()
230230

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-
238231
msg = "Must specify a fill 'value' or 'method'"
239232
with pytest.raises(ValueError, match=msg):
240-
self.tsframe.fillna()
233+
datetime_frame.fillna()
241234
msg = "Cannot specify both 'value' and 'method'"
242235
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):
244248

245249
# 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'])
247251
mf.loc[mf.index[-10:], 'A'] = np.nan
248252
result = mf.fillna(value=0)
249253
_check_mixed_float(result, dtype=dict(C=None))
250254

251255
result = mf.fillna(method='pad')
252256
_check_mixed_float(result, dtype=dict(C=None))
253257

258+
def test_fillna_other(self):
254259
# empty frame (GH #2778)
255260
df = DataFrame(columns=['x'])
256261
for m in ['pad', 'backfill']:
@@ -464,19 +469,19 @@ def test_fillna_datetime_columns(self):
464469
index=pd.date_range('20130110', periods=3))
465470
tm.assert_frame_equal(result, expected)
466471

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
470475

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'))
473478

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
477482

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'))
480485

481486
def test_frame_pad_backfill_limit(self):
482487
index = np.arange(10)
@@ -602,24 +607,24 @@ def test_fillna_columns(self):
602607
expected = df.astype(float).fillna(method='ffill', axis=1)
603608
assert_frame_equal(result, expected)
604609

605-
def test_fillna_invalid_method(self):
610+
def test_fillna_invalid_method(self, float_frame):
606611
with pytest.raises(ValueError, match='ffil'):
607-
self.frame.fillna(method='ffil')
612+
float_frame.fillna(method='ffil')
608613

609-
def test_fillna_invalid_value(self):
614+
def test_fillna_invalid_value(self, float_frame):
610615
# list
611616
msg = ("\"value\" parameter must be a scalar or dict, but you passed"
612617
" a \"{}\"")
613618
with pytest.raises(TypeError, match=msg.format('list')):
614-
self.frame.fillna([1, 2])
619+
float_frame.fillna([1, 2])
615620
# tuple
616621
with pytest.raises(TypeError, match=msg.format('tuple')):
617-
self.frame.fillna((1, 2))
622+
float_frame.fillna((1, 2))
618623
# frame with series
619624
msg = ("\"value\" parameter must be a scalar, dict or Series, but you"
620625
" passed a \"DataFrame\"")
621626
with pytest.raises(TypeError, match=msg):
622-
self.frame.iloc[:, 0].fillna(self.frame)
627+
float_frame.iloc[:, 0].fillna(float_frame)
623628

624629
def test_fillna_col_reordering(self):
625630
cols = ["COL." + str(i) for i in range(5, 0, -1)]
@@ -628,16 +633,16 @@ def test_fillna_col_reordering(self):
628633
filled = df.fillna(method='ffill')
629634
assert df.columns.tolist() == filled.columns.tolist()
630635

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
633638
mf.loc[mf.index[5:20], 'foo'] = np.nan
634639
mf.loc[mf.index[-10:], 'A'] = np.nan
635640

636-
filled = self.mixed_frame.fillna(value=0)
641+
filled = float_string_frame.fillna(value=0)
637642
assert (filled.loc[filled.index[5:20], 'foo'] == 0).all()
638-
del self.mixed_frame['foo']
643+
del float_string_frame['foo']
639644

640-
empty_float = self.frame.reindex(columns=[])
645+
empty_float = float_frame.reindex(columns=[])
641646

642647
# TODO(wesm): unused?
643648
result = empty_float.fillna(value=0) # noqa
@@ -652,7 +657,7 @@ def test_fill_value_when_combine_const(self):
652657
assert_frame_equal(res, exp)
653658

654659

655-
class TestDataFrameInterpolate(TestData):
660+
class TestDataFrameInterpolate():
656661

657662
def test_interp_basic(self):
658663
df = DataFrame({'A': [1, 2, np.nan, 4],

0 commit comments

Comments
 (0)