Skip to content
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

BUG: SparseSeries init from dict fixes #16906

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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`` (:issue:`16905`)

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
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_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_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)

def test_constructor_subclass_dict(self):
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
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_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_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_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_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)
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_klass(
data=['A', 'B', 'C'],
index=pd.to_timedelta([0, 10, 20], unit='s')
)

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'},
index=pd.to_timedelta([0, 10, 20], unit='s')
)
tm.assert_series_equal(result, expected)


class TestSeriesMisc(TestData, SharedWithSparse):

series_klass = Series

def test_tab_completion(self):
# GH 9910
s = Series(list('abcd'))
Expand Down
65 changes: 1 addition & 64 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from pandas._libs import lib
from pandas._libs.tslib import iNaT

from pandas.compat import lrange, range, zip, OrderedDict, long
from pandas import compat
from pandas.compat import lrange, range, zip, long
from pandas.util.testing import assert_series_equal
import pandas.util.testing as tm

Expand Down Expand Up @@ -605,48 +604,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 +627,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
6 changes: 4 additions & 2 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,12 +1002,14 @@ def _check(frame, orig):

shifted = frame.shift(2, freq='B')
exp = orig.shift(2, freq='B')
exp = exp.to_sparse(frame.default_fill_value)
exp = exp.to_sparse(frame.default_fill_value,
kind=frame.default_kind)
tm.assert_frame_equal(shifted, exp)

shifted = frame.shift(2, freq=BDay())
exp = orig.shift(2, freq=BDay())
exp = exp.to_sparse(frame.default_fill_value)
exp = exp.to_sparse(frame.default_fill_value,
kind=frame.default_kind)
tm.assert_frame_equal(shifted, exp)

self._check_all(_check)
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable-msg=E1101,W0612

import operator
from datetime import datetime

import pytest

from numpy import nan
Expand Down Expand Up @@ -58,6 +60,8 @@ def _test_data2_zero():

class TestSparseSeries(SharedWithSparse):

series_klass = SparseSeries

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

Expand Down Expand Up @@ -1361,3 +1365,18 @@ def test_numpy_func_call(self):
for func in funcs:
for series in ('bseries', 'zbseries'):
getattr(np, func)(getattr(self, series))


@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)
8 changes: 7 additions & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from pandas import (bdate_range, CategoricalIndex, Categorical, IntervalIndex,
DatetimeIndex, TimedeltaIndex, PeriodIndex, RangeIndex,
Index, MultiIndex,
Series, DataFrame, Panel, Panel4D)
Series, DataFrame, SparseSeries, Panel, Panel4D)

from pandas._libs import testing as _testing
from pandas.io.common import urlopen
Expand Down Expand Up @@ -1225,6 +1225,12 @@ def assert_series_equal(left, right, check_dtype=True,
# instance validation
_check_isinstance(left, right, Series)

if isinstance(left, SparseSeries) and isinstance(right, SparseSeries):
return assert_sp_series_equal(left, right,
check_dtype=check_dtype,
check_series_type=check_series_type,
check_names=check_names)

if check_series_type:
# ToDo: There are some tests using rhs is sparse
# lhs is dense. Should use assert_class_equal in future
Expand Down