-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: SparseSeries init from dict fixes #16906
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b5305b
BUG: SparseSeries init from dict fixes
kernc 14f7047
fixup! BUG: SparseSeries init from dict fixes
kernc 991b99a
Move several tests to SharedWithSparse
kernc 7af0dae
update whatsnew
kernc 195550c
assert_series_equal dispatch to sp_series_equal if both are sparse
kernc e7405bf
Fix a failing test ...
kernc bff326a
fixup! Fix a failing test ...
kernc 659559c
self.Series -> self.series_klass
kernc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# pylint: disable-msg=E1101,W0612 | ||
|
||
import operator | ||
from collections import OrderedDict | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from numpy import nan | ||
|
@@ -1361,3 +1364,89 @@ def test_numpy_func_call(self): | |
for func in funcs: | ||
for series in ('bseries', 'zbseries'): | ||
getattr(np, func)(getattr(self, series)) | ||
|
||
|
||
def test_constructor_dict(): | ||
d = {'a': 0., 'b': 1., 'c': 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. you might be able to move some of these into |
||
result = SparseSeries(d) | ||
expected = SparseSeries(d, index=sorted(d.keys())) | ||
tm.assert_sp_series_equal(result, expected) | ||
|
||
result = SparseSeries(d, index=['b', 'c', 'd', 'a']) | ||
expected = SparseSeries([1, 2, nan, 0], index=['b', 'c', 'd', 'a']) | ||
tm.assert_sp_series_equal(result, expected) | ||
|
||
|
||
def test_constructor_dict_multiindex(): | ||
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.} | ||
_d = sorted(d.items()) | ||
result = SparseSeries(d) | ||
expected = SparseSeries( | ||
[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 = SparseSeries(d) | ||
expected = SparseSeries([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(): | ||
# 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 = SparseSeries( | ||
data=['A', 'B', 'C'], | ||
index=pd.to_timedelta([0, 10, 20], unit='s') | ||
) | ||
|
||
result = SparseSeries( | ||
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_sp_series_equal(result, expected) | ||
|
||
|
||
def test_constructor_subclass_dict(): | ||
data = tm.TestSubDict((x, 10.0 * x) for x in range(10)) | ||
series = SparseSeries(data) | ||
expected = SparseSeries(dict(compat.iteritems(data))) | ||
tm.assert_sp_series_equal(series, expected) | ||
|
||
|
||
@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) | ||
|
||
|
||
def test_orderedDict_ctor(): | ||
# GH3283 | ||
data = OrderedDict(('col%s' % i, np.random.random()) for i in range(12)) | ||
|
||
series = SparseSeries(data) | ||
expected = SparseSeries(list(data.values()), list(data.keys())) | ||
tm.assert_sp_series_equal(series, expected) | ||
|
||
# Test with subclass | ||
class A(OrderedDict): | ||
pass | ||
|
||
series = SparseSeries(A(data)) | ||
tm.assert_sp_series_equal(series, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If it doesn't matter whether
index
is passed in, why mention it in the description?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.
It's a reference to the two issues fixed. With, the result was invalid; without, it crashed.
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.
Fair enough, though ultimately instantiating from
dict
just didn't work at all though, which could be considered a single bug (also you're only referencing one issue here). Note that without further context, people won't be aware of that difference (whether it was incorrect or crashed), so it is preferable to be concise.