Skip to content

TST: Remove subset of singleton fixtures #24873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
185 changes: 1 addition & 184 deletions pandas/tests/frame/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from pandas import DataFrame, NaT, compat, date_range
from pandas import DataFrame
import pandas.util.testing as tm


Expand All @@ -15,30 +15,6 @@ def float_frame():
return DataFrame(tm.getSeriesData())


@pytest.fixture
def float_frame_with_na():
"""
Fixture for DataFrame of floats with index of unique strings

Columns are ['A', 'B', 'C', 'D']; some entries are missing
"""
df = DataFrame(tm.getSeriesData())
# set some NAs
df.loc[5:10] = np.nan
df.loc[15:20, -2:] = np.nan
return df


@pytest.fixture
def float_frame2():
"""
Fixture for DataFrame of floats with index of unique strings

Columns are ['D', 'C', 'B', 'A']
"""
return DataFrame(tm.getSeriesData(), columns=['D', 'C', 'B', 'A'])


@pytest.fixture
def bool_frame_with_na():
"""
Expand All @@ -54,168 +30,9 @@ def bool_frame_with_na():
return df


@pytest.fixture
def int_frame():
"""
Fixture for DataFrame of ints with index of unique strings

Columns are ['A', 'B', 'C', 'D']
"""
df = DataFrame({k: v.astype(int)
for k, v in compat.iteritems(tm.getSeriesData())})
# force these all to int64 to avoid platform testing issues
return DataFrame({c: s for c, s in compat.iteritems(df)}, dtype=np.int64)


@pytest.fixture
def datetime_frame():
"""
Fixture for DataFrame of floats with DatetimeIndex

Columns are ['A', 'B', 'C', 'D']
"""
return DataFrame(tm.getTimeSeriesData())


@pytest.fixture
def float_string_frame():
"""
Fixture for DataFrame of floats and strings with index of unique strings

Columns are ['A', 'B', 'C', 'D', 'foo'].
"""
df = DataFrame(tm.getSeriesData())
df['foo'] = 'bar'
return df


@pytest.fixture
def mixed_float_frame():
"""
Fixture for DataFrame of different float types with index of unique strings

Columns are ['A', 'B', 'C', 'D'].
"""
df = DataFrame(tm.getSeriesData())
df.A = df.A.astype('float32')
df.B = df.B.astype('float32')
df.C = df.C.astype('float16')
df.D = df.D.astype('float64')
return df


@pytest.fixture
def mixed_float_frame2():
"""
Fixture for DataFrame of different float types with index of unique strings

Columns are ['A', 'B', 'C', 'D'].
"""
df = DataFrame(tm.getSeriesData())
df.D = df.D.astype('float32')
df.C = df.C.astype('float32')
df.B = df.B.astype('float16')
df.D = df.D.astype('float64')
return df


@pytest.fixture
def mixed_int_frame():
"""
Fixture for DataFrame of different int types with index of unique strings

Columns are ['A', 'B', 'C', 'D'].
"""
df = DataFrame({k: v.astype(int)
for k, v in compat.iteritems(tm.getSeriesData())})
df.A = df.A.astype('int32')
df.B = np.ones(len(df.B), dtype='uint64')
df.C = df.C.astype('uint8')
df.D = df.C.astype('int64')
return df


@pytest.fixture
def mixed_type_frame():
"""
Fixture for DataFrame of float/int/string columns with RangeIndex

Columns are ['a', 'b', 'c', 'float32', 'int32'].
"""
return DataFrame({'a': 1., 'b': 2, 'c': 'foo',
'float32': np.array([1.] * 10, dtype='float32'),
'int32': np.array([1] * 10, dtype='int32')},
index=np.arange(10))


@pytest.fixture
def timezone_frame():
"""
Fixture for DataFrame of date_range Series with different time zones

