def test_squeeze(self): # noop for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries() ]: tm.assert_series_equal(s.squeeze(), s) for df in [tm.makeTimeDataFrame()]: tm.assert_frame_equal(df.squeeze(), df) with catch_warnings(record=True): for p in [tm.makePanel()]: tm.assert_panel_equal(p.squeeze(), p) with catch_warnings(record=True): for p4d in [tm.makePanel4D()]: tm.assert_panel4d_equal(p4d.squeeze(), p4d) # squeezing df = tm.makeTimeDataFrame().reindex(columns=['A']) tm.assert_series_equal(df.squeeze(), df['A']) with catch_warnings(record=True): p = tm.makePanel().reindex(items=['ItemA']) tm.assert_frame_equal(p.squeeze(), p['ItemA']) p = tm.makePanel().reindex(items=['ItemA'], minor_axis=['A']) tm.assert_series_equal(p.squeeze(), p.loc['ItemA', :, 'A']) with catch_warnings(record=True): p4d = tm.makePanel4D().reindex(labels=['label1']) tm.assert_panel_equal(p4d.squeeze(), p4d['label1']) with catch_warnings(record=True): p4d = tm.makePanel4D().reindex(labels=['label1'], items=['ItemA']) tm.assert_frame_equal(p4d.squeeze(), p4d.loc['label1', 'ItemA']) # don't fail with 0 length dimensions GH11229 & GH8999 empty_series = Series([], name='five') empty_frame = DataFrame([empty_series]) with catch_warnings(record=True): empty_panel = Panel({'six': empty_frame}) [ tm.assert_series_equal(empty_series, higher_dim.squeeze()) for higher_dim in [empty_series, empty_frame, empty_panel] ] # axis argument df = tm.makeTimeDataFrame(nper=1).iloc[:, :1] assert df.shape == (1, 1) tm.assert_series_equal(df.squeeze(axis=0), df.iloc[0]) tm.assert_series_equal(df.squeeze(axis='index'), df.iloc[0]) tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0]) tm.assert_series_equal(df.squeeze(axis='columns'), df.iloc[:, 0]) assert df.squeeze() == df.iloc[0, 0] pytest.raises(ValueError, df.squeeze, axis=2) pytest.raises(ValueError, df.squeeze, axis='x') df = tm.makeTimeDataFrame(3) tm.assert_frame_equal(df.squeeze(axis=0), df)
def test_isnull(self): self.assertFalse(isnull(1.)) self.assertTrue(isnull(None)) self.assertTrue(isnull(np.NaN)) self.assertTrue(float('nan')) self.assertFalse(isnull(np.inf)) self.assertFalse(isnull(-np.inf)) # series for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries()]: assert isinstance(isnull(s), Series) # frame for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel with catch_warnings(record=True): for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel())]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d with catch_warnings(record=True): for p in [tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D())]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected)
def test_isnull(): assert not isnull(1.) assert isnull(None) assert isnull(np.NaN) assert not isnull(np.inf) assert not isnull(-np.inf) # series for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries()]: assert (isinstance(isnull(s), Series)) # frame for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d for p in [tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D())]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected)
def test_5d_construction(self): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name='Panel4D', orders=['labels1', 'items', 'major_axis', 'minor_axis'], slices={'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis'}, slicer=Panel, aliases={'major': 'major_axis', 'minor': 'minor_axis'}, stat_axis=2) p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # create a 5D Panel5D = panelnd.create_nd_panel_factory( klass_name='Panel5D', orders=['cool1', 'labels1', 'items', 'major_axis', 'minor_axis'], slices={'labels1': 'labels1', 'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis'}, slicer=Panel4D, aliases={'major': 'major_axis', 'minor': 'minor_axis'}, stat_axis=2) p5d = Panel5D(dict(C1=p4d)) # slice back to 4d results = p5d.ix['C1', :, :, 0:3, :] expected = p4d.ix[:, :, 0:3, :] assert_panel_equal(results['L1'], expected['L1'])
def test_isnull(): assert not isnull(1.) assert isnull(None) assert isnull(np.NaN) assert not isnull(np.inf) assert not isnull(-np.inf) # series for s in [tm.makeFloatSeries(),tm.makeStringSeries(), tm.makeObjectSeries(),tm.makeTimeSeries(),tm.makePeriodSeries()]: assert(isinstance(isnull(s), Series)) # frame for df in [tm.makeTimeDataFrame(),tm.makePeriodFrame(),tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel for p in [ tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d for p in [ tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D()) ]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected)
def test_isna_isnull(self, isna_f): assert not isna_f(1.) assert isna_f(None) assert isna_f(np.NaN) assert float('nan') assert not isna_f(np.inf) assert not isna_f(-np.inf) # series for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries()]: assert isinstance(isna_f(s), Series) # frame for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame()]: result = isna_f(df) expected = df.apply(isna_f) tm.assert_frame_equal(result, expected) # panel with catch_warnings(record=True): simplefilter("ignore", FutureWarning) for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel())]: result = isna_f(p) expected = p.apply(isna_f) tm.assert_panel_equal(result, expected)
def test_squeeze(self): # noop for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries() ]: tm.assert_series_equal(s.squeeze(), s) for df in [tm.makeTimeDataFrame()]: tm.assert_frame_equal(df.squeeze(), df) for p in [tm.makePanel()]: tm.assert_panel_equal(p.squeeze(), p) for p4d in [tm.makePanel4D()]: tm.assert_panel4d_equal(p4d.squeeze(), p4d) # squeezing df = tm.makeTimeDataFrame().reindex(columns=['A']) tm.assert_series_equal(df.squeeze(), df['A']) p = tm.makePanel().reindex(items=['ItemA']) tm.assert_frame_equal(p.squeeze(), p['ItemA']) p = tm.makePanel().reindex(items=['ItemA'], minor_axis=['A']) tm.assert_series_equal(p.squeeze(), p.ix['ItemA', :, 'A']) p4d = tm.makePanel4D().reindex(labels=['label1']) tm.assert_panel_equal(p4d.squeeze(), p4d['label1']) p4d = tm.makePanel4D().reindex(labels=['label1'], items=['ItemA']) tm.assert_frame_equal(p4d.squeeze(), p4d.ix['label1', 'ItemA'])
def test_isnull(self): self.assertFalse(isnull(1.)) self.assertTrue(isnull(None)) self.assertTrue(isnull(np.NaN)) self.assertTrue(float('nan')) self.assertFalse(isnull(np.inf)) self.assertFalse(isnull(-np.inf)) # series for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries()]: self.assertIsInstance(isnull(s), Series) # frame for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame()]: result = isnull(df) expected = df.apply(isnull) tm.assert_frame_equal(result, expected) # panel with catch_warnings(record=True): for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel())]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel_equal(result, expected) # panel 4d with catch_warnings(record=True): for p in [tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D())]: result = isnull(p) expected = p.apply(isnull) tm.assert_panel4d_equal(result, expected)
def test_isna_isnull(self, isna_f): assert not isna_f(1.) assert isna_f(None) assert isna_f(np.NaN) assert float('nan') assert not isna_f(np.inf) assert not isna_f(-np.inf) # series for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries(), tm.makeTimeSeries(), tm.makePeriodSeries()]: assert isinstance(isna_f(s), Series) # frame for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(), tm.makeMixedDataFrame()]: result = isna_f(df) expected = df.apply(isna_f) tm.assert_frame_equal(result, expected) # panel with catch_warnings(record=True): for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel())]: result = isna_f(p) expected = p.apply(isna_f) tm.assert_panel_equal(result, expected)
def test_squeeze(self): # noop for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries() ]: tm.assert_series_equal(s.squeeze(),s) for df in [ tm.makeTimeDataFrame() ]: tm.assert_frame_equal(df.squeeze(),df) for p in [ tm.makePanel() ]: tm.assert_panel_equal(p.squeeze(),p) for p4d in [ tm.makePanel4D() ]: tm.assert_panel4d_equal(p4d.squeeze(),p4d) # squeezing df = tm.makeTimeDataFrame().reindex(columns=['A']) tm.assert_series_equal(df.squeeze(),df['A']) p = tm.makePanel().reindex(items=['ItemA']) tm.assert_frame_equal(p.squeeze(),p['ItemA']) p = tm.makePanel().reindex(items=['ItemA'],minor_axis=['A']) tm.assert_series_equal(p.squeeze(),p.ix['ItemA',:,'A']) p4d = tm.makePanel4D().reindex(labels=['label1']) tm.assert_panel_equal(p4d.squeeze(),p4d['label1']) p4d = tm.makePanel4D().reindex(labels=['label1'],items=['ItemA']) tm.assert_frame_equal(p4d.squeeze(),p4d.ix['label1','ItemA'])
def test_5d_construction(self): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name="Panel4D", axis_orders=["labels1", "items", "major_axis", "minor_axis"], axis_slices={"items": "items", "major_axis": "major_axis", "minor_axis": "minor_axis"}, slicer=Panel, axis_aliases={"major": "major_axis", "minor": "minor_axis"}, stat_axis=2, ) p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # create a 5D Panel5D = panelnd.create_nd_panel_factory( klass_name="Panel5D", axis_orders=["cool1", "labels1", "items", "major_axis", "minor_axis"], axis_slices={ "labels1": "labels1", "items": "items", "major_axis": "major_axis", "minor_axis": "minor_axis", }, slicer=Panel4D, axis_aliases={"major": "major_axis", "minor": "minor_axis"}, stat_axis=2, ) p5d = Panel5D(dict(C1=p4d)) # slice back to 4d results = p5d.ix["C1", :, :, 0:3, :] expected = p4d.ix[:, :, 0:3, :] assert_panel_equal(results["L1"], expected["L1"])
def test_keys(self): self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() self.store['foo/bar'] = tm.makePanel() self.assertEquals(len(self.store), 5) self.assert_(set(self.store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar']))
def test_repr(self): repr(self.store) self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() self.store.append('e', tm.makePanel()) repr(self.store) str(self.store)
def test_5d_construction(self): with catch_warnings(record=True): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name='Panel4D', orders=['labels1', 'items', 'major_axis', 'minor_axis'], slices={ 'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis' }, slicer=Panel, aliases={ 'major': 'major_axis', 'minor': 'minor_axis' }, stat_axis=2) # deprecation GH13564 p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # create a 5D Panel5D = panelnd.create_nd_panel_factory(klass_name='Panel5D', orders=[ 'cool1', 'labels1', 'items', 'major_axis', 'minor_axis' ], slices={ 'labels1': 'labels1', 'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis' }, slicer=Panel4D, aliases={ 'major': 'major_axis', 'minor': 'minor_axis' }, stat_axis=2) # deprecation GH13564 p5d = Panel5D(dict(C1=p4d)) # slice back to 4d results = p5d.iloc[p5d.cool1.get_loc('C1'), :, :, 0:3, :] expected = p4d.iloc[:, :, 0:3, :] assert_panel_equal(results['L1'], expected['L1'])
def test_5d_construction(self): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name='Panel4D', orders=['labels1', 'items', 'major_axis', 'minor_axis'], slices={ 'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis' }, slicer=Panel, aliases={ 'major': 'major_axis', 'minor': 'minor_axis' }, stat_axis=2) # deprecation GH13564 p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # create a 5D Panel5D = panelnd.create_nd_panel_factory(klass_name='Panel5D', orders=[ 'cool1', 'labels1', 'items', 'major_axis', 'minor_axis' ], slices={ 'labels1': 'labels1', 'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis' }, slicer=Panel4D, aliases={ 'major': 'major_axis', 'minor': 'minor_axis' }, stat_axis=2) # deprecation GH13564 p5d = Panel5D(dict(C1=p4d)) # slice back to 4d results = p5d.ix['C1', :, :, 0:3, :] expected = p4d.ix[:, :, 0:3, :] assert_panel_equal(results['L1'], expected['L1'])
def test_keys(self): self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() self.store['foo/bar'] = tm.makePanel() self.assertEquals(len(self.store), 5) self.assert_( set(self.store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar' ]))
def test_squeeze(self): # noop for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]: tm.assert_series_equal(s.squeeze(), s) for df in [tm.makeTimeDataFrame()]: tm.assert_frame_equal(df.squeeze(), df) with catch_warnings(record=True): for p in [tm.makePanel()]: tm.assert_panel_equal(p.squeeze(), p) with catch_warnings(record=True): for p4d in [tm.makePanel4D()]: tm.assert_panel4d_equal(p4d.squeeze(), p4d) # squeezing df = tm.makeTimeDataFrame().reindex(columns=['A']) tm.assert_series_equal(df.squeeze(), df['A']) with catch_warnings(record=True): p = tm.makePanel().reindex(items=['ItemA']) tm.assert_frame_equal(p.squeeze(), p['ItemA']) p = tm.makePanel().reindex(items=['ItemA'], minor_axis=['A']) tm.assert_series_equal(p.squeeze(), p.loc['ItemA', :, 'A']) with catch_warnings(record=True): p4d = tm.makePanel4D().reindex(labels=['label1']) tm.assert_panel_equal(p4d.squeeze(), p4d['label1']) with catch_warnings(record=True): p4d = tm.makePanel4D().reindex(labels=['label1'], items=['ItemA']) tm.assert_frame_equal(p4d.squeeze(), p4d.loc['label1', 'ItemA']) # don't fail with 0 length dimensions GH11229 & GH8999 empty_series = Series([], name='five') empty_frame = DataFrame([empty_series]) with catch_warnings(record=True): empty_panel = Panel({'six': empty_frame}) [tm.assert_series_equal(empty_series, higher_dim.squeeze()) for higher_dim in [empty_series, empty_frame, empty_panel]] # axis argument df = tm.makeTimeDataFrame(nper=1).iloc[:, :1] assert df.shape == (1, 1) tm.assert_series_equal(df.squeeze(axis=0), df.iloc[0]) tm.assert_series_equal(df.squeeze(axis='index'), df.iloc[0]) tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0]) tm.assert_series_equal(df.squeeze(axis='columns'), df.iloc[:, 0]) assert df.squeeze() == df.iloc[0, 0] pytest.raises(ValueError, df.squeeze, axis=2) pytest.raises(ValueError, df.squeeze, axis='x') df = tm.makeTimeDataFrame(3) tm.assert_frame_equal(df.squeeze(axis=0), df)
def test_4d_construction(self): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name = 'Panel4D', axis_orders = ['labels','items','major_axis','minor_axis'], axis_slices = { 'items' : 'items', 'major_axis' : 'major_axis', 'minor_axis' : 'minor_axis' }, slicer = Panel, axis_aliases = { 'major' : 'major_axis', 'minor' : 'minor_axis' }, stat_axis = 2) p4d = Panel4D(dict(L1 = tm.makePanel(), L2 = tm.makePanel()))
def test_4d_construction_alt(self): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name="Panel4D", axis_orders=["labels", "items", "major_axis", "minor_axis"], axis_slices={"items": "items", "major_axis": "major_axis", "minor_axis": "minor_axis"}, slicer="Panel", axis_aliases={"major": "major_axis", "minor": "minor_axis"}, stat_axis=2, ) p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel()))
def test_4d_construction_alt(self): with catch_warnings(record=True): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name='Panel4D', orders=['labels', 'items', 'major_axis', 'minor_axis'], slices={'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis'}, slicer='Panel', aliases={'major': 'major_axis', 'minor': 'minor_axis'}, stat_axis=2) p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # noqa
def test_4d_construction_alt(self): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): # create a 4D Panel4D = panelnd.create_nd_panel_factory( klass_name='Panel4D', orders=['labels', 'items', 'major_axis', 'minor_axis'], slices={'items': 'items', 'major_axis': 'major_axis', 'minor_axis': 'minor_axis'}, slicer='Panel', aliases={'major': 'major_axis', 'minor': 'minor_axis'}, stat_axis=2) p4d = Panel4D(dict(L1=tm.makePanel(), L2=tm.makePanel())) # noqa
def test_panel_join(self): with catch_warnings(record=True): panel = tm.makePanel() tm.add_nans(panel) p1 = panel.iloc[:2, :10, :3] p2 = panel.iloc[2:, 5:, 2:] # left join result = p1.join(p2) expected = p1.copy() expected['ItemC'] = p2['ItemC'] tm.assert_panel_equal(result, expected) # right join result = p1.join(p2, how='right') expected = p2.copy() expected['ItemA'] = p1['ItemA'] expected['ItemB'] = p1['ItemB'] expected = expected.reindex(items=['ItemA', 'ItemB', 'ItemC']) tm.assert_panel_equal(result, expected) # inner join result = p1.join(p2, how='inner') expected = panel.iloc[:, 5:10, 2:3] tm.assert_panel_equal(result, expected) # outer join result = p1.join(p2, how='outer') expected = p1.reindex(major=panel.major_axis, minor=panel.minor_axis) expected = expected.join(p2.reindex(major=panel.major_axis, minor=panel.minor_axis)) tm.assert_panel_equal(result, expected)
def test_select(self): wp = tm.makePanel() # put/select ok self.store.remove('wp') self.store.put('wp', wp, table=True) self.store.select('wp') # non-table ok (where = None) self.store.remove('wp') self.store.put('wp2', wp, table=False) self.store.select('wp2') # selection on the non-indexable with a large number of columns wp = Panel(np.random.randn(100, 100, 100), items=['Item%03d' % i for i in xrange(100)], major_axis=date_range('1/1/2000', periods=100), minor_axis=['E%03d' % i for i in xrange(100)]) self.store.remove('wp') self.store.append('wp', wp) items = ['Item%03d' % i for i in xrange(80)] result = self.store.select('wp', Term('items', items)) expected = wp.reindex(items=items) tm.assert_panel_equal(expected, result)
def test_panel_join_many(self): tm.K = 10 panel = tm.makePanel() tm.K = 4 panels = [panel.ix[:2], panel.ix[2:6], panel.ix[6:]] joined = panels[0].join(panels[1:]) tm.assert_panel_equal(joined, panel) panels = [panel.ix[:2, :-5], panel.ix[2:6, 2:], panel.ix[6:, 5:-7]] data_dict = {} for p in panels: data_dict.update(p.iterkv()) joined = panels[0].join(panels[1:], how='inner') expected = Panel.from_dict(data_dict, intersect=True) tm.assert_panel_equal(joined, expected) joined = panels[0].join(panels[1:], how='outer') expected = Panel.from_dict(data_dict, intersect=False) tm.assert_panel_equal(joined, expected) # edge cases self.assertRaises(ValueError, panels[0].join, panels[1:], how='outer', lsuffix='foo', rsuffix='bar') self.assertRaises(ValueError, panels[0].join, panels[1:], how='right')
def test_transpose(self): msg = (r"transpose\(\) got multiple values for " r"keyword argument 'axes'") for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries() ]: # calls implementation in pandas/core/base.py tm.assert_series_equal(s.transpose(), s) for df in [tm.makeTimeDataFrame()]: tm.assert_frame_equal(df.transpose().transpose(), df) with catch_warnings(record=True): simplefilter("ignore", FutureWarning) for p in [tm.makePanel()]: tm.assert_panel_equal( p.transpose(2, 0, 1).transpose(1, 2, 0), p) tm.assert_raises_regex(TypeError, msg, p.transpose, 2, 0, 1, axes=(2, 0, 1))
def test_append_with_strings(self): wp = tm.makePanel() wp2 = wp.rename_axis(dict([(x, "%s_extra" % x) for x in wp.minor_axis]), axis=2) self.store.append('s1', wp, min_itemsize=20) self.store.append('s1', wp2) expected = concat([wp, wp2], axis=2) expected = expected.reindex(minor_axis=sorted(expected.minor_axis)) tm.assert_panel_equal(self.store['s1'], expected) # test dict format self.store.append('s2', wp, min_itemsize={'column': 20}) self.store.append('s2', wp2) expected = concat([wp, wp2], axis=2) expected = expected.reindex(minor_axis=sorted(expected.minor_axis)) tm.assert_panel_equal(self.store['s2'], expected) # apply the wrong field (similar to #1) self.store.append('s3', wp, min_itemsize={'index': 20}) self.assertRaises(Exception, self.store.append, 's3') # test truncation of bigger strings self.store.append('s4', wp) self.assertRaises(Exception, self.store.append, 's4', wp2)
def test_remove_where(self): # non-existance crit1 = Term('index','>','foo') self.store.remove('a', where=[crit1]) # try to remove non-table (with crit) # non-table ok (where = None) wp = tm.makePanel() self.store.put('wp', wp, table=True) self.store.remove('wp', [('minor_axis', ['A', 'D'])]) rs = self.store.select('wp') expected = wp.reindex(minor_axis = ['B','C']) tm.assert_panel_equal(rs,expected) # empty where self.store.remove('wp') self.store.put('wp', wp, table=True) self.store.remove('wp', []) # non - empty where self.store.remove('wp') self.store.put('wp', wp, table=True) self.assertRaises(Exception, self.store.remove, 'wp', ['foo']) # selectin non-table with a where self.store.put('wp2', wp, table=False) self.assertRaises(Exception, self.store.remove, 'wp2', [('column', ['A', 'D'])])
def test_pivot(self): data = { "index": ["A", "B", "C", "C", "B", "A"], "columns": ["One", "One", "One", "Two", "Two", "Two"], "values": [1.0, 2.0, 3.0, 3.0, 2.0, 1.0], } frame = DataFrame(data) pivoted = frame.pivot(index="index", columns="columns", values="values") expected = DataFrame({"One": {"A": 1.0, "B": 2.0, "C": 3.0}, "Two": {"A": 1.0, "B": 2.0, "C": 3.0}}) expected.index.name, expected.columns.name = "index", "columns" assert_frame_equal(pivoted, expected) # name tracking self.assertEqual(pivoted.index.name, "index") self.assertEqual(pivoted.columns.name, "columns") # don't specify values pivoted = frame.pivot(index="index", columns="columns") self.assertEqual(pivoted.index.name, "index") self.assertEqual(pivoted.columns.names, (None, "columns")) # pivot multiple columns wp = tm.makePanel() lp = wp.to_frame() df = lp.reset_index() assert_frame_equal(df.pivot("major", "minor"), lp.unstack())
def test_len_keys(self): self.store['a'] = tm.makeTimeSeries() self.store['b'] = tm.makeStringSeries() self.store['c'] = tm.makeDataFrame() self.store['d'] = tm.makePanel() self.assertEquals(len(self.store), 4) self.assert_(set(self.store.keys()) == set(['a', 'b', 'c', 'd']))
def test_append(self): pth = '__test_append__.h5' try: store = HDFStore(pth) df = tm.makeTimeDataFrame() store.append('df1', df[:10]) store.append('df1', df[10:]) tm.assert_frame_equal(store['df1'], df) store.put('df2', df[:10], table=True) store.append('df2', df[10:]) tm.assert_frame_equal(store['df2'], df) wp = tm.makePanel() store.append('wp1', wp.ix[:,:10,:]) store.append('wp1', wp.ix[:,10:,:]) tm.assert_panel_equal(store['wp1'], wp) except: raise finally: store.close() os.remove(pth)
def test_append(self): pth = '__test_append__.h5' try: store = HDFStore(pth) df = tm.makeTimeDataFrame() store.append('df1', df[:10]) store.append('df1', df[10:]) tm.assert_frame_equal(store['df1'], df) store.put('df2', df[:10], table=True) store.append('df2', df[10:]) tm.assert_frame_equal(store['df2'], df) wp = tm.makePanel() store.append('wp1', wp.ix[:, :10, :]) store.append('wp1', wp.ix[:, 10:, :]) tm.assert_panel_equal(store['wp1'], wp) except: raise finally: store.close() os.remove(pth)
def test_take(self): indices = [1, 5, -2, 6, 3, -1] for s in [tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries()]: out = s.take(indices) expected = Series(data=s.values.take(indices), index=s.index.take(indices), dtype=s.dtype) tm.assert_series_equal(out, expected) for df in [tm.makeTimeDataFrame()]: out = df.take(indices) expected = DataFrame(data=df.values.take(indices, axis=0), index=df.index.take(indices), columns=df.columns) tm.assert_frame_equal(out, expected) indices = [-3, 2, 0, 1] with catch_warnings(record=True): simplefilter("ignore", FutureWarning) for p in [tm.makePanel()]: out = p.take(indices) expected = Panel(data=p.values.take(indices, axis=0), items=p.items.take(indices), major_axis=p.major_axis, minor_axis=p.minor_axis) tm.assert_panel_equal(out, expected)
def test_cache_updating(self): # GH 4939, make sure to update the cache on setitem df = tm.makeDataFrame() df['A'] # cache series df.ix["Hello Friend"] = df.ix[0] assert "Hello Friend" in df['A'].index assert "Hello Friend" in df['B'].index panel = tm.makePanel() panel.ix[0] # get first item into cache panel.ix[:, :, 'A+1'] = panel.ix[:, :, 'A'] + 1 assert "A+1" in panel.ix[0].columns assert "A+1" in panel.ix[1].columns # 10264 df = DataFrame(np.zeros((5, 5), dtype='int64'), columns=['a', 'b', 'c', 'd', 'e'], index=range(5)) df['f'] = 0 df.f.values[3] = 1 # TODO(wesm): unused? # y = df.iloc[np.arange(2, len(df))] df.f.values[3] = 2 expected = DataFrame(np.zeros((5, 6), dtype='int64'), columns=['a', 'b', 'c', 'd', 'e', 'f'], index=range(5)) expected.at[3, 'f'] = 2 tm.assert_frame_equal(df, expected) expected = Series([0, 0, 0, 2, 0], name='f') tm.assert_series_equal(df.f, expected)
def test_pivot(self): data = { 'index': ['A', 'B', 'C', 'C', 'B', 'A'], 'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'], 'values': [1., 2., 3., 3., 2., 1.] } frame = DataFrame(data) pivoted = frame.pivot( index='index', columns='columns', values='values') expected = DataFrame({ 'One': {'A': 1., 'B': 2., 'C': 3.}, 'Two': {'A': 1., 'B': 2., 'C': 3.} }) expected.index.name, expected.columns.name = 'index', 'columns' assert_frame_equal(pivoted, expected) # name tracking self.assertEqual(pivoted.index.name, 'index') self.assertEqual(pivoted.columns.name, 'columns') # don't specify values pivoted = frame.pivot(index='index', columns='columns') self.assertEqual(pivoted.index.name, 'index') self.assertEqual(pivoted.columns.names, (None, 'columns')) # pivot multiple columns wp = tm.makePanel() lp = wp.to_frame() df = lp.reset_index() assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
def test_panel_join_many(self): with catch_warnings(record=True): tm.K = 10 panel = tm.makePanel() tm.K = 4 panels = [panel.iloc[:2], panel.iloc[2:6], panel.iloc[6:]] joined = panels[0].join(panels[1:]) tm.assert_panel_equal(joined, panel) panels = [panel.iloc[:2, :-5], panel.iloc[2:6, 2:], panel.iloc[6:, 5:-7]] data_dict = {} for p in panels: data_dict.update(p.iteritems()) joined = panels[0].join(panels[1:], how='inner') expected = pd.Panel.from_dict(data_dict, intersect=True) tm.assert_panel_equal(joined, expected) joined = panels[0].join(panels[1:], how='outer') expected = pd.Panel.from_dict(data_dict, intersect=False) tm.assert_panel_equal(joined, expected) # edge cases msg = "Suffixes not supported when passing multiple panels" with pytest.raises(ValueError, match=msg): panels[0].join(panels[1:], how='outer', lsuffix='foo', rsuffix='bar') msg = "Right join not supported with multiple panels" with pytest.raises(ValueError, match=msg): panels[0].join(panels[1:], how='right')
def test_long(self): def _check(left, right): tm.assert_panel_equal(left.to_wide(), right.to_wide()) wp = tm.makePanel() self._check_roundtrip(wp.to_long(), _check)
def test_panel_concat_other_axes(self): panel = tm.makePanel() p1 = panel.ix[:, :5, :] p2 = panel.ix[:, 5:, :] result = concat([p1, p2], axis=1) tm.assert_panel_equal(result, panel) p1 = panel.ix[:, :, :2] p2 = panel.ix[:, :, 2:] result = concat([p1, p2], axis=2) tm.assert_panel_equal(result, panel) # if things are a bit misbehaved p1 = panel.ix[:2, :, :2] p2 = panel.ix[:, :, 2:] p1['ItemC'] = 'baz' result = concat([p1, p2], axis=2) expected = panel.copy() expected['ItemC'] = expected['ItemC'].astype('O') expected.ix['ItemC', :, :2] = 'baz' tm.assert_panel_equal(result, expected)
def test_take(self): indices = [1, 5, -2, 6, 3, -1] for s in [ tm.makeFloatSeries(), tm.makeStringSeries(), tm.makeObjectSeries() ]: out = s.take(indices) expected = Series(data=s.values.take(indices), index=s.index.take(indices), dtype=s.dtype) tm.assert_series_equal(out, expected) for df in [tm.makeTimeDataFrame()]: out = df.take(indices) expected = DataFrame(data=df.values.take(indices, axis=0), index=df.index.take(indices), columns=df.columns) tm.assert_frame_equal(out, expected) indices = [-3, 2, 0, 1] with catch_warnings(record=True): for p in [tm.makePanel()]: out = p.take(indices) expected = Panel(data=p.values.take(indices, axis=0), items=p.items.take(indices), major_axis=p.major_axis, minor_axis=p.minor_axis) tm.assert_panel_equal(out, expected)
def test_take_invalid_kwargs(self): indices = [-3, 2, 0, 1] s = tm.makeFloatSeries() df = tm.makeTimeDataFrame() with catch_warnings(record=True): p = tm.makePanel() for obj in (s, df, p): msg = r"take\(\) got an unexpected keyword argument 'foo'" tm.assert_raises_regex(TypeError, msg, obj.take, indices, foo=2) msg = "the 'out' parameter is not supported" tm.assert_raises_regex(ValueError, msg, obj.take, indices, out=indices) msg = "the 'mode' parameter is not supported" tm.assert_raises_regex(ValueError, msg, obj.take, indices, mode='clip')
def test_append_diff_item_order(self): wp = tm.makePanel() wp1 = wp.ix[:, :10, :] wp2 = wp.ix[['ItemC', 'ItemB', 'ItemA'], 10:, :] self.store.put('panel', wp1, table=True) self.assertRaises(Exception, self.store.put, 'panel', wp2, append=True)
def test_create_table_index(self): wp = tm.makePanel() self.store.append('p5', wp) self.store.create_table_index('p5') assert (self.store.handle.root.p5.table.cols.index.is_indexed == True) assert ( self.store.handle.root.p5.table.cols.column.is_indexed == False) df = tm.makeTimeDataFrame() self.store.append('f', df[:10]) self.store.append('f', df[10:]) self.store.create_table_index('f') # create twice self.store.create_table_index('f') # try to index a non-table self.store.put('f2', df) self.assertRaises(Exception, self.store.create_table_index, 'f2') # try to change the version supports flag from pandas.io import pytables pytables._table_supports_index = False self.assertRaises(Exception, self.store.create_table_index, 'f')
def test_numpy_transpose(self): msg = "the 'axes' parameter is not supported" s = tm.makeFloatSeries() tm.assert_series_equal( np.transpose(s), s) tm.assert_raises_regex(ValueError, msg, np.transpose, s, axes=1) df = tm.makeTimeDataFrame() tm.assert_frame_equal(np.transpose( np.transpose(df)), df) tm.assert_raises_regex(ValueError, msg, np.transpose, df, axes=1) with catch_warnings(record=True): p = tm.makePanel() tm.assert_panel_equal(np.transpose( np.transpose(p, axes=(2, 0, 1)), axes=(1, 2, 0)), p) with catch_warnings(record=True): p4d = tm.makePanel4D() tm.assert_panel4d_equal(np.transpose( np.transpose(p4d, axes=(2, 0, 3, 1)), axes=(1, 3, 0, 2)), p4d)
def test_remove_where(self): # non-existance crit1 = Term('index', '>', 'foo') self.store.remove('a', where=[crit1]) # try to remove non-table (with crit) # non-table ok (where = None) wp = tm.makePanel() self.store.put('wp', wp, table=True) self.store.remove('wp', [('column', ['A', 'D'])]) rs = self.store.select('wp') expected = wp.reindex(minor_axis=['B', 'C']) tm.assert_panel_equal(rs, expected) # empty where self.store.remove('wp') self.store.put('wp', wp, table=True) self.store.remove('wp', []) # non - empty where self.store.remove('wp') self.store.put('wp', wp, table=True) self.assertRaises(Exception, self.store.remove, 'wp', ['foo']) # selectin non-table with a where self.store.put('wp2', wp, table=False) self.assertRaises(Exception, self.store.remove, 'wp2', [('column', ['A', 'D'])])
def test_pivot(self): data = { 'index': ['A', 'B', 'C', 'C', 'B', 'A'], 'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'], 'values': [1., 2., 3., 3., 2., 1.] } frame = DataFrame(data) pivoted = frame.pivot( index='index', columns='columns', values='values') expected = DataFrame({ 'One': {'A': 1., 'B': 2., 'C': 3.}, 'Two': {'A': 1., 'B': 2., 'C': 3.} }) expected.index.name, expected.columns.name = 'index', 'columns' tm.assert_frame_equal(pivoted, expected) # name tracking assert pivoted.index.name == 'index' assert pivoted.columns.name == 'columns' # don't specify values pivoted = frame.pivot(index='index', columns='columns') assert pivoted.index.name == 'index' assert pivoted.columns.names == (None, 'columns') with catch_warnings(record=True): # pivot multiple columns wp = tm.makePanel() lp = wp.to_frame() df = lp.reset_index() tm.assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
def test_panel_join(self): panel = tm.makePanel() tm.add_nans(panel) p1 = panel.ix[:2, :10, :3] p2 = panel.ix[2:, 5:, 2:] # left join result = p1.join(p2) expected = p1.copy() expected["ItemC"] = p2["ItemC"] tm.assert_panel_equal(result, expected) # right join result = p1.join(p2, how="right") expected = p2.copy() expected["ItemA"] = p1["ItemA"] expected["ItemB"] = p1["ItemB"] expected = expected.reindex(items=["ItemA", "ItemB", "ItemC"]) tm.assert_panel_equal(result, expected) # inner join result = p1.join(p2, how="inner") expected = panel.ix[:, 5:10, 2:3] tm.assert_panel_equal(result, expected) # outer join result = p1.join(p2, how="outer") expected = p1.reindex(major=panel.major_axis, minor=panel.minor_axis) expected = expected.join(p2.reindex(major=panel.major_axis, minor=panel.minor_axis)) tm.assert_panel_equal(result, expected)
def test_cache_updating(self): # GH 4939, make sure to update the cache on setitem df = tm.makeDataFrame() df['A'] # cache series df.ix["Hello Friend"] = df.ix[0] assert "Hello Friend" in df['A'].index assert "Hello Friend" in df['B'].index panel = tm.makePanel() panel.ix[0] # get first item into cache panel.ix[:, :, 'A+1'] = panel.ix[:, :, 'A'] + 1 assert "A+1" in panel.ix[0].columns assert "A+1" in panel.ix[1].columns # 10264 df = DataFrame(np.zeros((5, 5), dtype='int64'), columns=[ 'a', 'b', 'c', 'd', 'e'], index=range(5)) df['f'] = 0 df.f.values[3] = 1 # TODO(wesm): unused? # y = df.iloc[np.arange(2, len(df))] df.f.values[3] = 2 expected = DataFrame(np.zeros((5, 6), dtype='int64'), columns=[ 'a', 'b', 'c', 'd', 'e', 'f'], index=range(5)) expected.at[3, 'f'] = 2 tm.assert_frame_equal(df, expected) expected = Series([0, 0, 0, 2, 0], name='f') tm.assert_series_equal(df.f, expected)
def test_pivot(self): data = { 'index': ['A', 'B', 'C', 'C', 'B', 'A'], 'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'], 'values': [1., 2., 3., 3., 2., 1.] } frame = DataFrame(data) pivoted = frame.pivot( index='index', columns='columns', values='values') expected = DataFrame({ 'One': {'A': 1., 'B': 2., 'C': 3.}, 'Two': {'A': 1., 'B': 2., 'C': 3.} }) expected.index.name, expected.columns.name = 'index', 'columns' tm.assert_frame_equal(pivoted, expected) # name tracking assert pivoted.index.name == 'index' assert pivoted.columns.name == 'columns' # don't specify values pivoted = frame.pivot(index='index', columns='columns') assert pivoted.index.name == 'index' assert pivoted.columns.names == (None, 'columns') with catch_warnings(record=True): # pivot multiple columns simplefilter("ignore", FutureWarning) wp = tm.makePanel() lp = wp.to_frame() df = lp.reset_index() tm.assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
def test_errors(self): for obj in [pd.Timestamp('20130101'), tm.makePanel()]: def f(): hash_pandas_object(f) self.assertRaises(TypeError, f)
def test_panel_join_many(self): tm.K = 10 panel = tm.makePanel() tm.K = 4 panels = [panel.ix[:2], panel.ix[2:6], panel.ix[6:]] joined = panels[0].join(panels[1:]) tm.assert_panel_equal(joined, panel) panels = [panel.ix[:2, :-5], panel.ix[2:6, 2:], panel.ix[6:, 5:-7]] data_dict = {} for p in panels: data_dict.update(p.iteritems()) joined = panels[0].join(panels[1:], how='inner') expected = Panel.from_dict(data_dict, intersect=True) tm.assert_panel_equal(joined, expected) joined = panels[0].join(panels[1:], how='outer') expected = Panel.from_dict(data_dict, intersect=False) tm.assert_panel_equal(joined, expected) # edge cases self.assertRaises(ValueError, panels[0].join, panels[1:], how='outer', lsuffix='foo', rsuffix='bar') self.assertRaises(ValueError, panels[0].join, panels[1:], how='right')
def test_panel_join(self): panel = tm.makePanel() tm.add_nans(panel) p1 = panel.ix[:2, :10, :3] p2 = panel.ix[2:, 5:, 2:] # left join result = p1.join(p2) expected = p1.copy() expected['ItemC'] = p2['ItemC'] tm.assert_panel_equal(result, expected) # right join result = p1.join(p2, how='right') expected = p2.copy() expected['ItemA'] = p1['ItemA'] expected['ItemB'] = p1['ItemB'] expected = expected.reindex(items=['ItemA', 'ItemB', 'ItemC']) tm.assert_panel_equal(result, expected) # inner join result = p1.join(p2, how='inner') expected = panel.ix[:, 5:10, 2:3] tm.assert_panel_equal(result, expected) # outer join result = p1.join(p2, how='outer') expected = p1.reindex(major=panel.major_axis, minor=panel.minor_axis) expected = expected.join( p2.reindex(major=panel.major_axis, minor=panel.minor_axis)) tm.assert_panel_equal(result, expected)
def test_remove_crit(self): wp = tm.makePanel() # group row removal date4 = wp.major_axis.take([ 0,1,2,4,5,6,8,9,10 ]) crit4 = Term('major_axis',date4) self.store.put('wp3', wp, table=True) n = self.store.remove('wp3', where=[crit4]) assert(n == 36) result = self.store.select('wp3') expected = wp.reindex(major_axis = wp.major_axis-date4) tm.assert_panel_equal(result, expected) # upper half self.store.put('wp', wp, table=True) date = wp.major_axis[len(wp.major_axis) // 2] crit1 = Term('major_axis','>',date) crit2 = Term('minor_axis',['A', 'D']) n = self.store.remove('wp', where=[crit1]) assert(n == 56) n = self.store.remove('wp', where=[crit2]) assert(n == 32) result = self.store['wp'] expected = wp.truncate(after=date).reindex(minor=['B', 'C']) tm.assert_panel_equal(result, expected) # individual row elements self.store.put('wp2', wp, table=True) date1 = wp.major_axis[1:3] crit1 = Term('major_axis',date1) self.store.remove('wp2', where=[crit1]) result = self.store.select('wp2') expected = wp.reindex(major_axis=wp.major_axis-date1) tm.assert_panel_equal(result, expected) date2 = wp.major_axis[5] crit2 = Term('major_axis',date2) self.store.remove('wp2', where=[crit2]) result = self.store['wp2'] expected = wp.reindex(major_axis=wp.major_axis-date1-Index([date2])) tm.assert_panel_equal(result, expected) date3 = [wp.major_axis[7],wp.major_axis[9]] crit3 = Term('major_axis',date3) self.store.remove('wp2', where=[crit3]) result = self.store['wp2'] expected = wp.reindex(major_axis=wp.major_axis-date1-Index([date2])-Index(date3)) tm.assert_panel_equal(result, expected) # corners self.store.put('wp4', wp, table=True) n = self.store.remove('wp4', where=[Term('major_axis','>',wp.major_axis[-1])]) result = self.store.select('wp4') tm.assert_panel_equal(result, wp)
def test_pandas_errors(self): with pytest.raises(TypeError): hash_pandas_object(pd.Timestamp('20130101')) obj = tm.makePanel() with pytest.raises(TypeError): hash_pandas_object(obj)