Skip to content

Commit

Permalink
TST: Move more frame tests to SharedWithSparse (pandas-dev#17227)
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc authored and alanbato committed Nov 10, 2017
1 parent e2ffa72 commit 1baa14f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 59 deletions.
132 changes: 73 additions & 59 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@


class SharedWithSparse(object):
"""
A collection of tests DataFrame and SparseDataFrame can share.
In generic tests on this class, use ``self._assert_frame_equal()`` and
``self._assert_series_equal()`` which are implemented in sub-classes
and dispatch correctly.
"""
def _assert_frame_equal(self, left, right):
"""Dispatch to frame class dependent assertion"""
raise NotImplementedError

def _assert_series_equal(self, left, right):
"""Dispatch to series class dependent assertion"""
raise NotImplementedError

def test_copy_index_name_checking(self):
# don't want to be able to modify the index stored elsewhere after
Expand Down Expand Up @@ -76,11 +90,6 @@ def test_add_prefix_suffix(self):
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
tm.assert_index_equal(with_pct_suffix.columns, expected)


class TestDataFrameMisc(SharedWithSparse, TestData):

klass = DataFrame

def test_get_axis(self):
f = self.frame
assert f._get_axis_number(0) == 0
Expand Down Expand Up @@ -118,13 +127,13 @@ def test_column_contains_typeerror(self):
pass

def test_not_hashable(self):
df = pd.DataFrame([1])
df = self.klass([1])
pytest.raises(TypeError, hash, df)
pytest.raises(TypeError, hash, self.empty)

def test_new_empty_index(self):
df1 = DataFrame(randn(0, 3))
df2 = DataFrame(randn(0, 3))
df1 = self.klass(randn(0, 3))
df2 = self.klass(randn(0, 3))
df1.index.name = 'foo'
assert df2.index.name is None

Expand All @@ -135,7 +144,7 @@ def test_array_interface(self):
assert result.index is self.frame.index
assert result.columns is self.frame.columns

assert_frame_equal(result, self.frame.apply(np.sqrt))
self._assert_frame_equal(result, self.frame.apply(np.sqrt))

def test_get_agg_axis(self):
cols = self.frame._get_agg_axis(0)
Expand All @@ -160,36 +169,36 @@ def test_nonzero(self):
assert not df.empty

def test_iteritems(self):
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=['a', 'a', 'b'])
df = self.klass([[1, 2, 3], [4, 5, 6]], columns=['a', 'a', 'b'])
for k, v in compat.iteritems(df):
assert type(v) == Series
assert type(v) == self.klass._constructor_sliced

def test_iter(self):
assert tm.equalContents(list(self.frame), self.frame.columns)

def test_iterrows(self):
for i, (k, v) in enumerate(self.frame.iterrows()):
exp = self.frame.xs(self.frame.index[i])
assert_series_equal(v, exp)
for k, v in self.frame.iterrows():
exp = self.frame.loc[k]
self._assert_series_equal(v, exp)

for i, (k, v) in enumerate(self.mixed_frame.iterrows()):
exp = self.mixed_frame.xs(self.mixed_frame.index[i])
assert_series_equal(v, exp)
for k, v in self.mixed_frame.iterrows():
exp = self.mixed_frame.loc[k]
self._assert_series_equal(v, exp)

def test_itertuples(self):
for i, tup in enumerate(self.frame.itertuples()):
s = Series(tup[1:])
s = self.klass._constructor_sliced(tup[1:])
s.name = tup[0]
expected = self.frame.iloc[i, :].reset_index(drop=True)
assert_series_equal(s, expected)
self._assert_series_equal(s, expected)

df = DataFrame({'floats': np.random.randn(5),
'ints': lrange(5)}, columns=['floats', 'ints'])
df = self.klass({'floats': np.random.randn(5),
'ints': lrange(5)}, columns=['floats', 'ints'])

for tup in df.itertuples(index=False):
assert isinstance(tup[1], np.integer)

df = DataFrame(data={"a": [1, 2, 3], "b": [4, 5, 6]})
df = self.klass(data={"a": [1, 2, 3], "b": [4, 5, 6]})
dfaa = df[['a', 'a']]

