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
Next Next commit
fixup! BUG: SparseSeries init from dict fixes
  • Loading branch information
kernc committed Jul 13, 2017
commit 14f70478d966b2e289a50f9c7d04ca7a53a5d372
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Sparse
^^^^^^


- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index`` (:issue:`16905`)
- 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
51 changes: 22 additions & 29 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,20 +1380,20 @@ def test_constructor_dict():
def test_constructor_dict_multiindex():
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.}
_d = sorted(d.items())
ser = SparseSeries(d)
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(ser, expected)
tm.assert_series_equal(result, expected)

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


def test_constructor_dict_timedelta_index():
Expand All @@ -1417,43 +1417,36 @@ def test_constructor_dict_timedelta_index():
def test_constructor_subclass_dict():
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
series = SparseSeries(data)
refseries = SparseSeries(dict(compat.iteritems(data)))
tm.assert_sp_series_equal(refseries, series)
expected = SparseSeries(dict(compat.iteritems(data)))
tm.assert_sp_series_equal(series, expected)


def test_constructor_dict_datetime64_index():
@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_as_str = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15']
dates = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15']
values = [42544017.198965244, 1234565, 40512335.181958228, -1]

def create_data(constructor):
return dict(zip((constructor(x) for x in dates_as_str), values))

data_datetime64 = create_data(np.datetime64)
data_datetime = create_data(lambda x: datetime.strptime(x, '%Y-%m-%d'))
data_Timestamp = create_data(pd.Timestamp)

expected = SparseSeries(values, (pd.Timestamp(x) for x in dates_as_str))
result = SparseSeries(dict(zip(map(datetime_type, dates), values)))
expected = SparseSeries(values, map(pd.Timestamp, dates))

result_datetime64 = SparseSeries(data_datetime64)
result_datetime = SparseSeries(data_datetime)
result_Timestamp = SparseSeries(data_Timestamp)

tm.assert_sp_series_equal(result_datetime64, expected)
tm.assert_sp_series_equal(result_datetime, expected)
tm.assert_sp_series_equal(result_Timestamp, expected)
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))
s = SparseSeries(data)
tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values())))

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

data = A(('col%s' % i, np.random.random()) for i in range(12))
s = SparseSeries(data)
tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values())))
series = SparseSeries(A(data))
tm.assert_sp_series_equal(series, expected)