Skip to content

Commit 3a3b107

Browse files
committed
Fixturize tests/frame/test_block_internals.py
1 parent 56d8e78 commit 3a3b107

File tree

1 file changed

+87
-89
lines changed

1 file changed

+87
-89
lines changed

pandas/tests/frame/test_block_internals.py

+87-89
Original file line numberDiff line numberDiff line change
@@ -22,120 +22,118 @@
2222

2323
import pandas.util.testing as tm
2424

25-
from pandas.tests.frame.common import TestData
26-
2725

2826
# Segregated collection of methods that require the BlockManager internal data
2927
# structure
3028

3129

32-
class TestDataFrameBlockInternals(TestData):
30+
class TestDataFrameBlockInternals():
3331

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)
3735
assert_frame_equal(casted, expected)
3836

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)
4139
assert_frame_equal(casted, expected)
4240

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()
4644
assert len(consolidated._data.blocks) == 1
4745

4846
# Ensure copy, do I want this?
4947
recons = consolidated._consolidate()
5048
assert recons is not consolidated
5149
tm.assert_frame_equal(recons, consolidated)
5250

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
5553

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
5856

59-
def test_consolidate_deprecation(self):
60-
self.frame['E'] = 7
57+
def test_consolidate_deprecation(self, float_frame):
58+
float_frame['E'] = 7
6159
with tm.assert_produces_warning(FutureWarning):
62-
self.frame.consolidate()
60+
float_frame.consolidate()
6361

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
6664

6765
# triggers in-place consolidation
6866
for letter in range(ord('A'), ord('Z')):
69-
self.frame[chr(letter)] = chr(letter)
67+
float_frame[chr(letter)] = chr(letter)
7068

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()
7674

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()
8078

8179
# 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()
8583

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.
8886

89-
expected = self.frame.values.copy()
87+
expected = float_frame.values.copy()
9088
expected[expected > 1] = 2
9189

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)
9492

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'
9795

98-
values = self.frame[['A', 'B', 'C', 'D']].values
96+
values = float_frame[['A', 'B', 'C', 'D']].values
9997
assert values.dtype == np.float64
10098

101-
def test_values_lcd(self):
99+
def test_values_lcd(self, mixed_float_frame, mixed_int_frame):
102100

103101
# mixed lcd
104-
values = self.mixed_float[['A', 'B', 'C', 'D']].values
102+
values = mixed_float_frame[['A', 'B', 'C', 'D']].values
105103
assert values.dtype == np.float64
106104

107-
values = self.mixed_float[['A', 'B', 'C']].values
105+
values = mixed_float_frame[['A', 'B', 'C']].values
108106
assert values.dtype == np.float32
109107

110-
values = self.mixed_float[['C']].values
108+
values = mixed_float_frame[['C']].values
111109
assert values.dtype == np.float16
112110

113111
# GH 10364
114112
# 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
116114
assert values.dtype == np.float64
117115

118-
values = self.mixed_int[['A', 'D']].values
116+
values = mixed_int_frame[['A', 'D']].values
119117
assert values.dtype == np.int64
120118

121119
# 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
123121
assert values.dtype == np.float64
124122

125123
# 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
127125
assert values.dtype == np.uint64
128126

129-
values = self.mixed_int[['A', 'C']].values
127+
values = mixed_int_frame[['A', 'C']].values
130128
assert values.dtype == np.int32
131129

132-
values = self.mixed_int[['C', 'D']].values
130+
values = mixed_int_frame[['C', 'D']].values
133131
assert values.dtype == np.int64
134132

135-
values = self.mixed_int[['A']].values
133+
values = mixed_int_frame[['A']].values
136134
assert values.dtype == np.int32
137135

138-
values = self.mixed_int[['C']].values
136+
values = mixed_int_frame[['C']].values
139137
assert values.dtype == np.uint8
140138

141139
def test_constructor_with_convert(self):
@@ -205,7 +203,7 @@ def test_constructor_with_convert(self):
205203
None], np.object_), name='A')
206204
assert_series_equal(result, expected)
207205

