Skip to content

Commit

Permalink
Add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Aug 10, 2018
1 parent b4d4e38 commit 32b618f
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 71 deletions.
121 changes: 121 additions & 0 deletions pandas/tests/frame/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import pytest

import numpy as np

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


@pytest.fixture
def frame():
return DataFrame(tm.getSeriesData())


@pytest.fixture
def frame2():
return DataFrame(tm.getSeriesData(), columns=['D', 'C', 'B', 'A'])


@pytest.fixture
def intframe():
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 tsframe():
return DataFrame(tm.getTimeSeriesData())


@pytest.fixture
def mixed_frame():
df = DataFrame(tm.getSeriesData())
df['foo'] = 'bar'
return df


@pytest.fixture
def mixed_float():
df = DataFrame(tm.getSeriesData())
df.A = df.A.astype('float16')
df.B = df.B.astype('float32')
df.C = df.C.astype('float64')
return df


@pytest.fixture
def mixed_float2():
df = DataFrame(tm.getSeriesData())
df.D = df.D.astype('float16')
df.C = df.C.astype('float32')
df.B = df.B.astype('float64')
return df


@pytest.fixture
def mixed_int():
df = DataFrame({k: v.astype(int)
for k, v in compat.iteritems(tm.getSeriesData())})
df.A = df.A.astype('uint8')
df.B = df.B.astype('int32')
df.C = df.C.astype('int64')
df.D = np.ones(len(df.D), dtype='uint64')
return df


@pytest.fixture
def all_mixed():
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 tzframe():
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():
return DataFrame({})


@pytest.fixture
def ts1():
return tm.makeTimeSeries(nper=30)


@pytest.fixture
def ts2():
return tm.makeTimeSeries(nper=30)[5:]


@pytest.fixture
def simple():
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():
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)})
return df
129 changes: 58 additions & 71 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,19 @@
from pandas.tests.frame.common import TestData


@pytest.fixture
def frame_of_index_cols():
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)})
return df


class TestDataFrameAlterAxes(TestData):

def test_set_index_directly(self):
df = self.mixed_frame.copy()
def test_set_index_directly(self, mixed_frame):
df = mixed_frame
idx = Index(np.arange(len(df))[::-1])

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

def test_set_index(self):
df = self.mixed_frame.copy()
def test_set_index(self, mixed_frame):
df = mixed_frame
idx = Index(np.arange(len(df))[::-1])

df = df.set_index(idx)
Expand Down Expand Up @@ -408,11 +398,11 @@ def test_set_index_empty_column(self):
names=['a', 'x'])
tm.assert_frame_equal(result, expected)

def test_set_columns(self):
cols = Index(np.arange(len(self.mixed_frame.columns)))
self.mixed_frame.columns = cols
def test_set_columns(self, mixed_frame):
cols = Index(np.arange(len(mixed_frame.columns)))
mixed_frame.columns = cols
with tm.assert_raises_regex(ValueError, 'Length mismatch'):
self.mixed_frame.columns = cols[::2]
mixed_frame.columns = cols[::2]

def test_dti_set_index_reindex(self):
# GH 6631
Expand Down Expand Up @@ -440,20 +430,20 @@ def test_dti_set_index_reindex(self):

# Renaming

def test_rename(self):
def test_rename(self, frame):
mapping = {
'A': 'a',
'B': 'b',
'C': 'c',
'D': 'd'
}

renamed = self.frame.rename(columns=mapping)
renamed2 = self.frame.rename(columns=str.lower)
renamed = frame.rename(columns=mapping)
renamed2 = frame.rename(columns=str.lower)

tm.assert_frame_equal(renamed, renamed2)
tm.assert_frame_equal(renamed2.rename(columns=str.upper),
self.frame, check_names=False)
frame, check_names=False)

# index
data = {
Expand All @@ -469,14 +459,14 @@ def test_rename(self):
tm.assert_index_equal(renamed.index, Index(['BAR', 'FOO']))

# have to pass something
pytest.raises(TypeError, self.frame.rename)
pytest.raises(TypeError, frame.rename)

# partial columns
renamed = self.frame.rename(columns={'C': 'foo', 'D': 'bar'})
renamed = frame.rename(columns={'C': 'foo', 'D': 'bar'})
tm.assert_index_equal(renamed.columns, Index(['A', 'B', 'foo', 'bar']))

# other axis
renamed = self.frame.T.rename(index={'C': 'foo', 'D': 'bar'})
renamed = frame.T.rename(index={'C': 'foo', 'D': 'bar'})
tm.assert_index_equal(renamed.index, Index(['A', 'B', 'foo', 'bar']))

# index with name
Expand All @@ -487,9 +477,8 @@ def test_rename(self):
Index(['bar', 'foo'], name='name'))
assert renamed.index.name == renamer.index.name

def test_rename_axis_inplace(self):
def test_rename_axis_inplace(self, frame):
# GH 15704
frame = self.frame.copy()
expected = frame.rename_axis('foo')
result = frame.copy()
no_return = result.rename_axis('foo', inplace=True)
Expand Down Expand Up @@ -598,18 +587,18 @@ def test_rename_multiindex(self):
level=0)
tm.assert_index_equal(renamed.index, new_index)

def test_rename_nocopy(self):
renamed = self.frame.rename(columns={'C': 'foo'}, copy=False)
def test_rename_nocopy(self, frame):
renamed = frame.rename(columns={'C': 'foo'}, copy=False)
renamed['foo'] = 1.
assert (self.frame['C'] == 1.).all()
assert (frame['C'] == 1.).all()

