Skip to content

Commit

Permalink
Fixes SparseSeries initiated with dictionary raising AttributeError (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
metllord authored and jreback committed Jul 19, 2017
1 parent 1dc93b5 commit 47e909d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
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 @@ -219,7 +219,7 @@ Groupby/Resample/Rolling

Sparse
^^^^^^

- Bug in ``SparseSeries`` raises ``AttributeError`` when a dictionary is passed in as data (:issue:`16777`)


Reshaping
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ 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, index=index)
index = data.index.view()

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

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ def setup_method(self, method):
self.ziseries2 = SparseSeries(arr, index=index, kind='integer',
fill_value=0)

def test_constructor_dict_input(self):
# gh-16905
constructor_dict = {1: 1.}
index = [0, 1, 2]

# Series with index passed in
series = pd.Series(constructor_dict)
expected = SparseSeries(series, index=index)

result = SparseSeries(constructor_dict, index=index)
tm.assert_sp_series_equal(result, expected)

# Series with index and dictionary with no index
expected = SparseSeries(series)

result = SparseSeries(constructor_dict)
tm.assert_sp_series_equal(result, expected)

def test_constructor_dtype(self):
arr = SparseSeries([np.nan, 1, 2, np.nan])
assert arr.dtype == np.float64
Expand Down

0 comments on commit 47e909d

Please sign in to comment.