Skip to content

BUG: SparseSeries init from dict fixes #16906

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 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
self.Series -> self.series_klass
  • Loading branch information
kernc committed Jul 17, 2017
commit 659559c8f11bb71ea1b74c678b9ed319df86165e
32 changes: 16 additions & 16 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,49 +120,49 @@ def test_to_sparse_pass_name(self):

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

@gfyoung gfyoung Jul 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think GitHub hid away my comment about this, but I think we should not interlace assert_series_equal and assert_sp_series_equal. It reduces modularity, and "assert_series_equal" is confusing for SparseSeries I find.

I propose that we do the following and define this method:

def assert_series_klass_equal(result, expected):
    klass_name = self.series_klass.__name__

    if klass_name == "Series":
        tm.assert_series_equal(result, expected)
    elif klass_name == "SparseSeries":
        tm.assert_sp_series_equal(result, expected)
    else:
        raise ValueError("Invalid 'series_klass' : {name}".format(name=klass_name))

That way you also don't need to modify assert_series_equal. You can then call this method without worrying what type of Series you are comparing.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't check sparseness. I might modify assert_series_equal to dispatch to assert_sp_series_equal if both are SparseSeries.

Copy link
Member

@gfyoung gfyoung Jul 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In light of my comment below, I'd rather that we keep sparse equality and Series equality checks separate. Perhaps if we could write a function like:

def _check_series_equal(self, left, right):
     ...

that dispatches to tm.assert_series_equal OR tm.assert_sp_series_equal depending on test class. It would seem a little clearer implementation-wise.


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)))
series = self.series_klass(data)
expected = self.series_klass(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()))
series = self.series_klass(data)
expected = self.series_klass(list(data.values()), list(data.keys()))
tm.assert_series_equal(series, expected)

# Test with subclass
class A(OrderedDict):
pass

series = self.Series(A(data))
series = self.series_klass(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(
result = self.series_klass(d)
expected = self.series_klass(
[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],
result = self.series_klass(d)
expected = self.series_klass([x[1] for x in _d],
index=pd.Index([x[0] for x in _d],
tupleize_cols=False))
result = result.reindex(index=expected.index)
Expand All @@ -172,12 +172,12 @@ 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(
expected = self.series_klass(
data=['A', 'B', 'C'],
index=pd.to_timedelta([0, 10, 20], unit='s')
)

result = self.Series(
result = self.series_klass(
data={pd.to_timedelta(0, unit='s'): 'A',
pd.to_timedelta(10, unit='s'): 'B',
pd.to_timedelta(20, unit='s'): 'C'},
Expand All @@ -188,7 +188,7 @@ def test_constructor_dict_timedelta_index(self):

class TestSeriesMisc(TestData, SharedWithSparse):

Series = Series
series_klass = Series

def test_tab_completion(self):
# GH 9910
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _test_data2_zero():

class TestSparseSeries(SharedWithSparse):

Series = SparseSeries
series_klass = SparseSeries

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