24
24
25
25
import pandas .util .testing as tm
26
26
27
- from pandas .tests .frame .common import TestData
28
-
29
27
30
28
class SharedWithSparse (object ):
31
29
"""
@@ -43,57 +41,57 @@ def _assert_series_equal(self, left, right):
43
41
"""Dispatch to series class dependent assertion"""
44
42
raise NotImplementedError
45
43
46
- def test_copy_index_name_checking (self ):
44
+ def test_copy_index_name_checking (self , float_frame ):
47
45
# don't want to be able to modify the index stored elsewhere after
48
46
# making a copy
49
47
for attr in ('index' , 'columns' ):
50
- ind = getattr (self . frame , attr )
48
+ ind = getattr (float_frame , attr )
51
49
ind .name = None
52
- cp = self . frame .copy ()
50
+ cp = float_frame .copy ()
53
51
getattr (cp , attr ).name = 'foo'
54
- assert getattr (self . frame , attr ).name is None
52
+ assert getattr (float_frame , attr ).name is None
55
53
56
- def test_getitem_pop_assign_name (self ):
57
- s = self . frame ['A' ]
54
+ def test_getitem_pop_assign_name (self , float_frame ):
55
+ s = float_frame ['A' ]
58
56
assert s .name == 'A'
59
57
60
- s = self . frame .pop ('A' )
58
+ s = float_frame .pop ('A' )
61
59
assert s .name == 'A'
62
60
63
- s = self . frame .loc [:, 'B' ]
61
+ s = float_frame .loc [:, 'B' ]
64
62
assert s .name == 'B'
65
63
66
64
s2 = s .loc [:]
67
65
assert s2 .name == 'B'
68
66
69
- def test_get_value (self ):
70
- for idx in self . frame .index :
71
- for col in self . frame .columns :
67
+ def test_get_value (self , float_frame ):
68
+ for idx in float_frame .index :
69
+ for col in float_frame .columns :
72
70
with tm .assert_produces_warning (FutureWarning ,
73
71
check_stacklevel = False ):
74
- result = self . frame .get_value (idx , col )
75
- expected = self . frame [col ][idx ]
72
+ result = float_frame .get_value (idx , col )
73
+ expected = float_frame [col ][idx ]
76
74
tm .assert_almost_equal (result , expected )
77
75
78
- def test_add_prefix_suffix (self ):
79
- with_prefix = self . frame .add_prefix ('foo#' )
80
- expected = pd .Index (['foo#%s' % c for c in self . frame .columns ])
76
+ def test_add_prefix_suffix (self , float_frame ):
77
+ with_prefix = float_frame .add_prefix ('foo#' )
78
+ expected = pd .Index (['foo#%s' % c for c in float_frame .columns ])
81
79
tm .assert_index_equal (with_prefix .columns , expected )
82
80
83
- with_suffix = self . frame .add_suffix ('#foo' )
84
- expected = pd .Index (['%s#foo' % c for c in self . frame .columns ])
81
+ with_suffix = float_frame .add_suffix ('#foo' )
82
+ expected = pd .Index (['%s#foo' % c for c in float_frame .columns ])
85
83
tm .assert_index_equal (with_suffix .columns , expected )
86
84
87
- with_pct_prefix = self . frame .add_prefix ('%' )
88
- expected = pd .Index (['%{}' .format (c ) for c in self . frame .columns ])
85
+ with_pct_prefix = float_frame .add_prefix ('%' )
86
+ expected = pd .Index (['%{}' .format (c ) for c in float_frame .columns ])
89
87
tm .assert_index_equal (with_pct_prefix .columns , expected )
90
88
91
- with_pct_suffix = self . frame .add_suffix ('%' )
92
- expected = pd .Index (['{}%' .format (c ) for c in self . frame .columns ])
89
+ with_pct_suffix = float_frame .add_suffix ('%' )
90
+ expected = pd .Index (['{}%' .format (c ) for c in float_frame .columns ])
93
91
tm .assert_index_equal (with_pct_suffix .columns , expected )
94
92
95
- def test_get_axis (self ):
96
- f = self . frame
93
+ def test_get_axis (self , float_frame ):
94
+ f = float_frame
97
95
assert f ._get_axis_number (0 ) == 0
98
96
assert f ._get_axis_number (1 ) == 1
99
97
assert f ._get_axis_number ('index' ) == 0
@@ -118,13 +116,13 @@ def test_get_axis(self):
118
116
tm .assert_raises_regex (ValueError , 'No axis named' ,
119
117
f ._get_axis_number , None )
120
118
121
- def test_keys (self ):
122
- getkeys = self . frame .keys
123
- assert getkeys () is self . frame .columns
119
+ def test_keys (self , float_frame ):
120
+ getkeys = float_frame .keys
121
+ assert getkeys () is float_frame .columns
124
122
125
- def test_column_contains_typeerror (self ):
123
+ def test_column_contains_typeerror (self , float_frame ):
126
124
try :
127
- self . frame . columns in self . frame
125
+ float_frame . columns in float_frame
128
126
except TypeError :
129
127
pass
130
128
@@ -146,40 +144,40 @@ def test_tab_completion(self):
146
144
assert key not in dir (df )
147
145
assert isinstance (df .__getitem__ ('A' ), pd .DataFrame )
148
146
149
- def test_not_hashable (self ):
147
+ def test_not_hashable (self , empty_frame ):
150
148
df = self .klass ([1 ])
151
149
pytest .raises (TypeError , hash , df )
152
- pytest .raises (TypeError , hash , self . empty )
150
+ pytest .raises (TypeError , hash , empty_frame )
153
151
154
152
def test_new_empty_index (self ):
155
153
df1 = self .klass (randn (0 , 3 ))
156
154
df2 = self .klass (randn (0 , 3 ))
157
155
df1 .index .name = 'foo'
158
156
assert df2 .index .name is None
159
157
160
- def test_array_interface (self ):
158
+ def test_array_interface (self , float_frame ):
161
159
with np .errstate (all = 'ignore' ):
162
- result = np .sqrt (self . frame )
163
- assert isinstance (result , type (self . frame ))
164
- assert result .index is self . frame .index
165
- assert result .columns is self . frame .columns
160
+ result = np .sqrt (float_frame )
161
+ assert isinstance (result , type (float_frame ))
162
+ assert result .index is float_frame .index
163
+ assert result .columns is float_frame .columns
166
164
167
- self ._assert_frame_equal (result , self . frame .apply (np .sqrt ))
165
+ self ._assert_frame_equal (result , float_frame .apply (np .sqrt ))
168
166
169
- def test_get_agg_axis (self ):
170
- cols = self . frame ._get_agg_axis (0 )
171
- assert cols is self . frame .columns
167
+ def test_get_agg_axis (self , float_frame ):
168
+ cols = float_frame ._get_agg_axis (0 )
169
+ assert cols is float_frame .columns
172
170
173
- idx = self . frame ._get_agg_axis (1 )
174
- assert idx is self . frame .index
171
+ idx = float_frame ._get_agg_axis (1 )
172
+ assert idx is float_frame .index
175
173
176
- pytest .raises (ValueError , self . frame ._get_agg_axis , 2 )
174
+ pytest .raises (ValueError , float_frame ._get_agg_axis , 2 )
177
175
178
- def test_nonzero (self ):
179
- assert self . empty .empty
176
+ def test_nonzero (self , float_frame , float_string_frame , empty_frame ):
177
+ assert empty_frame .empty
180
178
181
- assert not self . frame .empty
182
- assert not self . mixed_frame .empty
179
+ assert not float_frame .empty
180
+ assert not float_string_frame .empty
183
181
184
182
# corner case
185
183
df = DataFrame ({'A' : [1. , 2. , 3. ],
@@ -202,16 +200,16 @@ def test_items(self):
202
200
assert isinstance (v , Series )
203
201
assert (df [k ] == v ).all ()
204
202
205
- def test_iter (self ):
206
- assert tm .equalContents (list (self . frame ), self . frame .columns )
203
+ def test_iter (self , float_frame ):
204
+ assert tm .equalContents (list (float_frame ), float_frame .columns )
207
205
208
- def test_iterrows (self ):
209
- for k , v in self . frame .iterrows ():
210
- exp = self . frame .loc [k ]
206
+ def test_iterrows (self , float_frame , float_string_frame ):
207
+ for k , v in float_frame .iterrows ():
208
+ exp = float_frame .loc [k ]
211
209
self ._assert_series_equal (v , exp )
212
210
213
- for k , v in self . mixed_frame .iterrows ():
214
- exp = self . mixed_frame .loc [k ]
211
+ for k , v in float_string_frame .iterrows ():
212
+ exp = float_string_frame .loc [k ]
215
213
self ._assert_series_equal (v , exp )
216
214
217
215
def test_iterrows_iso8601 (self ):
@@ -226,11 +224,11 @@ def test_iterrows_iso8601(self):
226
224
exp = s .loc [k ]
227
225
self ._assert_series_equal (v , exp )
228
226
229
- def test_itertuples (self ):
230
- for i , tup in enumerate (self . frame .itertuples ()):
227
+ def test_itertuples (self , float_frame ):
228
+ for i , tup in enumerate (float_frame .itertuples ()):
231
229
s = self .klass ._constructor_sliced (tup [1 :])
232
230
s .name = tup [0 ]
233
- expected = self . frame .iloc [i , :].reset_index (drop = True )
231
+ expected = float_frame .iloc [i , :].reset_index (drop = True )
234
232
self ._assert_series_equal (s , expected )
235
233
236
234
df = self .klass ({'floats' : np .random .randn (5 ),
@@ -289,11 +287,11 @@ def test_sequence_like_with_categorical(self):
289
287
for c , col in df .iteritems ():
290
288
str (s )
291
289
292
- def test_len (self ):
293
- assert len (self . frame ) == len (self . frame .index )
290
+ def test_len (self , float_frame ):
291
+ assert len (float_frame ) == len (float_frame .index )
294
292
295
- def test_values (self ):
296
- frame = self . frame
293
+ def test_values (self , float_frame , float_string_frame ):
294
+ frame = float_frame
297
295
arr = frame .values
298
296
299
297
frame_cols = frame .columns
@@ -306,20 +304,20 @@ def test_values(self):
306
304
assert value == frame [col ][i ]
307
305
308
306
# mixed type
309
- arr = self . mixed_frame [['foo' , 'A' ]].values
307
+ arr = float_string_frame [['foo' , 'A' ]].values
310
308
assert arr [0 , 0 ] == 'bar'
311
309
312
- df = self .klass ({'real ' : [1 , 2 , 3 ], 'complex ' : [1j , 2j , 3j ]})
310
+ df = self .klass ({'complex ' : [1j , 2j , 3j ], 'real ' : [1 , 2 , 3 ]})
313
311
arr = df .values
314
312
assert arr [0 , 0 ] == 1j
315
313
316
314
# single block corner case
317
- arr = self . frame [['A' , 'B' ]].values
318
- expected = self . frame .reindex (columns = ['A' , 'B' ]).values
315
+ arr = float_frame [['A' , 'B' ]].values
316
+ expected = float_frame .reindex (columns = ['A' , 'B' ]).values
319
317
assert_almost_equal (arr , expected )
320
318
321
- def test_transpose (self ):
322
- frame = self . frame
319
+ def test_transpose (self , float_frame ):
320
+ frame = float_frame
323
321
dft = frame .T
324
322
for idx , series in compat .iteritems (dft ):
325
323
for col , value in compat .iteritems (series ):
@@ -343,8 +341,8 @@ def test_swapaxes(self):
343
341
self ._assert_frame_equal (df , df .swapaxes (0 , 0 ))
344
342
pytest .raises (ValueError , df .swapaxes , 2 , 5 )
345
343
346
- def test_axis_aliases (self ):
347
- f = self . frame
344
+ def test_axis_aliases (self , float_frame ):
345
+ f = float_frame
348
346
349
347
# reg name
350
348
expected = f .sum (axis = 0 )
@@ -361,23 +359,23 @@ def test_class_axis(self):
361
359
assert pydoc .getdoc (DataFrame .index )
362
360
assert pydoc .getdoc (DataFrame .columns )
363
361
364
- def test_more_values (self ):
365
- values = self . mixed_frame .values
366
- assert values .shape [1 ] == len (self . mixed_frame .columns )
362
+ def test_more_values (self , float_string_frame ):
363
+ values = float_string_frame .values
364
+ assert values .shape [1 ] == len (float_string_frame .columns )
367
365
368
- def test_repr_with_mi_nat (self ):
366
+ def test_repr_with_mi_nat (self , float_string_frame ):
369
367
df = self .klass ({'X' : [1 , 2 ]},
370
368
index = [[pd .NaT , pd .Timestamp ('20130101' )], ['a' , 'b' ]])
371
369
res = repr (df )
372
370
exp = ' X\n NaT a 1\n 2013-01-01 b 2'
373
371
assert res == exp
374
372
375
- def test_iteritems_names (self ):
376
- for k , v in compat .iteritems (self . mixed_frame ):
373
+ def test_iteritems_names (self , float_string_frame ):
374
+ for k , v in compat .iteritems (float_string_frame ):
377
375
assert v .name == k
378
376
379
- def test_series_put_names (self ):
380
- series = self . mixed_frame ._series
377
+ def test_series_put_names (self , float_string_frame ):
378
+ series = float_string_frame ._series
381
379
for k , v in compat .iteritems (series ):
382
380
assert v .name == k
383
381
@@ -408,36 +406,37 @@ def test_with_datetimelikes(self):
408
406
tm .assert_series_equal (result , expected )
409
407
410
408
411
- class TestDataFrameMisc (SharedWithSparse , TestData ):
409
+ class TestDataFrameMisc (SharedWithSparse ):
412
410
413
411
klass = DataFrame
414
412
# SharedWithSparse tests use generic, klass-agnostic assertion
415
413
_assert_frame_equal = staticmethod (assert_frame_equal )
416
414
_assert_series_equal = staticmethod (assert_series_equal )
417
415
418
- def test_values (self ):
419
- self . frame .values [:, 0 ] = 5.
420
- assert (self . frame .values [:, 0 ] == 5 ).all ()
416
+ def test_values (self , float_frame ):
417
+ float_frame .values [:, 0 ] = 5.
418
+ assert (float_frame .values [:, 0 ] == 5 ).all ()
421
419
422
- def test_as_matrix_deprecated (self ):
420
+ def test_as_matrix_deprecated (self , float_frame ):
423
421
# GH18458
424
422
with tm .assert_produces_warning (FutureWarning ):
425
- result = self .frame .as_matrix (columns = self .frame .columns .tolist ())
426
- expected = self .frame .values
423
+ cols = float_frame .columns .tolist ()
424
+ result = float_frame .as_matrix (columns = cols )
425
+ expected = float_frame .values
427
426
tm .assert_numpy_array_equal (result , expected )
428
427
429
- def test_deepcopy (self ):
430
- cp = deepcopy (self . frame )
428
+ def test_deepcopy (self , float_frame ):
429
+ cp = deepcopy (float_frame )
431
430
series = cp ['A' ]
432
431
series [:] = 10
433
432
for idx , value in compat .iteritems (series ):
434
- assert self . frame ['A' ][idx ] != value
433
+ assert float_frame ['A' ][idx ] != value
435
434
436
- def test_transpose_get_view (self ):
437
- dft = self . frame .T
435
+ def test_transpose_get_view (self , float_frame ):
436
+ dft = float_frame .T
438
437
dft .values [:, 5 :10 ] = 5
439
438
440
- assert (self . frame .values [5 :10 ] == 5 ).all ()
439
+ assert (float_frame .values [5 :10 ] == 5 ).all ()
441
440
442
441
def test_inplace_return_self (self ):
443
442
# re #1893
0 commit comments