def test_series_min_max_b(self): # string objects work as expected; when fixed length strings, however, the do not s1 = Series(list('abc'), dtype=object) self.assertEqual(s1.min(), 'a') self.assertEqual(s1.max(), 'c') # get the same result from character arrays s2 = sf.Series(list('abc')) self.assertEqual(s2.min(), 'a') self.assertEqual(s2.max(), 'c')
def test_series_binary_operator_b(self): '''Test binary operators with Series of same index ''' s1 = Series(range(4), index=('a', 'b', 'c', 'd')) s2 = Series((x * 2 for x in range(4)), index=('a', 'b', 'c', 'd')) self.assertEqual(list((s1 + s2).items()), [('a', 0), ('b', 3), ('c', 6), ('d', 9)]) self.assertEqual(list((s1 * s2).items()), [('a', 0), ('b', 2), ('c', 8), ('d', 18)])
def test_index_intersection_c(self) -> None: idx1 = Index((10, 20)) with self.assertRaises(RuntimeError): # raises as it identifies labelled data _ = idx1.intersection(Series([20, 30])) idx2 = idx1.intersection(Series([20, 30]).values) self.assertEqual(idx2.values.tolist(), [20]) idx3 = idx1.intersection([20, 30]) self.assertEqual(idx3.values.tolist(), [20])
def test_series_to_frame_b(self): s1 = Series((2, 3), index=list('ab'), name='alt') f1 = s1.to_frame_go() self.assertTrue(f1.__class__ is FrameGO) self.assertEqual(f1.columns.values.tolist(), ['alt']) self.assertEqual(f1.to_pairs(0), (('alt', (('a', 2), ('b', 3))), )) self.assertTrue(s1.mloc == f1.mloc.tolist()[0])
def test_series_astype_a(self): s1 = Series(['a', 'b', 'c']) s2 = s1.astype(object) self.assertEqual(s2.to_pairs(), ((0, 'a'), (1, 'b'), (2, 'c'))) self.assertTrue(s2.dtype == object) # we cannot convert to float with self.assertRaises(ValueError): s1.astype(float)
def test_series_drop_duplicated_a(self): s1 = Series([5, 3, 3, 3, 7, 2, 2, 2, 1], index=('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), dtype=int) self.assertEqual(s1.drop_duplicated().to_pairs(), (('a', 5), ('e', 7), ('i', 1))) self.assertEqual( s1.drop_duplicated(exclude_first=True).to_pairs(), (('a', 5), ('b', 3), ('e', 7), ('f', 2), ('i', 1)))
def test_series_to_html_datatables_a(self): s1 = Series((2, 3, 0, -1, 8, 6), index=list('abcdef')) sio = StringIO() post = s1.to_html_datatables(sio, show=False) self.assertEqual(post, None) self.assertTrue(len(sio.read()) > 1500)
def test_series_pickle_a(self): s1 = Series(range(6), index=list('abcdef')) s2 = Series((2, 3, 0, -1, 8, 6), index=list('abcdef')) s3 = s2.astype(bool) for series in (s1, s2, s3): pbytes = pickle.dumps(series) series_new = pickle.loads(pbytes) for v in series: # iter labels # this compares series objects self.assertFalse(series_new.values.flags.writeable) self.assertEqual(series_new.loc[v], series.loc[v])
def test_series_binary_operator_c(self): '''Test binary operators with Series of different index ''' s1 = Series(range(4), index=('a', 'b', 'c', 'd')) s2 = Series((x * 2 for x in range(4)), index=('c', 'd', 'e', 'f')) self.assertAlmostEqualItems(list((s1 * s2).items()), [('a', nan), ('b', nan), ('c', 0), ('d', 6), ('e', nan), ('f', nan)])
def test_series_min_max_a(self): s1 = Series([1, 3, 4, 0]) self.assertEqual(s1.min(), 0) self.assertEqual(s1.max(), 4) s2 = sf.Series([-1, 4, None, np.nan]) self.assertEqual(s2.min(), -1) self.assertTrue(np.isnan(s2.min(skipna=False))) self.assertEqual(s2.max(), 4) self.assertTrue(np.isnan(s2.max(skipna=False)))
def test_series_group_b(self): s1 = Series(('foo', 'bar', 'foo', 20, 20), index=('a', 'b', 'c', 'd', 'e'), dtype=object) groups = tuple(s1.iter_group_items()) self.assertEqual([g[0] for g in groups], [20, 'bar', 'foo']) self.assertEqual([g[1].to_pairs() for g in groups], [(('d', 20), ('e', 20)), (('b', 'bar'), ), (('a', 'foo'), ('c', 'foo'))])
def test_series_init_c(self): # these test get different results in Pyhthon 3.6 # s1 = Series(dict(a=1, b=4), dtype=int) # self.assertEqual(s1.to_pairs(), # (('a', 1), ('b', 4))) # s1 = Series(dict(b=4, a=1), dtype=int) # self.assertEqual(s1.to_pairs(), # (('a', 1), ('b', 4))) s1 = Series(OrderedDict([('b', 4), ('a', 1)]), dtype=int) self.assertEqual(s1.to_pairs(), (('b', 4), ('a', 1)))
def test_series_reindex_add_level(self): s1 = Series(['a', 'b', 'c']) s2 = s1.reindex_add_level('I') self.assertEqual(s2.index.depth, 2) self.assertEqual(s2.to_pairs(), ((('I', 0), 'a'), (('I', 1), 'b'), (('I', 2), 'c'))) s3 = s2.reindex_flat() self.assertEqual(s3.index.depth, 1) self.assertEqual(s3.to_pairs(), ((('I', 0), 'a'), (('I', 1), 'b'), (('I', 2), 'c')))
def test_series_sort_index_a(self): s1 = Series((10, 3, 28, 21, 15), index=('a', 'c', 'b', 'e', 'd'), dtype=object) self.assertEqual(s1.sort_index().to_pairs(), (('a', 10), ('b', 28), ('c', 3), ('d', 15), ('e', 21))) self.assertEqual(s1.sort_values().to_pairs(), (('c', 3), ('a', 10), ('d', 15), ('e', 21), ('b', 28)))
def test_series_roll_a(self): s1 = Series((2, 3, 0, -1, 8, 6), index=list('abcdef')) self.assertEqual( s1.roll(2).to_pairs(), (('a', 8), ('b', 6), ('c', 2), ('d', 3), ('e', 0), ('f', -1))) self.assertEqual( s1.roll(-2).to_pairs(), (('a', 0), ('b', -1), ('c', 8), ('d', 6), ('e', 2), ('f', 3))) # if the roll is a noop, we reuse the same array self.assertEqual(s1.mloc, s1.roll(len(s1)).mloc)
def test_series_from_items_a(self): def gen(): r1 = range(10) r2 = iter(range(10, 20)) for x in r1: yield x, next(r2) s1 = Series.from_items(gen()) self.assertEqual(s1.loc[7:9].values.tolist(), [17, 18, 19]) s2 = Series.from_items(dict(a=30, b=40, c=50).items()) self.assertEqual(s2['c'], 50) self.assertEqual(s2['b'], 40) self.assertEqual(s2['a'], 30)
def test_series_from_concat_c(self): s1 = Series(( 2, 3, 0, ), index=list('abc')) s2 = Series(('10', '20'), index=list('de')) s3 = Series((8, 6), index=(1, 2)) s = Series.from_concat((s1, s2, s3)) self.assertEqual(s.to_pairs(), (('a', 2), ('b', 3), ('c', 0), ('d', '10'), ('e', '20'), (1, 8), (2, 6)))
def test_series_from_concat_a(self): s1 = Series(( 2, 3, 0, ), index=list('abc')) s2 = Series((10, 20), index=list('de')) s3 = Series((8, 6), index=list('fg')) s = Series.from_concat((s1, s2, s3)) self.assertEqual(s.to_pairs(), (('a', 2), ('b', 3), ('c', 0), ('d', 10), ('e', 20), ('f', 8), ('g', 6)))
def test_frame_via_fill_value_e(self) -> None: f1 = FrameGO(index=range(5)) f1.via_fill_value(0)['a'] = Series([10, 20], index=(2, 4)) f1.via_fill_value(-1)['b'] = Series([10, 20], index=(0, 1)) self.assertEqual(f1.to_pairs(), (('a', ((0, 0), (1, 0), (2, 10), (3, 0), (4, 20))), ('b', ((0, 10), (1, 20), (2, -1), (3, -1), (4, -1))))) f2 = Frame(index=range(5)) with self.assertRaises(TypeError): f2.via_fill_value(0)['a'] = range(5) # type: ignore #pylint: disable=E1137
def test_series_shift_a(self): s1 = Series((2, 3, 0, -1, 8, 6), index=list('abcdef')) # if the shift is a noop, we reuse the same array self.assertEqual(s1.mloc, s1.shift(0).mloc) # default fill is NaN self.assertEqual(s1.shift(4).dtype, np.dtype('float64')) # import ipdb; ipdb.set_trace() self.assertEqual( s1.shift(4, None).to_pairs(), (('a', None), ('b', None), ('c', None), ('d', None), ('e', 2), ('f', 3))) self.assertEqual( s1.shift(-4, None).to_pairs(), (('a', 8), ('b', 6), ('c', None), ('d', None), ('e', None), ('f', None))) self.assertEqual( s1.shift(6, None).to_pairs(), (('a', None), ('b', None), ('c', None), ('d', None), ('e', None), ('f', None))) self.assertEqual( s1.shift(-6, None).to_pairs(), (('a', None), ('b', None), ('c', None), ('d', None), ('e', None), ('f', None)))
def test_series_assign_c(self): s1 = Series(range(4), index=('a', 'b', 'c', 'd')) self.assertEqual(s1.assign.loc['c':](0).to_pairs(), (('a', 0), ('b', 1), ('c', 0), ('d', 0))) self.assertEqual(s1.assign.loc['c':]((20, 30)).to_pairs(), (('a', 0), ('b', 1), ('c', 20), ('d', 30))) self.assertEqual(s1.assign['c':](s1['c':] * 10).to_pairs(), (('a', 0), ('b', 1), ('c', 20), ('d', 30))) self.assertEqual( s1.assign['c':](Series({ 'd': 40, 'c': 60 })).to_pairs(), (('a', 0), ('b', 1), ('c', 60), ('d', 40)))
def test_series_from_pandas_a(self): import pandas as pd pds = pd.Series([3, 4, 5], index=list('abc')) sfs = Series.from_pandas(pds) self.assertEqual(list(pds.items()), list(sfs.items())) # mutate Pandas pds['c'] = 50 self.assertNotEqual(pds['c'], sfs['c']) # owning data pds = pd.Series([3, 4, 5], index=list('abc')) sfs = Series.from_pandas(pds, own_data=True) self.assertEqual(list(pds.items()), list(sfs.items()))
def test_index_date_series_a(self) -> None: s = Series(range(62), index=IndexDate.from_year_month_range('2017-12', '2018-01')) self.assertEqual(s.sum(), 1891) self.assertEqual(s.loc[s.index == '2018-01'].sum(), 1426) self.assertEqual(s.loc[s.index == '2017-12'].sum(), 465) self.assertEqual(s['2018-01-24'], 54) self.assertEqual( s['2018-01-28':].to_pairs(), # type: ignore # https://github.com/python/typeshed/pull/3024 ((np.datetime64('2018-01-28'), 58), (np.datetime64('2018-01-29'), 59), (np.datetime64('2018-01-30'), 60), (np.datetime64('2018-01-31'), 61)) )
def test_series_from_concat_d(self): s1 = Series(( 2, 3, 0, ), index=list('abc')).reindex_add_level('i') s2 = Series(('10', '20', '100'), index=list('abc')).reindex_add_level('ii') s3 = Series.from_concat((s1, s2)) self.assertEqual(s3.to_pairs(), ((('i', 'a'), 2), (('i', 'b'), 3), (('i', 'c'), 0), (('ii', 'a'), '10'), (('ii', 'b'), '20'), (('ii', 'c'), '100')))
def test_hierarchy_display_a(self): OD = OrderedDict tree = OD([ ('I', OD([('A', (1, 2)), ('B', (1, 2))])), ('II', OD([('A', (1, 2)), ('B', (1, 2))])), ]) ih = IndexHierarchy.from_tree(tree) post = ih.display() self.assertEqual(len(post), 10) s = Series(range(8), index=ih) post = s.display() self.assertEqual(len(post), 11)
def test_index_loc_to_iloc_boolen_a(self): idx = Index(('a', 'b', 'c', 'd')) # returns nothing as index does not match anything post = idx.loc_to_iloc(Series([False, True, False, True])) self.assertTrue(len(post) == 0) post = idx.loc_to_iloc( Series([False, True, False, True], index=('b', 'c', 'd', 'a'))) self.assertEqual(post.tolist(), [0, 2]) post = idx.loc_to_iloc( Series([False, True, False, True], index=list('abcd'))) self.assertEqual(post.tolist(), [1, 3])
def test_series_binary_operator_d(self): s1 = Series(range(4), index=list('abcd')) s2 = Series(range(3), index=list('abc')) s3 = s1 + s2 self.assertEqual( s3.fillna(None).to_pairs(), (('a', 0), ('b', 2), ('c', 4), ('d', None))) s1 = Series((False, True, False, True), index=list('abcd')) s2 = Series([True] * 3, index=list('abc')) # NOTE: for now, we cannot resolve this case, as after reindexing we get an object array that is not compatible with Boolean array for the NaN4 with self.assertRaises(TypeError): s3 = s1 | s2
def test_series_init_a(self): s1 = Series(np.nan, index=('a', 'b', 'c', 'd')) self.assertTrue(s1.dtype == float) self.assertTrue(len(s1) == 4) s2 = Series(False, index=('a', 'b', 'c', 'd')) self.assertTrue(s2.dtype == bool) self.assertTrue(len(s2) == 4) s3 = Series(None, index=('a', 'b', 'c', 'd')) self.assertTrue(s3.dtype == object) self.assertTrue(len(s3) == 4)
def test_series_to_pandas_b(self): from pandas import Timestamp s1 = Series(range(4), index=IndexDate(('2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05'))) df = s1.to_pandas() self.assertEqual(df.index.tolist(), [ Timestamp('2018-01-02 00:00:00'), Timestamp('2018-01-03 00:00:00'), Timestamp('2018-01-04 00:00:00'), Timestamp('2018-01-05 00:00:00') ]) self.assertEqual(df.values.tolist(), [0, 1, 2, 3])
def test_series_group_c(self): s1 = Series((10, 10, 10, 20, 20), index=('a', 'b', 'c', 'd', 'e'), dtype=object) groups = tuple(s1.iter_group()) self.assertEqual([g.sum() for g in groups], [30, 40]) self.assertEqual(s1.iter_group().apply(np.sum).to_pairs(), ((10, 30), (20, 40))) self.assertEqual( s1.iter_group_items().apply(lambda g, s: (g * s).values.tolist()).to_pairs(), ((10, [100, 100, 100]), (20, [400, 400])))