22
22
23
23
import pandas .util .testing as tm
24
24
25
- from pandas .tests .frame .common import TestData
26
-
27
25
28
26
# Segregated collection of methods that require the BlockManager internal data
29
27
# structure
30
28
31
29
32
- class TestDataFrameBlockInternals (TestData ):
30
+ class TestDataFrameBlockInternals ():
33
31
34
- def test_cast_internals (self ):
35
- casted = DataFrame (self . frame ._data , dtype = int )
36
- expected = DataFrame (self . frame ._series , dtype = int )
32
+ def test_cast_internals (self , float_frame ):
33
+ casted = DataFrame (float_frame ._data , dtype = int )
34
+ expected = DataFrame (float_frame ._series , dtype = int )
37
35
assert_frame_equal (casted , expected )
38
36
39
- casted = DataFrame (self . frame ._data , dtype = np .int32 )
40
- expected = DataFrame (self . frame ._series , dtype = np .int32 )
37
+ casted = DataFrame (float_frame ._data , dtype = np .int32 )
38
+ expected = DataFrame (float_frame ._series , dtype = np .int32 )
41
39
assert_frame_equal (casted , expected )
42
40
43
- def test_consolidate (self ):
44
- self . frame ['E' ] = 7.
45
- consolidated = self . frame ._consolidate ()
41
+ def test_consolidate (self , float_frame ):
42
+ float_frame ['E' ] = 7.
43
+ consolidated = float_frame ._consolidate ()
46
44
assert len (consolidated ._data .blocks ) == 1
47
45
48
46
# Ensure copy, do I want this?
49
47
recons = consolidated ._consolidate ()
50
48
assert recons is not consolidated
51
49
tm .assert_frame_equal (recons , consolidated )
52
50
53
- self . frame ['F' ] = 8.
54
- assert len (self . frame ._data .blocks ) == 3
51
+ float_frame ['F' ] = 8.
52
+ assert len (float_frame ._data .blocks ) == 3
55
53
56
- self . frame ._consolidate (inplace = True )
57
- assert len (self . frame ._data .blocks ) == 1
54
+ float_frame ._consolidate (inplace = True )
55
+ assert len (float_frame ._data .blocks ) == 1
58
56
59
- def test_consolidate_deprecation (self ):
60
- self . frame ['E' ] = 7
57
+ def test_consolidate_deprecation (self , float_frame ):
58
+ float_frame ['E' ] = 7
61
59
with tm .assert_produces_warning (FutureWarning ):
62
- self . frame .consolidate ()
60
+ float_frame .consolidate ()
63
61
64
- def test_consolidate_inplace (self ):
65
- frame = self . frame .copy () # noqa
62
+ def test_consolidate_inplace (self , float_frame ):
63
+ frame = float_frame .copy () # noqa
66
64
67
65
# triggers in-place consolidation
68
66
for letter in range (ord ('A' ), ord ('Z' )):
69
- self . frame [chr (letter )] = chr (letter )
67
+ float_frame [chr (letter )] = chr (letter )
70
68
71
- def test_values_consolidate (self ):
72
- self . frame ['E' ] = 7.
73
- assert not self . frame ._data .is_consolidated ()
74
- _ = self . frame .values # noqa
75
- assert self . frame ._data .is_consolidated ()
69
+ def test_values_consolidate (self , float_frame ):
70
+ float_frame ['E' ] = 7.
71
+ assert not float_frame ._data .is_consolidated ()
72
+ _ = float_frame .values # noqa
73
+ assert float_frame ._data .is_consolidated ()
76
74
77
- def test_modify_values (self ):
78
- self . frame .values [5 ] = 5
79
- assert (self . frame .values [5 ] == 5 ).all ()
75
+ def test_modify_values (self , float_frame ):
76
+ float_frame .values [5 ] = 5
77
+ assert (float_frame .values [5 ] == 5 ).all ()
80
78
81
79
# unconsolidated
82
- self . frame ['E' ] = 7.
83
- self . frame .values [6 ] = 6
84
- assert (self . frame .values [6 ] == 6 ).all ()
80
+ float_frame ['E' ] = 7.
81
+ float_frame .values [6 ] = 6
82
+ assert (float_frame .values [6 ] == 6 ).all ()
85
83
86
- def test_boolean_set_uncons (self ):
87
- self . frame ['E' ] = 7.
84
+ def test_boolean_set_uncons (self , float_frame ):
85
+ float_frame ['E' ] = 7.
88
86
89
- expected = self . frame .values .copy ()
87
+ expected = float_frame .values .copy ()
90
88
expected [expected > 1 ] = 2
91
89
92
- self . frame [ self . frame > 1 ] = 2
93
- assert_almost_equal (expected , self . frame .values )
90
+ float_frame [ float_frame > 1 ] = 2
91
+ assert_almost_equal (expected , float_frame .values )
94
92
95
- def test_values_numeric_cols (self ):
96
- self . frame ['foo' ] = 'bar'
93
+ def test_values_numeric_cols (self , float_frame ):
94
+ float_frame ['foo' ] = 'bar'
97
95
98
- values = self . frame [['A' , 'B' , 'C' , 'D' ]].values
96
+ values = float_frame [['A' , 'B' , 'C' , 'D' ]].values
99
97
assert values .dtype == np .float64
100
98
101
- def test_values_lcd (self ):
99
+ def test_values_lcd (self , mixed_float_frame , mixed_int_frame ):
102
100
103
101
# mixed lcd
104
- values = self . mixed_float [['A' , 'B' , 'C' , 'D' ]].values
102
+ values = mixed_float_frame [['A' , 'B' , 'C' , 'D' ]].values
105
103
assert values .dtype == np .float64
106
104
107
- values = self . mixed_float [['A' , 'B' , 'C' ]].values
105
+ values = mixed_float_frame [['A' , 'B' , 'C' ]].values
108
106
assert values .dtype == np .float32
109
107
110
- values = self . mixed_float [['C' ]].values
108
+ values = mixed_float_frame [['C' ]].values
111
109
assert values .dtype == np .float16
112
110
113
111
# GH 10364
114
112
# B uint64 forces float because there are other signed int types
115
- values = self . mixed_int [['A' , 'B' , 'C' , 'D' ]].values
113
+ values = mixed_int_frame [['A' , 'B' , 'C' , 'D' ]].values
116
114
assert values .dtype == np .float64
117
115
118
- values = self . mixed_int [['A' , 'D' ]].values
116
+ values = mixed_int_frame [['A' , 'D' ]].values
119
117
assert values .dtype == np .int64
120
118
121
119
# B uint64 forces float because there are other signed int types
122
- values = self . mixed_int [['A' , 'B' , 'C' ]].values
120
+ values = mixed_int_frame [['A' , 'B' , 'C' ]].values
123
121
assert values .dtype == np .float64
124
122
125
123
# as B and C are both unsigned, no forcing to float is needed
126
- values = self . mixed_int [['B' , 'C' ]].values
124
+ values = mixed_int_frame [['B' , 'C' ]].values
127
125
assert values .dtype == np .uint64
128
126
129
- values = self . mixed_int [['A' , 'C' ]].values
127
+ values = mixed_int_frame [['A' , 'C' ]].values
130
128
assert values .dtype == np .int32
131
129
132
- values = self . mixed_int [['C' , 'D' ]].values
130
+ values = mixed_int_frame [['C' , 'D' ]].values
133
131
assert values .dtype == np .int64
134
132
135
- values = self . mixed_int [['A' ]].values
133
+ values = mixed_int_frame [['A' ]].values
136
134
assert values .dtype == np .int32
137
135
138
- values = self . mixed_int [['C' ]].values
136
+ values = mixed_int_frame [['C' ]].values
139
137
assert values .dtype == np .uint8
140
138
141
139
def test_constructor_with_convert (self ):
@@ -205,7 +203,7 @@ def test_constructor_with_convert(self):
205
203
None ], np .object_ ), name = 'A' )
206
204
assert_series_equal (result , expected )
207
205
208
- def test_construction_with_mixed (self ):
206
+ def test_construction_with_mixed (self , float_string_frame ):
209
207
# test construction edge cases with mixed types
210
208
211
209
# f7u12, this does not work without extensive workaround
@@ -219,11 +217,11 @@ def test_construction_with_mixed(self):
219
217
expected = Series ({'datetime64[ns]' : 3 })
220
218
221
219
# mixed-type frames
222
- self . mixed_frame ['datetime' ] = datetime .now ()
223
- self . mixed_frame ['timedelta' ] = timedelta (days = 1 , seconds = 1 )
224
- assert self . mixed_frame ['datetime' ].dtype == 'M8[ns]'
225
- assert self . mixed_frame ['timedelta' ].dtype == 'm8[ns]'
226
- result = self . mixed_frame .get_dtype_counts ().sort_values ()
220
+ float_string_frame ['datetime' ] = datetime .now ()
221
+ float_string_frame ['timedelta' ] = timedelta (days = 1 , seconds = 1 )
222
+ assert float_string_frame ['datetime' ].dtype == 'M8[ns]'
223
+ assert float_string_frame ['timedelta' ].dtype == 'm8[ns]'
224
+ result = float_string_frame .get_dtype_counts ().sort_values ()
227
225
expected = Series ({'float64' : 4 ,
228
226
'object' : 1 ,
229
227
'datetime64[ns]' : 1 ,
@@ -296,9 +294,9 @@ def test_equals_different_blocks(self):
296
294
assert df0 .equals (df1 )
297
295
assert df1 .equals (df0 )
298
296
299
- def test_copy_blocks (self ):
297
+ def test_copy_blocks (self , float_frame ):
300
298
# API/ENH 9607
301
- df = DataFrame (self . frame , copy = True )
299
+ df = DataFrame (float_frame , copy = True )
302
300
column = df .columns [0 ]
303
301
304
302
# use the default copy=True, change a column
@@ -314,9 +312,9 @@ def test_copy_blocks(self):
314
312
# make sure we did not change the original DataFrame
315
313
assert not _df [column ].equals (df [column ])
316
314
317
- def test_no_copy_blocks (self ):
315
+ def test_no_copy_blocks (self , float_frame ):
318
316
# API/ENH 9607
319
- df = DataFrame (self . frame , copy = True )
317
+ df = DataFrame (float_frame , copy = True )
320
318
column = df .columns [0 ]
321
319
322
320
# use the copy=False, change a column
@@ -332,29 +330,29 @@ def test_no_copy_blocks(self):
332
330
# make sure we did change the original DataFrame
333
331
assert _df [column ].equals (df [column ])
334
332
335
- def test_copy (self ):
336
- cop = self . frame .copy ()
333
+ def test_copy (self , float_frame , float_string_frame ):
334
+ cop = float_frame .copy ()
337
335
cop ['E' ] = cop ['A' ]
338
- assert 'E' not in self . frame
336
+ assert 'E' not in float_frame
339
337
340
338
# copy objects
341
- copy = self . mixed_frame .copy ()
342
- assert copy ._data is not self . mixed_frame ._data
339
+ copy = float_string_frame .copy ()
340
+ assert copy ._data is not float_string_frame ._data
343
341
344
- def test_pickle (self ):
345
- unpickled = tm .round_trip_pickle (self . mixed_frame )
346
- assert_frame_equal (self . mixed_frame , unpickled )
342
+ def test_pickle (self , float_string_frame , empty_frame , timezone_frame ):
343
+ unpickled = tm .round_trip_pickle (float_string_frame )
344
+ assert_frame_equal (float_string_frame , unpickled )
347
345
348
346
# buglet
349
- self . mixed_frame ._data .ndim
347
+ float_string_frame ._data .ndim
350
348
351
349
# empty
352
- unpickled = tm .round_trip_pickle (self . empty )
350
+ unpickled = tm .round_trip_pickle (empty_frame )
353
351
repr (unpickled )
354
352
355
353
# tz frame
356
- unpickled = tm .round_trip_pickle (self . tzframe )
357
- assert_frame_equal (self . tzframe , unpickled )
354
+ unpickled = tm .round_trip_pickle (timezone_frame )
355
+ assert_frame_equal (timezone_frame , unpickled )
358
356
359
357
def test_consolidate_datetime64 (self ):
360
358
# numpy vstack bug
@@ -388,9 +386,9 @@ def test_consolidate_datetime64(self):
388
386
df .starting ), ser_starting .index )
389
387
tm .assert_index_equal (pd .DatetimeIndex (df .ending ), ser_ending .index )
390
388
391
- def test_is_mixed_type (self ):
392
- assert not self . frame ._is_mixed_type
393
- assert self . mixed_frame ._is_mixed_type
389
+ def test_is_mixed_type (self , float_frame , float_string_frame ):
390
+ assert not float_frame ._is_mixed_type
391
+ assert float_string_frame ._is_mixed_type
394
392
395
393
def test_get_numeric_data (self ):
396
394
# TODO(wesm): unused?
@@ -448,23 +446,23 @@ def test_get_numeric_data_extension_dtype(self):
448
446
expected = df .loc [:, ['A' , 'C' ]]
449
447
assert_frame_equal (result , expected )
450
448
451
- def test_convert_objects (self ):
449
+ def test_convert_objects (self , float_string_frame ):
452
450
453
- oops = self . mixed_frame .T .T
451
+ oops = float_string_frame .T .T
454
452
converted = oops ._convert (datetime = True )
455
- assert_frame_equal (converted , self . mixed_frame )
453
+ assert_frame_equal (converted , float_string_frame )
456
454
assert converted ['A' ].dtype == np .float64
457
455
458
456
# force numeric conversion
459
- self . mixed_frame ['H' ] = '1.'
460
- self . mixed_frame ['I' ] = '1'
457
+ float_string_frame ['H' ] = '1.'
458
+ float_string_frame ['I' ] = '1'
461
459
462
460
# add in some items that will be nan
463
- length = len (self . mixed_frame )
464
- self . mixed_frame ['J' ] = '1.'
465
- self . mixed_frame ['K' ] = '1'
466
- self . mixed_frame .loc [0 :5 , ['J' , 'K' ]] = 'garbled'
467
- converted = self . mixed_frame ._convert (datetime = True , numeric = True )
461
+ length = len (float_string_frame )
462
+ float_string_frame ['J' ] = '1.'
463
+ float_string_frame ['K' ] = '1'
464
+ float_string_frame .loc [0 :5 , ['J' , 'K' ]] = 'garbled'
465
+ converted = float_string_frame ._convert (datetime = True , numeric = True )
468
466
assert converted ['H' ].dtype == 'float64'
469
467
assert converted ['I' ].dtype == 'int64'
470
468
assert converted ['J' ].dtype == 'float64'
@@ -473,14 +471,14 @@ def test_convert_objects(self):
473
471
assert len (converted ['K' ].dropna ()) == length - 5
474
472
475
473
# via astype
476
- converted = self . mixed_frame .copy ()
474
+ converted = float_string_frame .copy ()
477
475
converted ['H' ] = converted ['H' ].astype ('float64' )
478
476
converted ['I' ] = converted ['I' ].astype ('int64' )
479
477
assert converted ['H' ].dtype == 'float64'
480
478
assert converted ['I' ].dtype == 'int64'
481
479
482
480
# via astype, but errors
483
- converted = self . mixed_frame .copy ()
481
+ converted = float_string_frame .copy ()
484
482
with tm .assert_raises_regex (ValueError , 'invalid literal' ):
485
483
converted ['H' ].astype ('int32' )
486
484
0 commit comments