Skip to content

Commit

Permalink
Move several tests to SharedWithSparse
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Jul 16, 2017
1 parent 14f7047 commit 5dcec57
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 134 deletions.
70 changes: 70 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8
# pylint: disable-msg=E1101,W0612
from collections import OrderedDict

import pytest

Expand Down Expand Up @@ -117,9 +118,78 @@ def test_to_sparse_pass_name(self):
result = self.ts.to_sparse()
assert result.name == self.ts.name

def test_constructor_dict(self):
d = {'a': 0., 'b': 1., 'c': 2.}
result = self.Series(d)
expected = self.Series(d, index=sorted(d.keys()))
tm.assert_series_equal(result, expected)

result = self.Series(d, index=['b', 'c', 'd', 'a'])
expected = self.Series([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a'])
tm.assert_series_equal(result, expected)

def test_constructor_subclass_dict(self):
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
series = self.Series(data)
expected = self.Series(dict(compat.iteritems(data)))
tm.assert_series_equal(series, expected)

def test_constructor_ordereddict(self):
# GH3283
data = OrderedDict(
('col%s' % i, np.random.random()) for i in range(12))

series = self.Series(data)
expected = self.Series(list(data.values()), list(data.keys()))
tm.assert_series_equal(series, expected)

# Test with subclass
class A(OrderedDict):
pass

series = self.Series(A(data))
tm.assert_series_equal(series, expected)

def test_constructor_dict_multiindex(self):
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.}
_d = sorted(d.items())
result = self.Series(d)
expected = self.Series(
[x[1] for x in _d],
index=pd.MultiIndex.from_tuples([x[0] for x in _d]))
tm.assert_series_equal(result, expected)

d['z'] = 111.
_d.insert(0, ('z', d['z']))
result = self.Series(d)
expected = self.Series([x[1] for x in _d],
index=pd.Index([x[0] for x in _d],
tupleize_cols=False))
result = result.reindex(index=expected.index)
tm.assert_series_equal(result, expected)

def test_constructor_dict_timedelta_index(self):
# GH #12169 : Resample category data with timedelta index
# construct Series from dict as data and TimedeltaIndex as index
# will result NaN in result Series data
expected = self.Series(
data=['A', 'B', 'C'],
index=pd.to_timedelta([0, 10, 20], unit='s')
)

result = self.Series(
data={pd.to_timedelta(0, unit='s'): 'A',
pd.to_timedelta(10, unit='s'): 'B',
pd.to_timedelta(20, unit='s'): 'C'},
index=pd.to_timedelta([0, 10, 20], unit='s')
)
tm.assert_series_equal(result, expected)


class TestSeriesMisc(TestData, SharedWithSparse):

Series = Series

def test_tab_completion(self):
# GH 9910
s = Series(list('abcd'))
Expand Down
62 changes: 0 additions & 62 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,48 +605,6 @@ def test_constructor_dict(self):
expected.iloc[1] = 1
assert_series_equal(result, expected)

def test_constructor_dict_multiindex(self):
check = lambda result, expected: tm.assert_series_equal(
result, expected, check_dtype=True, check_series_type=True)
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.}
_d = sorted(d.items())
ser = Series(d)
expected = Series([x[1] for x in _d],
index=MultiIndex.from_tuples([x[0] for x in _d]))
check(ser, expected)

d['z'] = 111.
_d.insert(0, ('z', d['z']))
ser = Series(d)
expected = Series([x[1] for x in _d], index=Index(
[x[0] for x in _d], tupleize_cols=False))
ser = ser.reindex(index=expected.index)
check(ser, expected)

def test_constructor_dict_timedelta_index(self):
# GH #12169 : Resample category data with timedelta index
# construct Series from dict as data and TimedeltaIndex as index
# will result NaN in result Series data
expected = Series(
data=['A', 'B', 'C'],
index=pd.to_timedelta([0, 10, 20], unit='s')
)

result = Series(
data={pd.to_timedelta(0, unit='s'): 'A',
pd.to_timedelta(10, unit='s'): 'B',
pd.to_timedelta(20, unit='s'): 'C'},
index=pd.to_timedelta([0, 10, 20], unit='s')
)
# this should work
assert_series_equal(result, expected)

