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