From b05ba81c8172fe5b132d75fe235fd240d5b21422 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sat, 15 Jul 2017 15:49:05 -0500 Subject: [PATCH 1/8] Fixes SparseSeries initiated with dictionary raising AttributeError (Issue ) --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/core/sparse/series.py | 2 +- pandas/tests/sparse/test_series.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 6ddf6029b99bb..e9c68f00c2e4d 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -180,7 +180,7 @@ Groupby/Resample/Rolling Sparse ^^^^^^ - +- Bug in SparseSeries raises AttributeError when a dicitonary is passed as data (:issue:`16777`) Reshaping diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index 9dd061e26ba06..be54bb57fd927 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -146,10 +146,10 @@ def __init__(self, data=None, index=None, sparse_index=None, kind='block', data = data._data elif isinstance(data, (Series, dict)): + data = Series(data) if index is None: index = data.index.view() - data = Series(data) res = make_sparse(data, kind=kind, fill_value=fill_value) data, sparse_index, fill_value = res diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index b524d6bfab418..0daa440134eac 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -87,6 +87,12 @@ def setup_method(self, method): fill_value=0) self.ziseries2 = SparseSeries(arr, index=index, kind='integer', fill_value=0) + def test_constructor_data_input(self): + arr = SparseSeries({1: 1}) + assert arr.count() == len({1: 1}) + + arr = SparseSeries(pd.Series({1: 1}, index=[0, 1, 2])) + assert arr.count() == pd.Series({1: 1}, index=[0, 1, 2]).count() def test_constructor_dtype(self): arr = SparseSeries([np.nan, 1, 2, np.nan]) @@ -109,6 +115,9 @@ def test_constructor_dtype(self): assert arr.dtype == np.int64 assert arr.fill_value == 0 + arr = SparseSeries({1: 1}) + assert arr.dtype == np.int64 + def test_iteration_and_str(self): [x for x in self.bseries] str(self.bseries) From 6877159cce4376b28b0ddd3105211d0a7bf940d5 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sat, 15 Jul 2017 21:19:35 -0500 Subject: [PATCH 2/8] Fixes spelling errors in whatsnew doc --- doc/source/whatsnew/v0.21.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index e9c68f00c2e4d..e6a14d4bb1c85 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -180,7 +180,7 @@ Groupby/Resample/Rolling Sparse ^^^^^^ -- Bug in SparseSeries raises AttributeError when a dicitonary is passed as data (:issue:`16777`) +- Bug in SparseSeries raises AttributeError when a dictionary is passed in as data (:issue:`16777`) Reshaping From 19da33784c701b5533eb63fa16258362b44fae14 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sat, 15 Jul 2017 21:20:07 -0500 Subject: [PATCH 3/8] Refactors test for SparseSeries construction --- pandas/tests/sparse/test_series.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 0daa440134eac..ea1566e76eee1 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -87,12 +87,18 @@ def setup_method(self, method): fill_value=0) self.ziseries2 = SparseSeries(arr, index=index, kind='integer', fill_value=0) + def test_constructor_data_input(self): - arr = SparseSeries({1: 1}) - assert arr.count() == len({1: 1}) + # see gh-16905 + test_dict = {1: 1} + test_index = [0, 1, 2] + test_series = pd.Series({1: 1}, index=test_index) + + arr = SparseSeries(test_dict) + assert arr.count() == len(test_dict) - arr = SparseSeries(pd.Series({1: 1}, index=[0, 1, 2])) - assert arr.count() == pd.Series({1: 1}, index=[0, 1, 2]).count() + arr = SparseSeries(test_series, index=test_index)) + assert arr.count() == test_series.count() def test_constructor_dtype(self): arr = SparseSeries([np.nan, 1, 2, np.nan]) From 9869a0523ed8518bdcf06847bfe3b2639f57fb07 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sun, 16 Jul 2017 03:36:10 -0500 Subject: [PATCH 4/8] fixes syntax error in test_series.py --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/tests/sparse/test_series.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index e6a14d4bb1c85..f8bd643f05ca0 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -180,7 +180,7 @@ Groupby/Resample/Rolling Sparse ^^^^^^ -- Bug in SparseSeries raises AttributeError when a dictionary is passed in as data (:issue:`16777`) +- Bug in ``SparseSeries`` raises ``AttributeError`` when a dictionary is passed in as data (:issue:`16777`) Reshaping diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index ea1566e76eee1..2773d00c1e6df 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -92,12 +92,12 @@ def test_constructor_data_input(self): # see gh-16905 test_dict = {1: 1} test_index = [0, 1, 2] - test_series = pd.Series({1: 1}, index=test_index) + test_series = pd.Series(test_dict, index=test_index) arr = SparseSeries(test_dict) assert arr.count() == len(test_dict) - arr = SparseSeries(test_series, index=test_index)) + arr = SparseSeries(test_series, index=test_index) assert arr.count() == test_series.count() def test_constructor_dtype(self): From 2d3e3188e0221004f154bc0387b717542fbe2491 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sun, 16 Jul 2017 05:08:53 -0500 Subject: [PATCH 5/8] Refactors test_series.py based on code review --- pandas/tests/sparse/test_series.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 2773d00c1e6df..57ea5dfa81ae6 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -90,15 +90,15 @@ def setup_method(self, method): def test_constructor_data_input(self): # see gh-16905 - test_dict = {1: 1} - test_index = [0, 1, 2] - test_series = pd.Series(test_dict, index=test_index) + constructor_dict = {1: 1} + index = [0, 1, 2] + series = pd.Series(constructor_dict, index=index) - arr = SparseSeries(test_dict) - assert arr.count() == len(test_dict) + result = SparseSeries(constructor_dict) + tm.assert_dict_equal(result.to_dict(), constructor_dict) - arr = SparseSeries(test_series, index=test_index) - assert arr.count() == test_series.count() + result = SparseSeries(series, index=index) + tm.assert_series_equal(result, series, check_series_type=False) def test_constructor_dtype(self): arr = SparseSeries([np.nan, 1, 2, np.nan]) From 4ce5703ccff6564c753a707b44817944ecb1e071 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sun, 16 Jul 2017 13:51:48 -0500 Subject: [PATCH 6/8] Fixes bug with index in SparseSeries; tests all construction options --- pandas/core/sparse/series.py | 2 +- pandas/tests/sparse/test_series.py | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index be54bb57fd927..23cd900b3b698 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -146,7 +146,7 @@ def __init__(self, data=None, index=None, sparse_index=None, kind='block', data = data._data elif isinstance(data, (Series, dict)): - data = Series(data) + data = Series(data, index=index) if index is None: index = data.index.view() diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 57ea5dfa81ae6..2290082cc1c09 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -89,16 +89,27 @@ def setup_method(self, method): fill_value=0) def test_constructor_data_input(self): - # see gh-16905 - constructor_dict = {1: 1} + constructor_dict = {1: 1.} index = [0, 1, 2] - series = pd.Series(constructor_dict, index=index) + series = pd.Series(constructor_dict) + + expected = SparseSeries(series, index=index) + + result = SparseSeries(constructor_dict, index=index) + tm.assert_sp_series_equal(result, expected) + + # Series and dictionary with no index: see gh-16905 + expected = SparseSeries(series) result = SparseSeries(constructor_dict) - tm.assert_dict_equal(result.to_dict(), constructor_dict) + tm.assert_sp_series_equal(result, expected) + + # Series with index and dictionary with no index + series = pd.Series(constructor_dict, index=index) + expected = SparseSeries(series) - result = SparseSeries(series, index=index) - tm.assert_series_equal(result, series, check_series_type=False) + result = SparseSeries(constructor_dict, index=index) + tm.assert_sp_series_equal(result, expected) def test_constructor_dtype(self): arr = SparseSeries([np.nan, 1, 2, np.nan]) From 5a4605a5cf03330607c165cf8db401792e1961a8 Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Sun, 16 Jul 2017 13:55:17 -0500 Subject: [PATCH 7/8] removed unnecessary test for sparceseries --- pandas/tests/sparse/test_series.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 2290082cc1c09..34ad160957ec7 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -132,9 +132,6 @@ def test_constructor_dtype(self): assert arr.dtype == np.int64 assert arr.fill_value == 0 - arr = SparseSeries({1: 1}) - assert arr.dtype == np.int64 - def test_iteration_and_str(self): [x for x in self.bseries] str(self.bseries) From 54167627f7c7a283aadc424298f6a71ac900231a Mon Sep 17 00:00:00 2001 From: Eric Stein Date: Mon, 17 Jul 2017 09:14:13 -0500 Subject: [PATCH 8/8] Removes unecessary code from series.py; cleans up test_series --- pandas/core/sparse/series.py | 3 +-- pandas/tests/sparse/test_series.py | 15 +++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index 23cd900b3b698..1bc9cf5379930 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -147,8 +147,7 @@ def __init__(self, data=None, index=None, sparse_index=None, kind='block', elif isinstance(data, (Series, dict)): data = Series(data, index=index) - if index is None: - index = data.index.view() + index = data.index.view() res = make_sparse(data, kind=kind, fill_value=fill_value) data, sparse_index, fill_value = res diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 34ad160957ec7..bb56f8a51897a 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -88,27 +88,22 @@ def setup_method(self, method): self.ziseries2 = SparseSeries(arr, index=index, kind='integer', fill_value=0) - def test_constructor_data_input(self): + def test_constructor_dict_input(self): + # gh-16905 constructor_dict = {1: 1.} index = [0, 1, 2] - series = pd.Series(constructor_dict) + # 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 and dictionary with no index: see gh-16905 - expected = SparseSeries(series) - - result = SparseSeries(constructor_dict) - tm.assert_sp_series_equal(result, expected) - # Series with index and dictionary with no index - series = pd.Series(constructor_dict, index=index) expected = SparseSeries(series) - result = SparseSeries(constructor_dict, index=index) + result = SparseSeries(constructor_dict) tm.assert_sp_series_equal(result, expected) def test_constructor_dtype(self):