Columns are ['A', 'B', 'C']; some entries are missing
"""
df = DataFrame({'A': date_range('20130101', periods=3),
'B': date_range('20130101', periods=3,
tz='US/Eastern'),
'C': date_range('20130101', periods=3,
tz='CET')})
df.iloc[1, 1] = NaT
df.iloc[1, 2] = NaT
return df


@pytest.fixture
def empty_frame():
"""
Fixture for empty DataFrame
"""
return DataFrame({})


@pytest.fixture
def datetime_series():
"""
Fixture for Series of floats with DatetimeIndex
"""
return tm.makeTimeSeries(nper=30)


@pytest.fixture
def datetime_series_short():
"""
Fixture for Series of floats with DatetimeIndex
"""
return tm.makeTimeSeries(nper=30)[5:]


@pytest.fixture
def simple_frame():
"""
Fixture for simple 3x3 DataFrame

Columns are ['one', 'two', 'three'], index is ['a', 'b', 'c'].
"""
arr = np.array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])

return DataFrame(arr, columns=['one', 'two', 'three'],
index=['a', 'b', 'c'])


@pytest.fixture
def frame_of_index_cols():
"""
Fixture for DataFrame of columns that can be used for indexing

Columns are ['A', 'B', 'C', 'D', 'E', ('tuple', 'as', 'label')];
'A' & 'B' contain duplicates (but are jointly unique), the rest are unique.
"""
df = DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar'],
'B': ['one', 'two', 'three', 'one', 'two'],
'C': ['a', 'b', 'c', 'd', 'e'],
'D': np.random.randn(5),
'E': np.random.randn(5),
('tuple', 'as', 'label'): np.random.randn(5)})
return df
60 changes: 29 additions & 31 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

class TestDataFrameAlterAxes():

def test_set_index_directly(self, float_string_frame):
df = float_string_frame
def test_set_index_directly(self):
df = tm.get_float_string_frame()
idx = Index(np.arange(len(df))[::-1])

df.index = idx
tm.assert_index_equal(df.index, idx)
with pytest.raises(ValueError, match='Length mismatch'):
df.index = idx[::2]

def test_set_index(self, float_string_frame):
df = float_string_frame
def test_set_index(self):
df = tm.get_float_string_frame()
idx = Index(np.arange(len(df))[::-1])

df = df.set_index(idx)
Expand All @@ -51,9 +51,8 @@ def test_set_index_cast(self):
('tuple', 'as', 'label')])
@pytest.mark.parametrize('inplace', [True, False])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_drop_inplace(self, frame_of_index_cols,
drop, inplace, keys):
df = frame_of_index_cols
def test_set_index_drop_inplace(self, drop, inplace, keys):
df = tm.get_frame_of_index_cols()

if isinstance(keys, list):
idx = MultiIndex.from_arrays([df[x] for x in keys], names=keys)
Expand All @@ -74,8 +73,8 @@ def test_set_index_drop_inplace(self, frame_of_index_cols,
@pytest.mark.parametrize('keys', ['A', 'C', ['A', 'B'],
('tuple', 'as', 'label')])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_append(self, frame_of_index_cols, drop, keys):
df = frame_of_index_cols
def test_set_index_append(self, drop, keys):
df = tm.get_frame_of_index_cols()

keys = keys if isinstance(keys, list) else [keys]
idx = MultiIndex.from_arrays([df.index] + [df[x] for x in keys],
Expand All @@ -91,8 +90,8 @@ def test_set_index_append(self, frame_of_index_cols, drop, keys):
@pytest.mark.parametrize('keys', ['A', 'C', ['A', 'B'],
('tuple', 'as', 'label')])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_append_to_multiindex(self, frame_of_index_cols,
drop, keys):
def test_set_index_append_to_multiindex(self, drop, keys):
frame_of_index_cols = tm.get_frame_of_index_cols()
# append to existing multiindex
df = frame_of_index_cols.set_index(['D'], drop=drop, append=True)

Expand Down Expand Up @@ -123,9 +122,8 @@ def test_set_index_after_mutation(self):
@pytest.mark.parametrize('append, index_name', [(True, None),
(True, 'B'), (True, 'test'), (False, None)])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_pass_single_array(self, frame_of_index_cols,
drop, append, index_name, box):
df = frame_of_index_cols
def test_set_index_pass_single_array(self, drop, append, index_name, box):
df = tm.get_frame_of_index_cols()
df.index.name = index_name

key = box(df['B'])
Expand Down Expand Up @@ -156,9 +154,8 @@ def test_set_index_pass_single_array(self, frame_of_index_cols,
[(True, None), (True, 'A'), (True, 'B'),
(True, 'test'), (False, None)])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_pass_arrays(self, frame_of_index_cols,
drop, append, index_name, box):
df = frame_of_index_cols
def test_set_index_pass_arrays(self, drop, append, index_name, box):
df = tm.get_frame_of_index_cols()
df.index.name = index_name

keys = ['A', box(df['B'])]
Expand Down Expand Up @@ -187,9 +184,9 @@ def test_set_index_pass_arrays(self, frame_of_index_cols,
@pytest.mark.parametrize('append, index_name', [(True, None),
(True, 'A'), (True, 'test'), (False, None)])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_pass_arrays_duplicate(self, frame_of_index_cols, drop,
def test_set_index_pass_arrays_duplicate(self, drop,
append, index_name, box1, box2):
df = frame_of_index_cols
df = tm.get_frame_of_index_cols()
df.index.name = index_name

keys = [box1(df['A']), box2(df['A'])]
Expand All @@ -209,9 +206,8 @@ def test_set_index_pass_arrays_duplicate(self, frame_of_index_cols, drop,

@pytest.mark.parametrize('append', [True, False])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_pass_multiindex(self, frame_of_index_cols,
drop, append):
df = frame_of_index_cols
def test_set_index_pass_multiindex(self, drop, append):
df = tm.get_frame_of_index_cols()
keys = MultiIndex.from_arrays([df['A'], df['B']], names=['A', 'B'])

result = df.set_index(keys, drop=drop, append=append)
Expand All @@ -221,8 +217,8 @@ def test_set_index_pass_multiindex(self, frame_of_index_cols,

tm.assert_frame_equal(result, expected)

def test_set_index_verify_integrity(self, frame_of_index_cols):
df = frame_of_index_cols
def test_set_index_verify_integrity(self):
df = tm.get_frame_of_index_cols()

with pytest.raises(ValueError, match='Index has duplicate keys'):
df.set_index('A', verify_integrity=True)
Expand All @@ -232,8 +228,8 @@ def test_set_index_verify_integrity(self, frame_of_index_cols):

@pytest.mark.parametrize('append', [True, False])
@pytest.mark.parametrize('drop', [True, False])
def test_set_index_raise_keys(self, frame_of_index_cols, drop, append):
df = frame_of_index_cols
def test_set_index_raise_keys(self, drop, append):
df = tm.get_frame_of_index_cols()

with pytest.raises(KeyError, match="['foo', 'bar', 'baz']"):
# column names are A-E, as well as one tuple
Expand All @@ -256,9 +252,8 @@ def test_set_index_raise_keys(self, frame_of_index_cols, drop, append):
@pytest.mark.parametrize('append', [True, False])
@pytest.mark.parametrize('drop', [True, False])
@pytest.mark.parametrize('box', [set, iter])
def test_set_index_raise_on_type(self, frame_of_index_cols, box,
drop, append):
df = frame_of_index_cols
def test_set_index_raise_on_type(self, box, drop, append):
df = tm.get_frame_of_index_cols()

msg = 'The parameter "keys" may be a column key, .*'
# forbidden type, e.g. set/tuple/iter
Expand Down Expand Up @@ -440,7 +435,9 @@ def test_set_index_empty_column(self):
names=['a', 'x'])
tm.assert_frame_equal(result, expected)

def test_set_columns(self, float_string_frame):
def test_set_columns(self):
float_string_frame = tm.get_float_string_frame()

cols = Index(np.arange(len(float_string_frame.columns)))
float_string_frame.columns = cols
with pytest.raises(ValueError, match='Length mismatch'):
Expand Down Expand Up @@ -1015,7 +1012,8 @@ def test_set_index_names(self):
# Check equality
tm.assert_index_equal(df.set_index([df.index, idx2]).index, mi2)

def test_rename_objects(self, float_string_frame):
def test_rename_objects(self):
float_string_frame = tm.get_float_string_frame()
renamed = float_string_frame.rename(columns=str.upper)

assert 'FOO' in renamed
Expand Down
Loading