assert (list(dfaa.itertuples()) ==
Expand Down Expand Up @@ -237,7 +246,7 @@ def test_as_matrix(self):
mat = self.mixed_frame.as_matrix(['foo', 'A'])
assert mat[0, 0] == 'bar'

df = DataFrame({'real': [1, 2, 3], 'complex': [1j, 2j, 3j]})
df = self.klass({'real': [1, 2, 3], 'complex': [1j, 2j, 3j]})
mat = df.as_matrix()
assert mat[0, 0] == 1j

Expand All @@ -246,20 +255,6 @@ def test_as_matrix(self):
expected = self.frame.reindex(columns=['A', 'B']).values
assert_almost_equal(mat, expected)

def test_values(self):
self.frame.values[:, 0] = 5.
assert (self.frame.values[:, 0] == 5).all()

def test_deepcopy(self):
cp = deepcopy(self.frame)
series = cp['A']
series[:] = 10
for idx, value in compat.iteritems(series):
assert self.frame['A'][idx] != value

# ---------------------------------------------------------------------
# Transposing

def test_transpose(self):
frame = self.frame
dft = frame.T
Expand All @@ -272,23 +267,17 @@ def test_transpose(self):

# mixed type
index, data = tm.getMixedTypeDict()
mixed = DataFrame(data, index=index)
mixed = self.klass(data, index=index)

mixed_T = mixed.T
for col, s in compat.iteritems(mixed_T):
assert s.dtype == np.object_

def test_transpose_get_view(self):
dft = self.frame.T
dft.values[:, 5:10] = 5

assert (self.frame.values[5:10] == 5).all()

def test_swapaxes(self):
df = DataFrame(np.random.randn(10, 5))
assert_frame_equal(df.T, df.swapaxes(0, 1))
assert_frame_equal(df.T, df.swapaxes(1, 0))
assert_frame_equal(df, df.swapaxes(0, 0))
df = self.klass(np.random.randn(10, 5))
self._assert_frame_equal(df.T, df.swapaxes(0, 1))
self._assert_frame_equal(df.T, df.swapaxes(1, 0))
self._assert_frame_equal(df, df.swapaxes(0, 0))
pytest.raises(ValueError, df.swapaxes, 2, 5)

def test_axis_aliases(self):
Expand All @@ -308,8 +297,8 @@ def test_more_asMatrix(self):
assert values.shape[1] == len(self.mixed_frame.columns)

def test_repr_with_mi_nat(self):
df = DataFrame({'X': [1, 2]},
index=[[pd.NaT, pd.Timestamp('20130101')], ['a', 'b']])
df = self.klass({'X': [1, 2]},
index=[[pd.NaT, pd.Timestamp('20130101')], ['a', 'b']])
res = repr(df)
exp = ' X\nNaT a 1\n2013-01-01 b 2'
assert res == exp
Expand All @@ -324,31 +313,56 @@ def test_series_put_names(self):
assert v.name == k

def test_empty_nonzero(self):
df = DataFrame([1, 2, 3])
df = self.klass([1, 2, 3])
assert not df.empty
df = pd.DataFrame(index=[1], columns=[1])
df = self.klass(index=[1], columns=[1])
assert not df.empty
df = DataFrame(index=['a', 'b'], columns=['c', 'd']).dropna()
df = self.klass(index=['a', 'b'], columns=['c', 'd']).dropna()
assert df.empty
assert df.T.empty
empty_frames = [pd.DataFrame(),
pd.DataFrame(index=[1]),
pd.DataFrame(columns=[1]),
pd.DataFrame({1: []})]
empty_frames = [self.klass(),
self.klass(index=[1]),
self.klass(columns=[1]),
self.klass({1: []})]
for df in empty_frames:
assert df.empty
assert df.T.empty

def test_with_datetimelikes(self):

df = DataFrame({'A': date_range('20130101', periods=10),
'B': timedelta_range('1 day', periods=10)})
df = self.klass({'A': date_range('20130101', periods=10),
'B': timedelta_range('1 day', periods=10)})
t = df.T

result = t.get_dtype_counts()
expected = Series({'object': 10})
tm.assert_series_equal(result, expected)


class TestDataFrameMisc(SharedWithSparse, TestData):

klass = DataFrame
# SharedWithSparse tests use generic, klass-agnostic assertion
_assert_frame_equal = staticmethod(assert_frame_equal)
_assert_series_equal = staticmethod(assert_series_equal)

def test_values(self):
self.frame.values[:, 0] = 5.
assert (self.frame.values[:, 0] == 5).all()

def test_deepcopy(self):
cp = deepcopy(self.frame)
series = cp['A']
series[:] = 10
for idx, value in compat.iteritems(series):
assert self.frame['A'][idx] != value

def test_transpose_get_view(self):
dft = self.frame.T
dft.values[:, 5:10] = 5

assert (self.frame.values[5:10] == 5).all()

def test_inplace_return_self(self):
# re #1893

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
class TestSparseDataFrame(SharedWithSparse):
klass = SparseDataFrame

# SharedWithSparse tests use generic, klass-agnostic assertion
_assert_frame_equal = staticmethod(tm.assert_sp_frame_equal)
_assert_series_equal = staticmethod(tm.assert_sp_series_equal)

def setup_method(self, method):
self.data = {'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6],
'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6],
Expand All @@ -43,6 +47,8 @@ def setup_method(self, method):
self.frame = SparseDataFrame(self.data, index=self.dates)
self.iframe = SparseDataFrame(self.data, index=self.dates,
default_kind='integer')
self.mixed_frame = self.frame.copy(False)
self.mixed_frame['foo'] = pd.SparseArray(['bar'] * len(self.dates))

values = self.frame.values.copy()
values[np.isnan(values)] = 0
Expand Down

0 comments on commit 1baa14f

Please sign in to comment.