def test_rename_inplace(self):
self.frame.rename(columns={'C': 'foo'})
assert 'C' in self.frame
assert 'foo' not in self.frame
def test_rename_inplace(self, frame):
frame.rename(columns={'C': 'foo'})
assert 'C' in frame
assert 'foo' not in frame

c_id = id(self.frame['C'])
frame = self.frame.copy()
c_id = id(frame['C'])
frame = frame.copy()
frame.rename(columns={'C': 'foo'}, inplace=True)

assert 'C' not in frame
Expand Down Expand Up @@ -682,8 +671,8 @@ def test_reorder_levels(self):
result = df.reorder_levels(['L0', 'L0', 'L0'])
tm.assert_frame_equal(result, expected)

def test_reset_index(self):
stacked = self.frame.stack()[::2]
def test_reset_index(self, frame):
stacked = frame.stack()[::2]
stacked = DataFrame({'foo': stacked, 'bar': stacked})

names = ['first', 'second']
Expand All @@ -703,55 +692,55 @@ def test_reset_index(self):
check_names=False)

# default name assigned
rdf = self.frame.reset_index()
exp = Series(self.frame.index.values, name='index')
rdf = frame.reset_index()
exp = Series(frame.index.values, name='index')
tm.assert_series_equal(rdf['index'], exp)

# default name assigned, corner case
df = self.frame.copy()
df = frame.copy()
df['index'] = 'foo'
rdf = df.reset_index()
exp = Series(self.frame.index.values, name='level_0')
exp = Series(frame.index.values, name='level_0')
tm.assert_series_equal(rdf['level_0'], exp)

# but this is ok
self.frame.index.name = 'index'
deleveled = self.frame.reset_index()
tm.assert_series_equal(deleveled['index'], Series(self.frame.index))
frame.index.name = 'index'
deleveled = frame.reset_index()
tm.assert_series_equal(deleveled['index'], Series(frame.index))
tm.assert_index_equal(deleveled.index,
Index(np.arange(len(deleveled))))

# preserve column names
self.frame.columns.name = 'columns'
resetted = self.frame.reset_index()
frame.columns.name = 'columns'
resetted = frame.reset_index()
assert resetted.columns.name == 'columns'

# only remove certain columns
frame = self.frame.reset_index().set_index(['index', 'A', 'B'])
rs = frame.reset_index(['A', 'B'])
df = frame.reset_index().set_index(['index', 'A', 'B'])
rs = df.reset_index(['A', 'B'])

# TODO should reset_index check_names ?
tm.assert_frame_equal(rs, self.frame, check_names=False)
tm.assert_frame_equal(rs, frame, check_names=False)

rs = frame.reset_index(['index', 'A', 'B'])
tm.assert_frame_equal(rs, self.frame.reset_index(), check_names=False)
rs = df.reset_index(['index', 'A', 'B'])
tm.assert_frame_equal(rs, frame.reset_index(), check_names=False)

rs = frame.reset_index(['index', 'A', 'B'])
tm.assert_frame_equal(rs, self.frame.reset_index(), check_names=False)
rs = df.reset_index(['index', 'A', 'B'])
tm.assert_frame_equal(rs, frame.reset_index(), check_names=False)

rs = frame.reset_index('A')
xp = self.frame.reset_index().set_index(['index', 'B'])
rs = df.reset_index('A')
xp = frame.reset_index().set_index(['index', 'B'])
tm.assert_frame_equal(rs, xp, check_names=False)

# test resetting in place
df = self.frame.copy()
resetted = self.frame.reset_index()
df = frame.copy()
resetted = frame.reset_index()
df.reset_index(inplace=True)
tm.assert_frame_equal(df, resetted, check_names=False)

frame = self.frame.reset_index().set_index(['index', 'A', 'B'])
rs = frame.reset_index('A', drop=True)
xp = self.frame.copy()
df = frame.reset_index().set_index(['index', 'A', 'B'])
rs = df.reset_index('A', drop=True)
xp = frame.copy()
del xp['A']
xp = xp.set_index(['B'], append=True)
tm.assert_frame_equal(rs, xp, check_names=False)
Expand Down Expand Up @@ -929,8 +918,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):
renamed = self.mixed_frame.rename(columns=str.upper)
def test_rename_objects(self, mixed_frame):
renamed = mixed_frame.rename(columns=str.upper)

assert 'FOO' in renamed
assert 'foo' not in renamed
Expand Down Expand Up @@ -1053,15 +1042,13 @@ def test_rename_positional(self):
assert 'rename' in message
assert 'Use named arguments' in message

def test_assign_columns(self):
self.frame['hi'] = 'there'
def test_assign_columns(self, frame):
frame['hi'] = 'there'

frame = self.frame.copy()
frame.columns = ['foo', 'bar', 'baz', 'quux', 'foo2']
tm.assert_series_equal(self.frame['C'], frame['baz'],
check_names=False)
tm.assert_series_equal(self.frame['hi'], frame['foo2'],
check_names=False)
df = frame.copy()
df.columns = ['foo', 'bar', 'baz', 'quux', 'foo2']
tm.assert_series_equal(frame['C'], df['baz'], check_names=False)
tm.assert_series_equal(frame['hi'], df['foo2'], check_names=False)

def test_set_index_preserve_categorical_dtype(self):
# GH13743, GH13854
Expand Down

0 comments on commit 32b618f

Please sign in to comment.