def test_constructor_subclass_dict(self):
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
series = Series(data)
refseries = Series(dict(compat.iteritems(data)))
assert_series_equal(refseries, series)

def test_constructor_dict_datetime64_index(self):
# GH 9456

Expand All @@ -670,26 +628,6 @@ def create_data(constructor):
assert_series_equal(result_datetime, expected)
assert_series_equal(result_Timestamp, expected)

def test_orderedDict_ctor(self):
# GH3283
import pandas
import random
data = OrderedDict([('col%s' % i, random.random()) for i in range(12)])
s = pandas.Series(data)
assert all(s.values == list(data.values()))

def test_orderedDict_subclass_ctor(self):
# GH3283
import pandas
import random

class A(OrderedDict):
pass

data = A([('col%s' % i, random.random()) for i in range(12)])
s = pandas.Series(data)
assert all(s.values == list(data.values()))

def test_constructor_list_of_tuples(self):
data = [(1, 1), (2, 2), (2, 3)]
s = Series(data)
Expand Down
74 changes: 2 additions & 72 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pylint: disable-msg=E1101,W0612

import operator
from collections import OrderedDict
from datetime import datetime

import pytest
Expand Down Expand Up @@ -61,6 +60,8 @@ def _test_data2_zero():

class TestSparseSeries(SharedWithSparse):

Series = SparseSeries

def setup_method(self, method):
arr, index = _test_data1()

Expand Down Expand Up @@ -1366,61 +1367,6 @@ def test_numpy_func_call(self):
getattr(np, func)(getattr(self, series))


def test_constructor_dict():
d = {'a': 0., 'b': 1., 'c': 2.}
result = SparseSeries(d)
expected = SparseSeries(d, index=sorted(d.keys()))
tm.assert_sp_series_equal(result, expected)

result = SparseSeries(d, index=['b', 'c', 'd', 'a'])
expected = SparseSeries([1, 2, nan, 0], index=['b', 'c', 'd', 'a'])
tm.assert_sp_series_equal(result, expected)


def test_constructor_dict_multiindex():
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.}
_d = sorted(d.items())
result = SparseSeries(d)
expected = SparseSeries(
[x[1] for x in _d],
index=pd.MultiIndex.from_tuples([x[0] for x in _d]))
tm.assert_series_equal(result, expected)

d['z'] = 111.
_d.insert(0, ('z', d['z']))
result = SparseSeries(d)
expected = SparseSeries([x[1] for x in _d],
index=pd.Index([x[0] for x in _d],
tupleize_cols=False))
result = result.reindex(index=expected.index)
tm.assert_series_equal(result, expected)


def test_constructor_dict_timedelta_index():
# GH #12169 : Resample category data with timedelta index
# construct Series from dict as data and TimedeltaIndex as index
# will result NaN in result Series data
expected = SparseSeries(
data=['A', 'B', 'C'],
index=pd.to_timedelta([0, 10, 20], unit='s')
)

result = SparseSeries(
data={pd.to_timedelta(0, unit='s'): 'A',
pd.to_timedelta(10, unit='s'): 'B',
pd.to_timedelta(20, unit='s'): 'C'},
index=pd.to_timedelta([0, 10, 20], unit='s')
)
tm.assert_sp_series_equal(result, expected)


def test_constructor_subclass_dict():
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
series = SparseSeries(data)
expected = SparseSeries(dict(compat.iteritems(data)))
tm.assert_sp_series_equal(series, expected)


@pytest.mark.parametrize(
'datetime_type', (np.datetime64,
pd.Timestamp,
Expand All @@ -1434,19 +1380,3 @@ def test_constructor_dict_datetime64_index(datetime_type):
expected = SparseSeries(values, map(pd.Timestamp, dates))

tm.assert_sp_series_equal(result, expected)


def test_orderedDict_ctor():
# GH3283
data = OrderedDict(('col%s' % i, np.random.random()) for i in range(12))

series = SparseSeries(data)
expected = SparseSeries(list(data.values()), list(data.keys()))
tm.assert_sp_series_equal(series, expected)

# Test with subclass
class A(OrderedDict):
pass

series = SparseSeries(A(data))
tm.assert_sp_series_equal(series, expected)

0 comments on commit 5dcec57

Please sign in to comment.