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 2 commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Sparse
^^^^^^


- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index=`` kwarg (:issue:`16905`)
Copy link
Member

Choose a reason for hiding this comment

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

If it doesn't matter whether index is passed in, why mention it in the description?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a reference to the two issues fixed. With, the result was invalid; without, it crashed.

Copy link
Member

@gfyoung gfyoung Jul 15, 2017

Choose a reason for hiding this comment

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

Fair enough, though ultimately instantiating from dict just didn't work at all though, which could be considered a single bug (also you're only referencing one issue here). Note that without further context, people won't be aware of that difference (whether it was incorrect or crashed), so it is preferable to be concise.


Reshaping
^^^^^^^^^
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,8 @@ def __init__(self, data=None, index=None, sparse_index=None, kind='block',
data = data._data

elif isinstance(data, (Series, dict)):
if index is None:
index = data.index.view()

data = Series(data)
data = Series(data, index=index)
index = data.index
res = make_sparse(data, kind=kind, fill_value=fill_value)
data, sparse_index, fill_value = res

Expand Down
89 changes: 89 additions & 0 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# pylint: disable-msg=E1101,W0612

import operator
from collections import OrderedDict
from datetime import datetime

import pytest

from numpy import nan
Expand Down Expand Up @@ -1361,3 +1364,89 @@ def test_numpy_func_call(self):
for func in funcs:
for series in ('bseries', 'zbseries'):
getattr(np, func)(getattr(self, series))


def test_constructor_dict():
d = {'a': 0., 'b': 1., 'c': 2.}
Copy link
Contributor

Choose a reason for hiding this comment

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

you might be able to move some of these into from pandas.tests.series.test_api import SharedWithSparse whech we already import (rather than directly copying them).

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,
lambda x: datetime.strptime(x, '%Y-%m-%d')))
def test_constructor_dict_datetime64_index(datetime_type):
# GH 9456
dates = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15']
values = [42544017.198965244, 1234565, 40512335.181958228, -1]

result = SparseSeries(dict(zip(map(datetime_type, dates), values)))
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)