208-
def test_construction_with_mixed(self):
206+
def test_construction_with_mixed(self, float_string_frame):
209207
# test construction edge cases with mixed types
210208

211209
# f7u12, this does not work without extensive workaround
@@ -219,11 +217,11 @@ def test_construction_with_mixed(self):
219217
expected = Series({'datetime64[ns]': 3})
220218

221219
# 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()
227225
expected = Series({'float64': 4,
228226
'object': 1,
229227
'datetime64[ns]': 1,
@@ -296,9 +294,9 @@ def test_equals_different_blocks(self):
296294
assert df0.equals(df1)
297295
assert df1.equals(df0)
298296

299-
def test_copy_blocks(self):
297+
def test_copy_blocks(self, float_frame):
300298
# API/ENH 9607
301-
df = DataFrame(self.frame, copy=True)
299+
df = DataFrame(float_frame, copy=True)
302300
column = df.columns[0]
303301

304302
# use the default copy=True, change a column
@@ -314,9 +312,9 @@ def test_copy_blocks(self):
314312
# make sure we did not change the original DataFrame
315313
assert not _df[column].equals(df[column])
316314

317-
def test_no_copy_blocks(self):
315+
def test_no_copy_blocks(self, float_frame):
318316
# API/ENH 9607
319-
df = DataFrame(self.frame, copy=True)
317+
df = DataFrame(float_frame, copy=True)
320318
column = df.columns[0]
321319

322320
# use the copy=False, change a column
@@ -332,29 +330,29 @@ def test_no_copy_blocks(self):
332330
# make sure we did change the original DataFrame
333331
assert _df[column].equals(df[column])
334332

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()
337335
cop['E'] = cop['A']
338-
assert 'E' not in self.frame
336+
assert 'E' not in float_frame
339337

340338
# 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
343341

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)
347345

348346
# buglet
349-
self.mixed_frame._data.ndim
347+
float_string_frame._data.ndim
350348

351349
# empty
352-
unpickled = tm.round_trip_pickle(self.empty)
350+
unpickled = tm.round_trip_pickle(empty_frame)
353351
repr(unpickled)
354352

355353
# 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)
358356

359357
def test_consolidate_datetime64(self):
360358
# numpy vstack bug
@@ -388,9 +386,9 @@ def test_consolidate_datetime64(self):
388386
df.starting), ser_starting.index)
389387
tm.assert_index_equal(pd.DatetimeIndex(df.ending), ser_ending.index)
390388

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
394392

395393
def test_get_numeric_data(self):
396394
# TODO(wesm): unused?
@@ -448,23 +446,23 @@ def test_get_numeric_data_extension_dtype(self):
448446
expected = df.loc[:, ['A', 'C']]
449447
assert_frame_equal(result, expected)
450448

451-
def test_convert_objects(self):
449+
def test_convert_objects(self, float_string_frame):
452450

453-
oops = self.mixed_frame.T.T
451+
oops = float_string_frame.T.T
454452
converted = oops._convert(datetime=True)
455-
assert_frame_equal(converted, self.mixed_frame)
453+
assert_frame_equal(converted, float_string_frame)
456454
assert converted['A'].dtype == np.float64
457455

458456
# 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'
461459

462460
# 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)
468466
assert converted['H'].dtype == 'float64'
469467
assert converted['I'].dtype == 'int64'
470468
assert converted['J'].dtype == 'float64'
@@ -473,14 +471,14 @@ def test_convert_objects(self):
473471
assert len(converted['K'].dropna()) == length - 5
474472

475473
# via astype
476-
converted = self.mixed_frame.copy()
474+
converted = float_string_frame.copy()
477475
converted['H'] = converted['H'].astype('float64')
478476
converted['I'] = converted['I'].astype('int64')
479477
assert converted['H'].dtype == 'float64'
480478
assert converted['I'].dtype == 'int64'
481479

482480
# via astype, but errors
483-
converted = self.mixed_frame.copy()
481+
converted = float_string_frame.copy()
484482
with tm.assert_raises_regex(ValueError, 'invalid literal'):
485483
converted['H'].astype('int32')
486484

0 commit comments

Comments
 (0)