8000 Fixes SparseSeries initiated with dictionary raising AttributeError by metllord · Pull Request #16960 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Fixes SparseSeries initiated with dictionary raising AttributeError #16960

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

Merged
merged 8 commits into from
Jul 19, 2017
Merged
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
Removes unecessary code from series.py; cleans up test_series
  • Loading branch information
Eric Stein committed Jul 17, 2017
commit 54167627f7c7a283aadc424298f6a71ac900231a
3 changes: 1 addition & 2 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why this being tested? The result variable is the same as in line 98.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was testing input with a series, but that is test turned out to be redundant. I removed it from the most recent commit.

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)

Copy link
Member

Choose a reason for hiding this comment

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

Okay : in light of what you just said, I think you can condense this test a bit and rename as follows:

def test_constructor_dict_input(self):
...
series = pd.Series(constructor_dict, index=index)
expected = SparseSeries(series, index=index)

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

And delete everything else below it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this. I added this test and did find a case that was not handled correctly. I fixed the code and added tests for all options of SparseSeries creation.

def test_constructor_dtype(self):
Expand Down
0