-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
TST: Move some Series ctor tests to SharedWithSparse #17050
Changes from 4 commits
9270224
d2abc70
4f8b133
0a7a362
b58cc06
61d10fb
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
# coding=utf-8 | ||
# pylint: disable-msg=E1101,W0612 | ||
from collections import OrderedDict | ||
|
||
import pytest | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from pandas import Index, Series, DataFrame, date_range | ||
from pandas import Index, Series, SparseSeries, DataFrame, date_range | ||
from pandas.core.indexes.datetimes import Timestamp | ||
|
||
from pandas.compat import range | ||
|
@@ -21,6 +22,10 @@ | |
|
||
class SharedWithSparse(object): | ||
|
||
def _assert_series_equal(self, left, right): | ||
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. why don't we instead define this in the Mixin classes? (and add a NotImplemented one here)? |
||
"""Dispatch to series class dependent assertion""" | ||
raise NotImplementedError | ||
|
||
def test_scalarop_preserve_name(self): | ||
result = self.ts * 2 | ||
assert result.name == self.ts.name | ||
|
@@ -117,9 +122,80 @@ def test_to_sparse_pass_name(self): | |
result = self.ts.to_sparse() | ||
assert result.name == self.ts.name | ||
|
||
def test_constructor_dict(self): | ||
d = {'a': 0., 'b': 1., 'c': 2.} | ||
result = self.series_klass(d) | ||
expected = self.series_klass(d, index=sorted(d.keys())) | ||
self._assert_series_equal(result, expected) | ||
|
||
result = self.series_klass(d, index=['b', 'c', 'd', 'a']) | ||
expected = self.series_klass([1, 2, np.nan, 0], | ||
index=['b', 'c', 'd', 'a']) | ||
self._assert_series_equal(result, expected) | ||
|
||
def test_constructor_subclass_dict(self): | ||
data = tm.TestSubDict((x, 10.0 * x) for x in range(10)) | ||
series = self.series_klass(data) | ||
expected = self.series_klass(dict(compat.iteritems(data))) | ||
self._assert_series_equal(series, expected) | ||
|
||
def test_constructor_ordereddict(self): | ||
# GH3283 | ||
data = OrderedDict( | ||
('col%s' % i, np.random.random()) for i in range(12)) | ||
|
||
series = self.series_klass(data) | ||
expected = self.series_klass(list(data.values()), list(data.keys())) | ||
self._assert_series_equal(series, expected) | ||
|
||
# Test with subclass | ||
class A(OrderedDict): | ||
pass | ||
|
||
series = self.series_klass(A(data)) | ||
self._assert_series_equal(series, expected) | ||
|
||
def test_constructor_dict_multiindex(self): | ||
d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.} | ||
_d = sorted(d.items()) | ||
result = self.series_klass(d) | ||
expected = self.series_klass( | ||
[x[1] for x in _d], | ||
index=pd.MultiIndex.from_tuples([x[0] for x in _d])) | ||
self._assert_series_equal(result, expected) | ||
|
||
d['z'] = 111. | ||
_d.insert(0, ('z', d['z'])) | ||
result = self.series_klass(d) | ||
expected = self.series_klass([x[1] for x in _d], | ||
index=pd.Index([x[0] for x in _d], | ||
tupleize_cols=False)) | ||
result = result.reindex(index=expected.index) | ||
self._assert_series_equal(result, expected) | ||
|
||
def test_constructor_dict_timedelta_index(self): | ||
# 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 = self.series_klass( | ||
data=['A', 'B', 'C'], | ||
index=pd.to_timedelta([0, 10, 20], unit='s') | ||
) | ||
|
||
result = self.series_klass( | ||
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') | ||
) | ||
self._assert_series_equal(result, expected) | ||
|
||
|
||
class TestSeriesMisc(TestData, SharedWithSparse): | ||
|
||
series_klass = Series | ||
_assert_series_equal = staticmethod(tm.assert_series_equal) | ||
|
||
def test_tab_completion(self): | ||
# GH 9910 | ||
s = Series(list('abcd')) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# pylint: disable-msg=E1101,W0612 | ||
|
||
import operator | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from numpy import nan | ||
|
@@ -58,6 +60,9 @@ def _test_data2_zero(): | |
|
||
class TestSparseSeries(SharedWithSparse): | ||
|
||
series_klass = SparseSeries | ||
_assert_series_equal = staticmethod(tm.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. This doesn't look as good, but avoids 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 like that actually. The only thing is we are using any ideas on a better / more consistent way would be good. (maybe add a comment to this effect in the sub-classes about 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. def setup_method():
pd.util.testing.assert_series_equal = whatever J/k. I have nothing better. |
||
|
||
def setup_method(self, method): | ||
arr, index = _test_data1() | ||
|
||
|
@@ -1379,3 +1384,18 @@ def test_numpy_func_call(self): | |
for func in funcs: | ||
for series in ('bseries', 'zbseries'): | ||
getattr(np, func)(getattr(self, series)) | ||
|
||
|
||
@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) |
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.
oh typo from before?
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.
Yea, #16960