From 8b5305b488ec32556248bc979aa3683d852b6d52 Mon Sep 17 00:00:00 2001 From: Kernc Date: Thu, 13 Jul 2017 14:57:43 +0200 Subject: [PATCH 1/8] BUG: SparseSeries init from dict fixes --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/sparse/series.py | 6 +- pandas/tests/sparse/test_series.py | 96 ++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 015fdf1f45f47..d4973076fcaed 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -179,6 +179,7 @@ Sparse ^^^^^^ +- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index`` (:issue:`16905`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index 9dd061e26ba06..ebe2547d5ec60 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -146,10 +146,8 @@ def __init__(self, data=None, index=None, sparse_index=None, kind='block', data = data._data elif isinstance(data, (Series, dict)): - if index is None: - index = data.index.view() - - data = Series(data) + data = Series(data, index=index) + index = data.index 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..92ecce42cfc34 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -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,96 @@ 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.} + 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()) + ser = 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(ser, expected) + + d['z'] = 111. + _d.insert(0, ('z', d['z'])) + ser = SparseSeries(d) + expected = SparseSeries([x[1] for x in _d], + index=pd.Index([x[0] for x in _d], + tupleize_cols=False)) + ser = ser.reindex(index=expected.index) + tm.assert_series_equal(ser, 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) + refseries = SparseSeries(dict(compat.iteritems(data))) + tm.assert_sp_series_equal(refseries, series) + + +def test_constructor_dict_datetime64_index(): + # GH 9456 + dates_as_str = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15'] + values = [42544017.198965244, 1234565, 40512335.181958228, -1] + + def create_data(constructor): + return dict(zip((constructor(x) for x in dates_as_str), values)) + + data_datetime64 = create_data(np.datetime64) + data_datetime = create_data(lambda x: datetime.strptime(x, '%Y-%m-%d')) + data_Timestamp = create_data(pd.Timestamp) + + expected = SparseSeries(values, (pd.Timestamp(x) for x in dates_as_str)) + + result_datetime64 = SparseSeries(data_datetime64) + result_datetime = SparseSeries(data_datetime) + result_Timestamp = SparseSeries(data_Timestamp) + + tm.assert_sp_series_equal(result_datetime64, expected) + tm.assert_sp_series_equal(result_datetime, expected) + tm.assert_sp_series_equal(result_Timestamp, expected) + + +def test_orderedDict_ctor(): + # GH3283 + data = OrderedDict(('col%s' % i, np.random.random()) for i in range(12)) + s = SparseSeries(data) + tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values()))) + + # Test with subclass + class A(OrderedDict): + pass + + data = A(('col%s' % i, np.random.random()) for i in range(12)) + s = SparseSeries(data) + tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values()))) From 14f70478d966b2e289a50f9c7d04ca7a53a5d372 Mon Sep 17 00:00:00 2001 From: Kernc Date: Thu, 13 Jul 2017 19:36:50 +0200 Subject: [PATCH 2/8] fixup! BUG: SparseSeries init from dict fixes --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/tests/sparse/test_series.py | 51 +++++++++++++----------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index d4973076fcaed..f7e6bdd4b534c 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -179,7 +179,7 @@ Sparse ^^^^^^ -- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index`` (:issue:`16905`) +- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index=`` kwarg (:issue:`16905`) Reshaping ^^^^^^^^^ diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 92ecce42cfc34..6ef3b8e6a4ed7 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -1380,20 +1380,20 @@ def test_constructor_dict(): def test_constructor_dict_multiindex(): d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.} _d = sorted(d.items()) - ser = SparseSeries(d) + 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(ser, expected) + tm.assert_series_equal(result, expected) d['z'] = 111. _d.insert(0, ('z', d['z'])) - ser = SparseSeries(d) + result = SparseSeries(d) expected = SparseSeries([x[1] for x in _d], index=pd.Index([x[0] for x in _d], tupleize_cols=False)) - ser = ser.reindex(index=expected.index) - tm.assert_series_equal(ser, expected) + result = result.reindex(index=expected.index) + tm.assert_series_equal(result, expected) def test_constructor_dict_timedelta_index(): @@ -1417,43 +1417,36 @@ def test_constructor_dict_timedelta_index(): def test_constructor_subclass_dict(): data = tm.TestSubDict((x, 10.0 * x) for x in range(10)) series = SparseSeries(data) - refseries = SparseSeries(dict(compat.iteritems(data))) - tm.assert_sp_series_equal(refseries, series) + expected = SparseSeries(dict(compat.iteritems(data))) + tm.assert_sp_series_equal(series, expected) -def test_constructor_dict_datetime64_index(): +@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_as_str = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15'] + dates = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15'] values = [42544017.198965244, 1234565, 40512335.181958228, -1] - def create_data(constructor): - return dict(zip((constructor(x) for x in dates_as_str), values)) - - data_datetime64 = create_data(np.datetime64) - data_datetime = create_data(lambda x: datetime.strptime(x, '%Y-%m-%d')) - data_Timestamp = create_data(pd.Timestamp) - - expected = SparseSeries(values, (pd.Timestamp(x) for x in dates_as_str)) + result = SparseSeries(dict(zip(map(datetime_type, dates), values))) + expected = SparseSeries(values, map(pd.Timestamp, dates)) - result_datetime64 = SparseSeries(data_datetime64) - result_datetime = SparseSeries(data_datetime) - result_Timestamp = SparseSeries(data_Timestamp) - - tm.assert_sp_series_equal(result_datetime64, expected) - tm.assert_sp_series_equal(result_datetime, expected) - tm.assert_sp_series_equal(result_Timestamp, expected) + 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)) - s = SparseSeries(data) - tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values()))) + + 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 - data = A(('col%s' % i, np.random.random()) for i in range(12)) - s = SparseSeries(data) - tm.assert_numpy_array_equal(s.values.values, np.array(list(data.values()))) + series = SparseSeries(A(data)) + tm.assert_sp_series_equal(series, expected) From 991b99a24421ecf311dcc624767dc0fae973e76b Mon Sep 17 00:00:00 2001 From: Kernc Date: Sun, 16 Jul 2017 18:52:27 +0200 Subject: [PATCH 3/8] Move several tests to SharedWithSparse --- pandas/tests/series/test_api.py | 70 ++++++++++++++++++++++ pandas/tests/series/test_constructors.py | 62 -------------------- pandas/tests/sparse/test_series.py | 74 +----------------------- 3 files changed, 72 insertions(+), 134 deletions(-) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 1eb2b98a7d7cc..6bcc35eb66347 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -1,5 +1,6 @@ # coding=utf-8 # pylint: disable-msg=E1101,W0612 +from collections import OrderedDict import pytest @@ -117,9 +118,78 @@ 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(d) + expected = self.Series(d, index=sorted(d.keys())) + tm.assert_series_equal(result, expected) + + result = self.Series(d, index=['b', 'c', 'd', 'a']) + expected = self.Series([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a']) + tm.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(data) + expected = self.Series(dict(compat.iteritems(data))) + tm.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(data) + expected = self.Series(list(data.values()), list(data.keys())) + tm.assert_series_equal(series, expected) + + # Test with subclass + class A(OrderedDict): + pass + + series = self.Series(A(data)) + tm.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(d) + expected = self.Series( + [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 = self.Series(d) + expected = self.Series([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(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( + data=['A', 'B', 'C'], + index=pd.to_timedelta([0, 10, 20], unit='s') + ) + + result = self.Series( + 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_series_equal(result, expected) + class TestSeriesMisc(TestData, SharedWithSparse): + Series = Series + def test_tab_completion(self): # GH 9910 s = Series(list('abcd')) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index d591aa4f567a9..cff775de3816c 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -605,48 +605,6 @@ def test_constructor_dict(self): expected.iloc[1] = 1 assert_series_equal(result, expected) - def test_constructor_dict_multiindex(self): - check = lambda result, expected: tm.assert_series_equal( - result, expected, check_dtype=True, check_series_type=True) - d = {('a', 'a'): 0., ('b', 'a'): 1., ('b', 'c'): 2.} - _d = sorted(d.items()) - ser = Series(d) - expected = Series([x[1] for x in _d], - index=MultiIndex.from_tuples([x[0] for x in _d])) - check(ser, expected) - - d['z'] = 111. - _d.insert(0, ('z', d['z'])) - ser = Series(d) - expected = Series([x[1] for x in _d], index=Index( - [x[0] for x in _d], tupleize_cols=False)) - ser = ser.reindex(index=expected.index) - check(ser, 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 = Series( - data=['A', 'B', 'C'], - index=pd.to_timedelta([0, 10, 20], unit='s') - ) - - result = Series( - 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') - ) - # this should work - assert_series_equal(result, expected) - - def test_constructor_subclass_dict(self): - data = tm.TestSubDict((x, 10.0 * x) for x in range(10)) - series = Series(data) - refseries = Series(dict(compat.iteritems(data))) - assert_series_equal(refseries, series) - def test_constructor_dict_datetime64_index(self): # GH 9456 @@ -670,26 +628,6 @@ def create_data(constructor): assert_series_equal(result_datetime, expected) assert_series_equal(result_Timestamp, expected) - def test_orderedDict_ctor(self): - # GH3283 - import pandas - import random - data = OrderedDict([('col%s' % i, random.random()) for i in range(12)]) - s = pandas.Series(data) - assert all(s.values == list(data.values())) - - def test_orderedDict_subclass_ctor(self): - # GH3283 - import pandas - import random - - class A(OrderedDict): - pass - - data = A([('col%s' % i, random.random()) for i in range(12)]) - s = pandas.Series(data) - assert all(s.values == list(data.values())) - def test_constructor_list_of_tuples(self): data = [(1, 1), (2, 2), (2, 3)] s = Series(data) diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 6ef3b8e6a4ed7..d5749acd28746 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -1,7 +1,6 @@ # pylint: disable-msg=E1101,W0612 import operator -from collections import OrderedDict from datetime import datetime import pytest @@ -61,6 +60,8 @@ def _test_data2_zero(): class TestSparseSeries(SharedWithSparse): + Series = SparseSeries + def setup_method(self, method): arr, index = _test_data1() @@ -1366,61 +1367,6 @@ def test_numpy_func_call(self): getattr(np, func)(getattr(self, series)) -def test_constructor_dict(): - d = {'a': 0., 'b': 1., 'c': 2.} - 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, @@ -1434,19 +1380,3 @@ def test_constructor_dict_datetime64_index(datetime_type): 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) From 7af0daef08dbead1466ef2314436ae0e3155587e Mon Sep 17 00:00:00 2001 From: Kernc Date: Sun, 16 Jul 2017 18:55:13 +0200 Subject: [PATCH 4/8] update whatsnew --- 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 f7e6bdd4b534c..30bd989f7a819 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -179,7 +179,7 @@ Sparse ^^^^^^ -- Bug in instantiating :class:`SparseSeries` from ``dict`` with or without ``index=`` kwarg (:issue:`16905`) +- Bug in instantiating :class:`SparseSeries` from ``dict`` (:issue:`16905`) Reshaping ^^^^^^^^^ From 195550c7f5fcc95377b4d0f7351a518dfe2d64be Mon Sep 17 00:00:00 2001 From: Kernc Date: Sun, 16 Jul 2017 19:17:19 +0200 Subject: [PATCH 5/8] assert_series_equal dispatch to sp_series_equal if both are sparse --- pandas/util/testing.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 17e09b38b20e0..d45589e6edd3a 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -46,7 +46,7 @@ from pandas import (bdate_range, CategoricalIndex, Categorical, IntervalIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex, RangeIndex, Index, MultiIndex, - Series, DataFrame, Panel, Panel4D) + Series, DataFrame, SparseSeries, Panel, Panel4D) from pandas._libs import testing as _testing from pandas.io.common import urlopen @@ -1225,6 +1225,12 @@ def assert_series_equal(left, right, check_dtype=True, # instance validation _check_isinstance(left, right, Series) + if isinstance(left, SparseSeries) and isinstance(right, SparseSeries): + return assert_sp_series_equal(left, right, + check_dtype=check_dtype, + check_series_type=check_series_type, + check_names=check_names) + if check_series_type: # ToDo: There are some tests using rhs is sparse # lhs is dense. Should use assert_class_equal in future From e7405bf4225a7ebd1b3be9939fcef234d1a465b4 Mon Sep 17 00:00:00 2001 From: Kernc Date: Mon, 17 Jul 2017 13:52:40 +0200 Subject: [PATCH 6/8] Fix a failing test ... that was failing due to introduced dispatch to assert_sp_series_equal being too strict. --- pandas/tests/sparse/test_frame.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/sparse/test_frame.py b/pandas/tests/sparse/test_frame.py index 654d12b782f37..631e324659f92 100644 --- a/pandas/tests/sparse/test_frame.py +++ b/pandas/tests/sparse/test_frame.py @@ -1002,12 +1002,14 @@ def _check(frame, orig): shifted = frame.shift(2, freq='B') exp = orig.shift(2, freq='B') - exp = exp.to_sparse(frame.default_fill_value) + exp = exp.to_sparse(frame.default_fill_value, + kind=frame.default_kind) tm.assert_frame_equal(shifted, exp) shifted = frame.shift(2, freq=BDay()) exp = orig.shift(2, freq=BDay()) - exp = exp.to_sparse(frame.default_fill_value) + exp = exp.to_sparse(frame.default_fill_value, + kind=frame.default_kind) tm.assert_frame_equal(shifted, exp) self._check_all(_check) From bff326a78c45a430676d7c656119854e2a229755 Mon Sep 17 00:00:00 2001 From: Kernc Date: Mon, 17 Jul 2017 23:23:51 +0200 Subject: [PATCH 7/8] fixup! Fix a failing test ... --- pandas/tests/series/test_constructors.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index cff775de3816c..a916c42c007f9 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -20,8 +20,7 @@ from pandas._libs import lib from pandas._libs.tslib import iNaT -from pandas.compat import lrange, range, zip, OrderedDict, long -from pandas import compat +from pandas.compat import lrange, range, zip, long from pandas.util.testing import assert_series_equal import pandas.util.testing as tm From 659559c8f11bb71ea1b74c678b9ed319df86165e Mon Sep 17 00:00:00 2001 From: Kernc Date: Mon, 17 Jul 2017 23:24:18 +0200 Subject: [PATCH 8/8] self.Series -> self.series_klass --- pandas/tests/series/test_api.py | 32 +++++++++++++++--------------- pandas/tests/sparse/test_series.py | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 6bcc35eb66347..d70468b7fffce 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -120,18 +120,18 @@ def test_to_sparse_pass_name(self): def test_constructor_dict(self): d = {'a': 0., 'b': 1., 'c': 2.} - result = self.Series(d) - expected = self.Series(d, index=sorted(d.keys())) + result = self.series_klass(d) + expected = self.series_klass(d, index=sorted(d.keys())) tm.assert_series_equal(result, expected) - result = self.Series(d, index=['b', 'c', 'd', 'a']) - expected = self.Series([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a']) + result = self.series_klass(d, index=['b', 'c', 'd', 'a']) + expected = self.series_klass([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a']) tm.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(data) - expected = self.Series(dict(compat.iteritems(data))) + series = self.series_klass(data) + expected = self.series_klass(dict(compat.iteritems(data))) tm.assert_series_equal(series, expected) def test_constructor_ordereddict(self): @@ -139,30 +139,30 @@ def test_constructor_ordereddict(self): data = OrderedDict( ('col%s' % i, np.random.random()) for i in range(12)) - series = self.Series(data) - expected = self.Series(list(data.values()), list(data.keys())) + series = self.series_klass(data) + expected = self.series_klass(list(data.values()), list(data.keys())) tm.assert_series_equal(series, expected) # Test with subclass class A(OrderedDict): pass - series = self.Series(A(data)) + series = self.series_klass(A(data)) tm.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(d) - expected = self.Series( + 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])) tm.assert_series_equal(result, expected) d['z'] = 111. _d.insert(0, ('z', d['z'])) - result = self.Series(d) - expected = self.Series([x[1] for x in _d], + 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) @@ -172,12 +172,12 @@ 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( + expected = self.series_klass( data=['A', 'B', 'C'], index=pd.to_timedelta([0, 10, 20], unit='s') ) - result = self.Series( + 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'}, @@ -188,7 +188,7 @@ def test_constructor_dict_timedelta_index(self): class TestSeriesMisc(TestData, SharedWithSparse): - Series = Series + series_klass = Series def test_tab_completion(self): # GH 9910 diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index d5749acd28746..08601648c473c 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -60,7 +60,7 @@ def _test_data2_zero(): class TestSparseSeries(SharedWithSparse): - Series = SparseSeries + series_klass = SparseSeries def setup_method(self, method): arr, index = _test_data1()