-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Fixes SparseSeries initiated with dictionary raising AttributeError #16960
Changes from 3 commits
b05ba81
6877159
19da337
9869a05
2d3e318
4ce5703
5a4605a
5416762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I think you might be able to remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see what if anything breaks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran the full test suite with this change and everything is working. I also manually ran a bunch of SparseSeries with different inputs (Series, dicts, with/without indexes) and it does work as intended. |
||
index = data.index.view() | ||
|
||
data = Series(data) | ||
res = make_sparse(data, kind=kind, fill_value=fill_value) | ||
data, sparse_index, fill_value = res | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,18 @@ def setup_method(self, method): | |
self.ziseries2 = SparseSeries(arr, index=index, kind='integer', | ||
fill_value=0) | ||
|
||
def test_constructor_data_input(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
# see gh-16905 | ||
test_dict = {1: 1} | ||
test_index = [0, 1, 2] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't name things with test_ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would _test_dict, etc work? I saw that higher up in the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @jreback is saying to come up with different names that don't use "test" at all. |
||
test_series = pd.Series({1: 1}, index=test_index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the |
||
|
||
arr = SparseSeries(test_dict) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. result = |
||
assert arr.count() == len(test_dict) | ||
|
||
arr = SparseSeries(test_series, index=test_index)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops: got an extra right parenthesis there |
||
assert arr.count() == test_series.count() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls use assert_sp_series_equal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to use assert_series_equal since assert_sp_series_equal requires two sparse series, even if check_series_type=False. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what you should do is construct your expected result via instantiation as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The second test is testing the creation of a SparseSeries from a Series. The SparseSeries constructor takes either a dict or a Series. Aside from class, the SparseSeries should be equal to the original Series. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
arr = SparseSeries([np.nan, 1, 2, np.nan]) | ||
assert arr.dtype == np.float64 | ||
|
@@ -109,6 +121,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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second look, I don't think you need this. You can remove. |
||
def test_iteration_and_str(self): | ||
[x for x in self.bseries] | ||
str(self.bseries) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add double ticks ("``") around both
SparseSeries
and `AttributeError`?