Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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"])
Exemplo n.º 3
0
def test_resample_panel():
    rng = date_range('1/1/2000', '6/30/2000')
    n = len(rng)

    with catch_warnings(record=True):
        simplefilter("ignore", FutureWarning)
        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1).mean()

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M').mean())
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2).mean()
        expected = p_apply(panel2,
                           lambda x: x.resample('M', axis=1).mean())
        tm.assert_panel_equal(result, expected)
Exemplo n.º 4
0
    def test_setitem(self):
        with catch_warnings(record=True):

            # Panel
            p = Panel(dict(
                ItemA=self.panel4d['l1']['ItemA'][2:].filter(
                    items=['A', 'B'])))
            self.panel4d['l4'] = p
            self.panel4d['l5'] = p

            p2 = self.panel4d['l4']

            tm.assert_panel_equal(p, p2.reindex(items=p.items,
                                                major_axis=p.major_axis,
                                                minor_axis=p.minor_axis))

            # scalar
            self.panel4d['lG'] = 1
            self.panel4d['lE'] = True
            assert self.panel4d['lG'].values.dtype == np.int64
            assert self.panel4d['lE'].values.dtype == np.bool_

            # object dtype
            self.panel4d['lQ'] = 'foo'
            assert self.panel4d['lQ'].values.dtype == np.object_

            # boolean dtype
            self.panel4d['lP'] = self.panel4d['l1'] > 0
            assert self.panel4d['lP'].values.dtype == np.bool_
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
def panels_equal(pl1, pl2):
    """Wrapper of assert_panel_equal from Pandas library."""
    try:
        assert_panel_equal(pl1, pl2)
        return True
    except AssertionError:
        return False
Exemplo n.º 7
0
    def test_to_frame(self):
        # filtered
        filtered = self.panel.to_frame()
        expected = self.panel.to_frame().dropna(how="any")
        assert_frame_equal(filtered, expected)

        # unfiltered
        unfiltered = self.panel.to_frame(filter_observations=False)
        assert_panel_equal(unfiltered.to_panel(), self.panel)

        # names
        self.assertEqual(unfiltered.index.names, ["major", "minor"])

        # unsorted, round trip
        df = self.panel.to_frame(filter_observations=False)
        unsorted = df.take(np.random.permutation(len(df)))
        pan = unsorted.to_panel()
        assert_panel_equal(pan, self.panel)

        # preserve original index names
        df = DataFrame(
            np.random.randn(6, 2), index=[["a", "a", "b", "b", "c", "c"], [0, 1, 0, 1, 0, 1]], columns=["one", "two"]
        )
        df.index.names = ["foo", "bar"]
        df.columns.name = "baz"

        rdf = df.to_panel().to_frame()
        self.assertEqual(rdf.index.names, df.index.names)
        self.assertEqual(rdf.columns.names, df.columns.names)
Exemplo n.º 8
0
    def test_swaplevel_panel(self):
        panel = Panel({"ItemA": self.frame, "ItemB": self.frame * 2})

        result = panel.swaplevel(0, 1, axis="major")
        expected = panel.copy()
        expected.major_axis = expected.major_axis.swaplevel(0, 1)
        tm.assert_panel_equal(result, expected)
Exemplo n.º 9
0
def test_epochs():
    ds = mock_dataset(hz=1000, ticks_per_recspan=100)
    ds.add_event(0, 10, 11, {"a": True})
    ds.add_event(0, 20, 21, {"a": False})
    ds.add_event(0, 98, 99, {"a": False})
    ds.add_event(1, 30, 31, {"a": True})

    epochs = ds.epochs("a", -1.5, 3.5)
    assert np.all(epochs.items == [0, 1])
    assert np.all(epochs.major_axis == [-1, 0, 1, 2, 3])
    assert np.all(epochs.minor_axis == ds.data_format.channel_names)
    assert np.all(epochs[0] == np.asarray(ds[0].iloc[9:14, :]))
    assert np.all(epochs[1] == np.asarray(ds[1].iloc[29:34, :]))

    epochs2 = ds.epochs_ticks("a", -1, 4)
    from pandas.util.testing import assert_panel_equal
    assert_panel_equal(epochs, epochs2)

    ds.epochs("has a", -1, 1)
    assert_raises(ValueError, ds.epochs, "has a", -1, 2)
    epochs3 = ds.epochs("has a", -1, 2, incomplete_action="drop")
    # it's epoch #2 that is incomplete
    assert np.all(epochs3.items == [0, 1, 3])
    assert_raises(ValueError, ds.epochs, "has a", -1, 2,
                  incomplete_action="asdf")
Exemplo n.º 10
0
    def test_new_junctions_neg(self):
        ind = [u'chr2:2-25', u'chr2:3-25', u'chr2:5-20', u'chr2:5-30',
               u'chr2:10-20', u'chr2:30-40']
        d = cpb.star._make_sj_out_dict([SJ_NEG_NONEW_A,
                                       SJ_NEG_NEW_A])
        df = d[SJ_NEG_NONEW_A].ix[ind, cpb.star.COUNT_COLS]
        df = df.fillna(0)
        df2 = d[SJ_NEG_NEW_A].ix[ind, cpb.star.COUNT_COLS]
        df2 = df2.fillna(0)

        p = pd.Panel({SJ_NEG_NONEW_A:df,
                      SJ_NEG_NEW_A:df2})
        p = p.astype(int)
        a = pd.DataFrame(
            [['chr2', 2, 25, '-', 'GT/AG', False],
             ['chr2', 3, 25, '-', 'CT/AC', False],
             ['chr2', 5, 20, '-', 'GT/AG', True],
             ['chr2', 5, 30, '-', 'GT/AG', False],
             ['chr2', 10, 20, '-', 'CT/AC', True],
             ['chr2', 30, 40, '-', 'CT/AC', False]],
            index=ind,
            columns=[u'chrom', u'start', u'end', u'strand',
                     u'intron_motif', u'annotated']
        )
        p2, a2 = cpb.star._make_sj_out_panel(d)
        assert_frame_equal(a, a2)
        assert_panel_equal(p, p2)
Exemplo n.º 11
0
    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'])
Exemplo n.º 12
0
    def test_truncate(self):
        dates = self.panel.major_axis
        start, end = dates[1], dates[5]

        trunced = self.panel.truncate(start, end).to_wide()
        expected = self.panel.to_wide()['ItemA'].truncate(start, end)

        assert_frame_equal(trunced['ItemA'], expected)

        trunced = self.panel.truncate(before=start).to_wide()
        expected = self.panel.to_wide()['ItemA'].truncate(before=start)

        assert_frame_equal(trunced['ItemA'], expected)

        trunced = self.panel.truncate(after=end).to_wide()
        expected = self.panel.to_wide()['ItemA'].truncate(after=end)

        assert_frame_equal(trunced['ItemA'], expected)

        # truncate on dates that aren't in there
        wp = self.panel.to_wide()
        new_index = wp.major_axis[::5]

        wp2 = wp.reindex(major=new_index)

        lp2 = wp2.to_long()
        lp_trunc = lp2.truncate(wp.major_axis[2], wp.major_axis[-2])

        wp_trunc = wp2.truncate(wp.major_axis[2], wp.major_axis[-2])

        assert_panel_equal(wp_trunc, lp_trunc.to_wide())

        # throw proper exception
        self.assertRaises(Exception, lp2.truncate, wp.major_axis[-2],
                          wp.major_axis[2])
Exemplo n.º 13
0
    def test_combinePanel(self):
        result = self.panel.add(self.panel)
        assert_panel_equal(result, self.panel * 2)

        long = self.panel.toLong(filter_observations=False)
        result = self.panel.add(long)
        assert_panel_equal(result, self.panel * 2)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
    def test_resample_panel(self):
        rng = date_range("1/1/2000", "6/30/2000")
        n = len(rng)

        panel = Panel(
            np.random.randn(3, n, 5),
            items=["one", "two", "three"],
            major_axis=rng,
            minor_axis=["a", "b", "c", "d", "e"],
        )

        result = panel.resample("M", axis=1)

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample("M"))
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample("M", axis=2)
        expected = p_apply(panel2, lambda x: x.resample("M", axis=1))
        tm.assert_panel_equal(result, expected)
Exemplo n.º 16
0
    def _check_stat_op(self, name, alternative, obj=None, has_skipna=True):
        if obj is None:
            obj = self.panel4d

            # # set some NAs
            # obj.ix[5:10] = np.nan
            # obj.ix[15:20, -2:] = np.nan

        f = getattr(obj, name)

        if has_skipna:
            def skipna_wrapper(x):
                nona = remove_na(x)
                if len(nona) == 0:
                    return np.nan
                return alternative(nona)

            def wrapper(x):
                return alternative(np.asarray(x))

            for i in range(obj.ndim):
                result = f(axis=i, skipna=False)
                assert_panel_equal(result, obj.apply(wrapper, axis=i))
        else:
            skipna_wrapper = alternative
            wrapper = alternative

        for i in range(obj.ndim):
            result = f(axis=i)
            assert_panel_equal(result, obj.apply(skipna_wrapper, axis=i))

        self.assertRaises(Exception, f, axis=obj.ndim)
Exemplo n.º 17
0
    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'])
Exemplo n.º 18
0
    def test_setitem(self):
        ## LongPanel with one item
        # lp = self.panel.filter(['ItemA', 'ItemB']).to_frame()
        # self.assertRaises(Exception, self.panel.__setitem__,
        #                  'ItemE', lp)

        # Panel
        p = Panel(dict(
            ItemA=self.panel4d['l1']['ItemA'][2:].filter(items=['A', 'B'])))
        self.panel4d['l4'] = p
        self.panel4d['l5'] = p

        p2 = self.panel4d['l4']

        assert_panel_equal(p, p2.reindex(items=p.items,
                                         major_axis=p.major_axis,
                                         minor_axis=p.minor_axis))

        # scalar
        self.panel4d['lG'] = 1
        self.panel4d['lE'] = True
        self.assertEqual(self.panel4d['lG'].values.dtype, np.int64)
        self.assertEqual(self.panel4d['lE'].values.dtype, np.bool_)

        # object dtype
        self.panel4d['lQ'] = 'foo'
        self.assertEqual(self.panel4d['lQ'].values.dtype, np.object_)

        # boolean dtype
        self.panel4d['lP'] = self.panel4d['l1'] > 0
        self.assertEqual(self.panel4d['lP'].values.dtype, np.bool_)
Exemplo n.º 19
0
    def test_panel_setitem(self):

        with catch_warnings(record=True):
            # GH 7763
            # loc and setitem have setting differences
            np.random.seed(0)
            index = range(3)
            columns = list('abc')

            panel = Panel({'A': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns),
                           'B': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns),
                           'C': DataFrame(np.random.randn(3, 3),
                                          index=index, columns=columns)})

            replace = DataFrame(np.eye(3, 3), index=range(3), columns=columns)
            expected = Panel({'A': replace, 'B': replace, 'C': replace})

            p = panel.copy()
            for idx in list('ABC'):
                p[idx] = replace
            tm.assert_panel_equal(p, expected)

            p = panel.copy()
            for idx in list('ABC'):
                p.loc[idx, :, :] = replace
            tm.assert_panel_equal(p, expected)
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
 def test_reindex_like(self):
     # reindex_like
     smaller = self.panel.reindex(items=self.panel.items[:-1],
                                  major=self.panel.major_axis[:-1],
                                  minor=self.panel.minor_axis[:-1])
     smaller_like = self.panel.reindex_like(smaller)
     assert_panel_equal(smaller, smaller_like)
Exemplo n.º 22
0
    def test_basics(self, window=10):
        items = ['bar', 'baz', 'foo']
        minor = ['A', 'B', 'C', 'D']

        rp = RollingPanel(window, items, minor, cap_multiple=2)

        dates = pd.date_range('2000-01-01', periods=30, tz='utc')

        major_deque = deque(maxlen=window)

        frames = {}

        for i, date in enumerate(dates):
            frame = pd.DataFrame(np.random.randn(3, 4), index=items,
                                 columns=minor)

            rp.add_frame(date, frame)

            frames[date] = frame
            major_deque.append(date)

            result = rp.get_current()
            expected = pd.Panel(frames, items=list(major_deque),
                                major_axis=items, minor_axis=minor)

            tm.assert_panel_equal(result, expected.swapaxes(0, 1))
Exemplo n.º 23
0
    def test_ctor_dict(self):
        itema = self.panel['ItemA']
        itemb = self.panel['ItemB']

        d = {'A' : itema, 'B' : itemb[5:]}
        d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
        d3 = {'A' : DataFrame(itema._series),
              'B' : DataFrame(itemb[5:]._series)}

        wp = Panel.from_dict(d)
        wp2 = Panel.from_dict(d2) # nested Dict
        wp3 = Panel.from_dict(d3)
        self.assert_(wp.major_axis.equals(self.panel.major_axis))
        assert_panel_equal(wp, wp2)

        # intersect
        wp = Panel.from_dict(d, intersect=True)
        self.assert_(wp.major_axis.equals(itemb.index[5:]))

        # use constructor
        assert_panel_equal(Panel(d), Panel.from_dict(d))
        assert_panel_equal(Panel(d2), Panel.from_dict(d2))
        assert_panel_equal(Panel(d3), Panel.from_dict(d3))

        # cast
        dcasted = dict((k, v.reindex(wp.major_axis).fillna(0))
                       for k, v in d.iteritems())
        result = Panel(dcasted, dtype=int)
        expected = Panel(dict((k, v.astype(int))
                              for k, v in dcasted.iteritems()))
        assert_panel_equal(result, expected)
Exemplo n.º 24
0
 def _dense_comp(op):
     with tm.assert_produces_warning(FutureWarning,
                                     check_stacklevel=False):
         dense = panel.to_dense()
         sparse_result = op(panel)
         dense_result = op(dense)
         assert_panel_equal(sparse_result.to_dense(), dense_result)
Exemplo n.º 25
0
    def test_table_mixed_dtypes(self):

        # frame
        def _make_one_df():
            df = tm.makeDataFrame()
            df['obj1'] = 'foo'
            df['obj2'] = 'bar'
            df['bool1'] = df['A'] > 0
            df['bool2'] = df['B'] > 0
            df['bool3'] = True
            df['int1'] = 1
            df['int2'] = 2
            return df.consolidate()

        df1 = _make_one_df()

        self.store.append('df1_mixed', df1)
        tm.assert_frame_equal(self.store.select('df1_mixed'), df1)

        # panel
        def _make_one_panel():
            wp = tm.makePanel()
            wp['obj1'] = 'foo'
            wp['obj2'] = 'bar'
            wp['bool1'] = wp['ItemA'] > 0
            wp['bool2'] = wp['ItemB'] > 0
            wp['int1'] = 1
            wp['int2'] = 2
            return wp.consolidate()
        p1 = _make_one_panel()

        self.store.append('p1_mixed', p1)
        tm.assert_panel_equal(self.store.select('p1_mixed'), p1)
Exemplo n.º 26
0
    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'])])
Exemplo n.º 27
0
    def test_setitem(self):
        # LongPanel with one item
        # lp = self.panel.filter(['ItemA', 'ItemB']).to_frame()
        # self.assertRaises(Exception, self.panel.__setitem__,
        #                  'ItemE', lp)

        # Panel
        p = Panel(dict(ItemA=self.panel4d["l1"]["ItemA"][2:].filter(items=["A", "B"])))
        self.panel4d["l4"] = p
        self.panel4d["l5"] = p

        p2 = self.panel4d["l4"]

        assert_panel_equal(p, p2.reindex(items=p.items, major_axis=p.major_axis, minor_axis=p.minor_axis))

        # scalar
        self.panel4d["lG"] = 1
        self.panel4d["lE"] = True
        self.assertEqual(self.panel4d["lG"].values.dtype, np.int64)
        self.assertEqual(self.panel4d["lE"].values.dtype, np.bool_)

        # object dtype
        self.panel4d["lQ"] = "foo"
        self.assertEqual(self.panel4d["lQ"].values.dtype, np.object_)

        # boolean dtype
        self.panel4d["lP"] = self.panel4d["l1"] > 0
        self.assertEqual(self.panel4d["lP"].values.dtype, np.bool_)
Exemplo n.º 28
0
    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)
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
    def test_to_long(self):
        # filtered
        filtered = self.panel.to_long()

        # unfiltered
        unfiltered = self.panel.to_long(filter_observations=False)

        assert_panel_equal(unfiltered.to_wide(), self.panel)
Exemplo n.º 31
0
    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)

        with catch_warnings(record=True):
            for p4d in [tm.makePanel4D()]:
                out = p4d.take(indices)
                expected = Panel4D(data=p4d.values.take(indices, axis=0),
                                   labels=p4d.labels.take(indices),
                                   major_axis=p4d.major_axis,
                                   minor_axis=p4d.minor_axis,
                                   items=p4d.items)
                tm.assert_panel4d_equal(out, expected)
Exemplo n.º 32
0
    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)
Exemplo n.º 33
0
    def _check_stat_op(self, name, alternative, obj=None, has_skipna=True):
        if obj is None:
            obj = self.panel4d

            # # set some NAs
            # obj.loc[5:10] = np.nan
            # obj.loc[15:20, -2:] = np.nan

        f = getattr(obj, name)

        if has_skipna:

            def skipna_wrapper(x):
                nona = remove_na(x)
                if len(nona) == 0:
                    return np.nan
                return alternative(nona)

            def wrapper(x):
                return alternative(np.asarray(x))

            with catch_warnings(record=True):
                for i in range(obj.ndim):
                    result = f(axis=i, skipna=False)
                    expected = obj.apply(wrapper, axis=i)
                    tm.assert_panel_equal(result, expected)
        else:
            skipna_wrapper = alternative
            wrapper = alternative

        with catch_warnings(record=True):
            for i in range(obj.ndim):
                result = f(axis=i)
                if not tm._incompat_bottleneck_version(name):
                    expected = obj.apply(skipna_wrapper, axis=i)
                    tm.assert_panel_equal(result, expected)

        pytest.raises(Exception, f, axis=obj.ndim)
Exemplo n.º 34
0
    def test_remove_crit(self):
        wp = tm.makePanel()
        self.store.put('wp', wp, table=True)
        date = wp.major_axis[len(wp.major_axis) // 2]

        crit1 = Term('index', '>', date)
        crit2 = Term('column', ['A', 'D'])
        self.store.remove('wp', where=[crit1])
        self.store.remove('wp', where=[crit2])
        result = self.store['wp']
        expected = wp.truncate(after=date).reindex(minor=['B', 'C'])
        tm.assert_panel_equal(result, expected)

        # test non-consecutive row removal
        wp = tm.makePanel()
        self.store.put('wp2', wp, table=True)

        date1 = wp.major_axis[1:3]
        date2 = wp.major_axis[5]
        date3 = [wp.major_axis[7], wp.major_axis[9]]

        crit1 = Term('index', date1)
        crit2 = Term('index', date2)
        crit3 = Term('index', date3)

        self.store.remove('wp2', where=[crit1])
        self.store.remove('wp2', where=[crit2])
        self.store.remove('wp2', where=[crit3])
        result = self.store['wp2']

        ma = list(wp.major_axis)
        for d in date1:
            ma.remove(d)
        ma.remove(date2)
        for d in date3:
            ma.remove(d)
        expected = wp.reindex(major=ma)
        tm.assert_panel_equal(result, expected)
Exemplo n.º 35
0
    def test_constructor(self):
        # with BlockManager
        wp = Panel(self.panel._data)
        self.assert_(wp._data is self.panel._data)

        wp = Panel(self.panel._data, copy=True)
        self.assert_(wp._data is not self.panel._data)
        assert_panel_equal(wp, self.panel)

        # strings handled prop
        wp = Panel([[['foo', 'foo', 'foo',],
                         ['foo', 'foo', 'foo']]])
        self.assert_(wp.values.dtype == np.object_)

        vals = self.panel.values

        # no copy
        wp = Panel(vals)
        self.assert_(wp.values is vals)

        # copy
        wp = Panel(vals, copy=True)
        self.assert_(wp.values is not vals)
Exemplo n.º 36
0
    def test_to_panel_expanddim(self):
        # GH 9762

        with catch_warnings(record=True):

            class SubclassedFrame(DataFrame):
                @property
                def _constructor_expanddim(self):
                    return SubclassedPanel

            class SubclassedPanel(Panel):
                pass

            index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
            df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
            result = df.to_panel()
            self.assertTrue(isinstance(result, SubclassedPanel))
            expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
                                       items=['X', 'Y'],
                                       major_axis=[0],
                                       minor_axis=[0, 1, 2],
                                       dtype='int64')
            tm.assert_panel_equal(result, expected)
Exemplo n.º 37
0
    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):
            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))

        with catch_warnings(record=True):
            for p4d in [tm.makePanel4D()]:
                tm.assert_panel4d_equal(
                    p4d.transpose(2, 0, 3, 1).transpose(1, 3, 0, 2), p4d)
                tm.assert_raises_regex(TypeError,
                                       msg,
                                       p4d.transpose,
                                       2,
                                       0,
                                       3,
                                       1,
                                       axes=(2, 0, 3, 1))
Exemplo n.º 38
0
    def _check_stat_op(self, name, alternative, obj=None, has_skipna=True,
                       skipna_alternative=None):
        if obj is None:
            obj = self.panel4d

            # # set some NAs
            # obj.loc[5:10] = np.nan
            # obj.loc[15:20, -2:] = np.nan

        f = getattr(obj, name)

        if has_skipna:

            skipna_wrapper = tm._make_skipna_wrapper(alternative,
                                                     skipna_alternative)

            def wrapper(x):
                return alternative(np.asarray(x))

            with catch_warnings(record=True):
                for i in range(obj.ndim):
                    result = f(axis=i, skipna=False)
                    expected = obj.apply(wrapper, axis=i)
                    tm.assert_panel_equal(result, expected)
        else:
            skipna_wrapper = alternative
            wrapper = alternative

        with catch_warnings(record=True):
            for i in range(obj.ndim):
                result = f(axis=i)
                if name in ['sum', 'prod']:
                    expected = obj.apply(skipna_wrapper, axis=i)
                    tm.assert_panel_equal(result, expected)

        pytest.raises(Exception, f, axis=obj.ndim)
Exemplo n.º 39
0
    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'])
Exemplo n.º 40
0
    def test_resample_panel(self):
        rng = date_range('1/1/2000', '6/30/2000')
        n = len(rng)

        panel = Panel(np.random.randn(3, n, 5),
                      items=['one', 'two', 'three'],
                      major_axis=rng,
                      minor_axis=['a', 'b', 'c', 'd', 'e'])

        result = panel.resample('M', axis=1)

        def p_apply(panel, f):
            result = {}
            for item in panel.items:
                result[item] = f(panel[item])
            return Panel(result, items=panel.items)

        expected = p_apply(panel, lambda x: x.resample('M'))
        tm.assert_panel_equal(result, expected)

        panel2 = panel.swapaxes(1, 2)
        result = panel2.resample('M', axis=2)
        expected = p_apply(panel2, lambda x: x.resample('M', axis=1))
        tm.assert_panel_equal(result, expected)
Exemplo n.º 41
0
    def test_transpose(self):
        result = self.panel.transpose('minor', 'major', 'items')
        expected = self.panel.swapaxes('items', 'minor')
        assert_panel_equal(result, expected)

        result = self.panel.transpose(2, 1, 0)
        assert_panel_equal(result, expected)

        result = self.panel.transpose('minor', 'items', 'major')
        expected = self.panel.swapaxes('items', 'minor')
        expected = expected.swapaxes('major', 'minor')
        assert_panel_equal(result, expected)

        result = self.panel.transpose(2, 0, 1)
        assert_panel_equal(result, expected)

        self.assertRaises(ValueError, self.panel.transpose, 0, 0, 1)
Exemplo n.º 42
0
    def test_constructor_resize(self):
        data = self.panel._data
        items = self.panel.items[:-1]
        major = self.panel.major_axis[:-1]
        minor = self.panel.minor_axis[:-1]

        result = Panel(data, items=items, major_axis=major, minor_axis=minor)
        expected = self.panel.reindex(items=items, major=major, minor=minor)
        assert_panel_equal(result, expected)

        result = Panel(data, items=items, major_axis=major)
        expected = self.panel.reindex(items=items, major=major)
        assert_panel_equal(result, expected)

        result = Panel(data, items=items)
        expected = self.panel.reindex(items=items)
        assert_panel_equal(result, expected)

        result = Panel(data, minor_axis=minor)
        expected = self.panel.reindex(minor=minor)
        assert_panel_equal(result, expected)
Exemplo n.º 43
0
class TestPanel(Generic):
    _typ = Panel
    _comparator = lambda self, x, y: assert_panel_equal(x, y, by_blocks=True)

    @td.skip_if_no('xarray', min_version='0.7.0')
    def test_to_xarray(self):
        from xarray import DataArray

        with catch_warnings(record=True):
            p = tm.makePanel()

            result = p.to_xarray()
            assert isinstance(result, DataArray)
            assert len(result.coords) == 3
            assert_almost_equal(list(result.coords.keys()),
                                ['items', 'major_axis', 'minor_axis'])
            assert len(result.dims) == 3

            # idempotency
            assert_panel_equal(result.to_pandas(), p)
Exemplo n.º 44
0
    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
            self.assertRaises(ValueError,
                              panels[0].join,
                              panels[1:],
                              how='outer',
                              lsuffix='foo',
                              rsuffix='bar')
            self.assertRaises(ValueError,
                              panels[0].join,
                              panels[1:],
                              how='right')
Exemplo n.º 45
0
    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)
Exemplo n.º 46
0
    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_panel_getitem(self):

        with catch_warnings(record=True):
            # GH4016, date selection returns a frame when a partial string
            # selection
            ind = date_range(start="2000", freq="D", periods=1000)
            df = DataFrame(np.random.randn(len(ind), 5),
                           index=ind,
                           columns=list('ABCDE'))
            panel = Panel({'frame_' + c: df for c in list('ABC')})

            test2 = panel.loc[:, "2002":"2002-12-31"]
            test1 = panel.loc[:, "2002"]
            tm.assert_panel_equal(test1, test2)

            # GH8710
            # multi-element getting with a list
            panel = tm.makePanel()

            expected = panel.iloc[[0, 1]]

            result = panel.loc[['ItemA', 'ItemB']]
            tm.assert_panel_equal(result, expected)

            result = panel.loc[['ItemA', 'ItemB'], :, :]
            tm.assert_panel_equal(result, expected)

            result = panel[['ItemA', 'ItemB']]
            tm.assert_panel_equal(result, expected)

            result = panel.loc['ItemA':'ItemB']
            tm.assert_panel_equal(result, expected)

            with catch_warnings(record=True):
                result = panel.ix[['ItemA', 'ItemB']]
            tm.assert_panel_equal(result, expected)

            # with an object-like
            # GH 9140
            class TestObject(object):
                def __str__(self):
                    return "TestObject"

            obj = TestObject()

            p = Panel(np.random.randn(1, 5, 4),
                      items=[obj],
                      major_axis=date_range('1/1/2000', periods=5),
                      minor_axis=['A', 'B', 'C', 'D'])

            expected = p.iloc[0]
            result = p[obj]
            tm.assert_frame_equal(result, expected)
Exemplo n.º 48
0
    def test_basic_panel(self):

        for s, i in self.panel.items():
            i_rec = self.encode_decode(i)
            assert_panel_equal(i, i_rec)
Exemplo n.º 49
0
    def test_delitem_and_pop(self):
        expected = self.panel4d['l2']
        result = self.panel4d.pop('l2')
        assert_panel_equal(expected, result)
        self.assertNotIn('l2', self.panel4d.labels)

        del self.panel4d['l3']
        self.assertNotIn('l3', self.panel4d.labels)
        self.assertRaises(Exception, self.panel4d.__delitem__, 'l3')

        values = np.empty((4, 4, 4, 4))
        values[0] = 0
        values[1] = 1
        values[2] = 2
        values[3] = 3

        panel4d = Panel4D(values, lrange(4), lrange(4), lrange(4), lrange(4))

        # did we delete the right row?

        panel4dc = panel4d.copy()
        del panel4dc[0]
        assert_panel_equal(panel4dc[1], panel4d[1])
        assert_panel_equal(panel4dc[2], panel4d[2])
        assert_panel_equal(panel4dc[3], panel4d[3])

        panel4dc = panel4d.copy()
        del panel4dc[1]
        assert_panel_equal(panel4dc[0], panel4d[0])
        assert_panel_equal(panel4dc[2], panel4d[2])
        assert_panel_equal(panel4dc[3], panel4d[3])

        panel4dc = panel4d.copy()
        del panel4dc[2]
        assert_panel_equal(panel4dc[1], panel4d[1])
        assert_panel_equal(panel4dc[0], panel4d[0])
        assert_panel_equal(panel4dc[3], panel4d[3])

        panel4dc = panel4d.copy()
        del panel4dc[3]
        assert_panel_equal(panel4dc[1], panel4d[1])
        assert_panel_equal(panel4dc[2], panel4d[2])
        assert_panel_equal(panel4dc[0], panel4d[0])
Exemplo n.º 50
0
 def _test_op(panel4d, op):
     result = op(panel4d, 1)
     assert_panel_equal(result['l1'], op(panel4d['l1'], 1))
Exemplo n.º 51
0
 def assert_panel_equal(cls, x, y):
     assert_panel_equal(x, y)
Exemplo n.º 52
0
 def test_get_attr(self):
     assert_panel_equal(self.panel4d['l1'], self.panel4d.l1)
Exemplo n.º 53
0
class TestPanel(tm.TestCase, Generic):
    _typ = Panel
    _comparator = lambda self, x, y: assert_panel_equal(x, y)
Exemplo n.º 54
0
    def test_sample(sel):
        # Fixes issue: 2419
        # additional specific object based tests

        # A few dataframe test with degenerate weights.
        easy_weight_list = [0] * 10
        easy_weight_list[5] = 1

        df = pd.DataFrame({
            'col1': range(10, 20),
            'col2': range(20, 30),
            'colString': ['a'] * 10,
            'easyweights': easy_weight_list
        })
        sample1 = df.sample(n=1, weights='easyweights')
        assert_frame_equal(sample1, df.iloc[5:6])

        # Ensure proper error if string given as weight for Series, panel, or
        # DataFrame with axis = 1.
        s = Series(range(10))
        with pytest.raises(ValueError):
            s.sample(n=3, weights='weight_column')

        with catch_warnings(record=True):
            panel = Panel(items=[0, 1, 2],
                          major_axis=[2, 3, 4],
                          minor_axis=[3, 4, 5])
            with pytest.raises(ValueError):
                panel.sample(n=1, weights='weight_column')

        with pytest.raises(ValueError):
            df.sample(n=1, weights='weight_column', axis=1)

        # Check weighting key error
        with pytest.raises(KeyError):
            df.sample(n=3, weights='not_a_real_column_name')

        # Check that re-normalizes weights that don't sum to one.
        weights_less_than_1 = [0] * 10
        weights_less_than_1[0] = 0.5
        tm.assert_frame_equal(df.sample(n=1, weights=weights_less_than_1),
                              df.iloc[:1])

        ###
        # POJO.Test axis argument
        ###

        # POJO.Test axis argument
        df = pd.DataFrame({'col1': range(10), 'col2': ['a'] * 10})
        second_column_weight = [0, 1]
        assert_frame_equal(
            df.sample(n=1, axis=1, weights=second_column_weight), df[['col2']])

        # Different axis arg types
        assert_frame_equal(
            df.sample(n=1, axis='columns', weights=second_column_weight),
            df[['col2']])

        weight = [0] * 10
        weight[5] = 0.5
        assert_frame_equal(df.sample(n=1, axis='rows', weights=weight),
                           df.iloc[5:6])
        assert_frame_equal(df.sample(n=1, axis='index', weights=weight),
                           df.iloc[5:6])

        # Check out of range axis values
        with pytest.raises(ValueError):
            df.sample(n=1, axis=2)

        with pytest.raises(ValueError):
            df.sample(n=1, axis='not_a_name')

        with pytest.raises(ValueError):
            s = pd.Series(range(10))
            s.sample(n=1, axis=1)

        # POJO.Test weight length compared to correct axis
        with pytest.raises(ValueError):
            df.sample(n=1, axis=1, weights=[0.5] * 10)

        # Check weights with axis = 1
        easy_weight_list = [0] * 3
        easy_weight_list[2] = 1

        df = pd.DataFrame({
            'col1': range(10, 20),
            'col2': range(20, 30),
            'colString': ['a'] * 10
        })
        sample1 = df.sample(n=1, axis=1, weights=easy_weight_list)
        assert_frame_equal(sample1, df[['colString']])

        # POJO.Test default axes
        with catch_warnings(record=True):
            p = Panel(items=['a', 'b', 'c'],
                      major_axis=[2, 4, 6],
                      minor_axis=[1, 3, 5])
            assert_panel_equal(p.sample(n=3, random_state=42),
                               p.sample(n=3, axis=1, random_state=42))
            assert_frame_equal(df.sample(n=3, random_state=42),
                               df.sample(n=3, axis=0, random_state=42))

        # POJO.Test that function aligns weights with frame
        df = DataFrame({
            'col1': [5, 6, 7],
            'col2': ['a', 'b', 'c'],
        },
                       index=[9, 5, 3])
        s = Series([1, 0, 0], index=[3, 5, 9])
        assert_frame_equal(df.loc[[3]], df.sample(1, weights=s))

        # Weights have index values to be dropped because not in
        # sampled DataFrame
        s2 = Series([0.001, 0, 10000], index=[3, 5, 10])
        assert_frame_equal(df.loc[[3]], df.sample(1, weights=s2))

        # Weights have empty values to be filed with zeros
        s3 = Series([0.01, 0], index=[3, 5])
        assert_frame_equal(df.loc[[3]], df.sample(1, weights=s3))

        # No overlap in weight and sampled DataFrame indices
        s4 = Series([1, 0], index=[1, 2])
        with pytest.raises(ValueError):
            df.sample(1, weights=s4)
Exemplo n.º 55
0
 def _check(left, right):
     tm.assert_panel_equal(left.to_panel(), right.to_panel())
Exemplo n.º 56
0
    def test_getitem_fancy_labels(self):
        p = self.panel

        items = p.items[[1, 0]]
        dates = p.major_axis[::2]
        cols = ['D', 'C', 'F']

        # all 3 specified
        assert_panel_equal(p.ix[items, dates, cols],
                           p.reindex(items=items, major=dates, minor=cols))

        # 2 specified
        assert_panel_equal(p.ix[:, dates, cols],
                           p.reindex(major=dates, minor=cols))

        assert_panel_equal(p.ix[items, :, cols],
                           p.reindex(items=items, minor=cols))

        assert_panel_equal(p.ix[items, dates, :],
                           p.reindex(items=items, major=dates))

        # only 1
        assert_panel_equal(p.ix[items, :, :], p.reindex(items=items))

        assert_panel_equal(p.ix[:, dates, :], p.reindex(major=dates))

        assert_panel_equal(p.ix[:, :, cols], p.reindex(minor=cols))
Exemplo n.º 57
0
        def _eq(t, o, a, obj, k1, k2):
            """ compare equal for these 2 keys """

            if a is not None and a > obj.ndim - 1:
                return

            def _print(result, error=None):
                if error is not None:
                    error = str(error)
                v = ("%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
                     "key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s" %
                     (name, result, t, o, method1, method2, a, error or ''))
                if _verbose:
                    pprint_thing(v)

            try:
                rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))

                try:
                    xp = self.get_result(obj, method2, k2, a)
                except:
                    result = 'no comp'
                    _print(result)
                    return

                detail = None

                try:
                    if is_scalar(rs) and is_scalar(xp):
                        assert rs == xp
                    elif xp.ndim == 1:
                        tm.assert_series_equal(rs, xp)
                    elif xp.ndim == 2:
                        tm.assert_frame_equal(rs, xp)
                    elif xp.ndim == 3:
                        tm.assert_panel_equal(rs, xp)
                    result = 'ok'
                except AssertionError as e:
                    detail = str(e)
                    result = 'fail'

                # reverse the checks
                if fails is True:
                    if result == 'fail':
                        result = 'ok (fail)'

                _print(result)
                if not result.startswith('ok'):
                    raise AssertionError(detail)

            except AssertionError:
                raise
            except Exception as detail:

                # if we are in fails, the ok, otherwise raise it
                if fails is not None:
                    if isinstance(detail, fails):
                        result = 'ok (%s)' % type(detail).__name__
                        _print(result)
                        return

                result = type(detail).__name__
                raise AssertionError(_print(result, error=detail))
    def test_iloc_getitem_panel(self):

        with catch_warnings(record=True):
            # GH 7189
            p = Panel(np.arange(4 * 3 * 2).reshape(4, 3, 2),
                      items=['A', 'B', 'C', 'D'],
                      major_axis=['a', 'b', 'c'],
                      minor_axis=['one', 'two'])

            result = p.iloc[1]
            expected = p.loc['B']
            tm.assert_frame_equal(result, expected)

            result = p.iloc[1, 1]
            expected = p.loc['B', 'b']
            tm.assert_series_equal(result, expected)

            result = p.iloc[1, 1, 1]
            expected = p.loc['B', 'b', 'two']
            assert result == expected

            # slice
            result = p.iloc[1:3]
            expected = p.loc[['B', 'C']]
            tm.assert_panel_equal(result, expected)

            result = p.iloc[:, 0:2]
            expected = p.loc[:, ['a', 'b']]
            tm.assert_panel_equal(result, expected)

            # list of integers
            result = p.iloc[[0, 2]]
            expected = p.loc[['A', 'C']]
            tm.assert_panel_equal(result, expected)

            # neg indices
            result = p.iloc[[-1, 1], [-1, 1]]
            expected = p.loc[['D', 'B'], ['c', 'b']]
            tm.assert_panel_equal(result, expected)

            # dups indices
            result = p.iloc[[-1, -1, 1], [-1, 1]]
            expected = p.loc[['D', 'D', 'B'], ['c', 'b']]
            tm.assert_panel_equal(result, expected)

            # combined
            result = p.iloc[0, [True, True], [0, 1]]
            expected = p.loc['A', ['a', 'b'], ['one', 'two']]
            tm.assert_frame_equal(result, expected)

            # out-of-bounds exception
            with pytest.raises(IndexError):
                p.iloc[tuple([10, 5])]

            with pytest.raises(IndexError):
                p.iloc[0, [True, True], [0, 1, 2]]

            # trying to use a label
            with pytest.raises(ValueError):
                p.iloc[tuple(['j', 'D'])]

            # GH
            p = Panel(np.random.rand(4, 3, 2),
                      items=['A', 'B', 'C', 'D'],
                      major_axis=['U', 'V', 'W'],
                      minor_axis=['X', 'Y'])
            expected = p['A']

            result = p.iloc[0, :, :]
            tm.assert_frame_equal(result, expected)

            result = p.iloc[0, [True, True, True], :]
            tm.assert_frame_equal(result, expected)

            result = p.iloc[0, [True, True, True], [0, 1]]
            tm.assert_frame_equal(result, expected)

            with pytest.raises(IndexError):
                p.iloc[0, [True, True, True], [0, 1, 2]]

            with pytest.raises(IndexError):
                p.iloc[0, [True, True, True], [2]]
Exemplo n.º 59
0
    def test_delitem_and_pop(self):

        with catch_warnings(record=True):
            expected = self.panel4d['l2']
            result = self.panel4d.pop('l2')
            tm.assert_panel_equal(expected, result)
            assert 'l2' not in self.panel4d.labels

            del self.panel4d['l3']
            assert 'l3' not in self.panel4d.labels
            pytest.raises(Exception, self.panel4d.__delitem__, 'l3')

            values = np.empty((4, 4, 4, 4))
            values[0] = 0
            values[1] = 1
            values[2] = 2
            values[3] = 3

            panel4d = Panel4D(values, lrange(4), lrange(4), lrange(4),
                              lrange(4))

            # did we delete the right row?
            panel4dc = panel4d.copy()
            del panel4dc[0]
            tm.assert_panel_equal(panel4dc[1], panel4d[1])
            tm.assert_panel_equal(panel4dc[2], panel4d[2])
            tm.assert_panel_equal(panel4dc[3], panel4d[3])

            panel4dc = panel4d.copy()
            del panel4dc[1]
            tm.assert_panel_equal(panel4dc[0], panel4d[0])
            tm.assert_panel_equal(panel4dc[2], panel4d[2])
            tm.assert_panel_equal(panel4dc[3], panel4d[3])

            panel4dc = panel4d.copy()
            del panel4dc[2]
            tm.assert_panel_equal(panel4dc[1], panel4d[1])
            tm.assert_panel_equal(panel4dc[0], panel4d[0])
            tm.assert_panel_equal(panel4dc[3], panel4d[3])

            panel4dc = panel4d.copy()
            del panel4dc[3]
            tm.assert_panel_equal(panel4dc[1], panel4d[1])
            tm.assert_panel_equal(panel4dc[2], panel4d[2])
            tm.assert_panel_equal(panel4dc[0], panel4d[0])
Exemplo n.º 60
0
    def test_partial_setting(self):

        # GH2578, allow ix and friends to partially set

        # series
        s_orig = Series([1, 2, 3])

        s = s_orig.copy()
        s[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5
        expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        s = s_orig.copy()
        s.loc[5] = 5.
        expected = Series([1, 2, 3, 5.], index=[0, 1, 2, 5])
        tm.assert_series_equal(s, expected)

        # iloc/iat raise
        s = s_orig.copy()

        def f():
            s.iloc[3] = 5.

        pytest.raises(IndexError, f)

        def f():
            s.iat[3] = 5.

        pytest.raises(IndexError, f)

        # ## frame ##

        df_orig = DataFrame(np.arange(6).reshape(3, 2),
                            columns=['A', 'B'],
                            dtype='int64')

        # iloc/iat raise
        df = df_orig.copy()

        def f():
            df.iloc[4, 2] = 5.

        pytest.raises(IndexError, f)

        def f():
            df.iat[4, 2] = 5.

        pytest.raises(IndexError, f)

        # row setting where it exists
        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.iloc[1] = df.iloc[2]
        tm.assert_frame_equal(df, expected)

        expected = DataFrame(dict({'A': [0, 4, 4], 'B': [1, 5, 5]}))
        df = df_orig.copy()
        df.loc[1] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # like 2578, partial setting with dtype preservation
        expected = DataFrame(dict({'A': [0, 2, 4, 4], 'B': [1, 3, 5, 5]}))
        df = df_orig.copy()
        df.loc[3] = df.loc[2]
        tm.assert_frame_equal(df, expected)

        # single dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': [0, 2, 4]}))
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed dtype frame, overwrite
        expected = DataFrame(dict({'A': [0, 2, 4], 'B': Series([0, 2, 4])}))
        df = df_orig.copy()
        df['B'] = df['B'].astype(np.float64)
        with catch_warnings(record=True):
            df.ix[:, 'B'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # single dtype frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        # mixed frame, partial setting
        expected = df_orig.copy()
        expected['C'] = df['A']
        df = df_orig.copy()
        with catch_warnings(record=True):
            df.ix[:, 'C'] = df.ix[:, 'A']
        tm.assert_frame_equal(df, expected)

        with catch_warnings(record=True):
            # ## panel ##
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'],
                           dtype='float64')

            # panel setting via item
            p_orig = Panel(np.arange(16).reshape(2, 4, 2),
                           items=['Item1', 'Item2'],
                           major_axis=pd.date_range('2001/1/12', periods=4),
                           minor_axis=['A', 'B'],
                           dtype='float64')
            expected = p_orig.copy()
            expected['Item3'] = expected['Item1']
            p = p_orig.copy()
            p.loc['Item3'] = p['Item1']
            tm.assert_panel_equal(p, expected)

            # panel with aligned series
            expected = p_orig.copy()
            expected = expected.transpose(2, 1, 0)
            expected['C'] = DataFrame(
                {
                    'Item1': [30, 30, 30, 30],
                    'Item2': [32, 32, 32, 32]
                },
                index=p_orig.major_axis)
            expected = expected.transpose(2, 1, 0)
            p = p_orig.copy()
            p.loc[:, :, 'C'] = Series([30, 32], index=p_orig.items)
            tm.assert_panel_equal(p, expected)

        # GH 8473
        dates = date_range('1/1/2000', periods=8)
        df_orig = DataFrame(np.random.randn(8, 4),
                            index=dates,
                            columns=['A', 'B', 'C', 'D'])

        expected = pd.concat(
            [df_orig, DataFrame({'A': 7}, index=[dates[-1] + 1])])
        df = df_orig.copy()
        df.loc[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 'A'] = 7
        tm.assert_frame_equal(df, expected)

        exp_other = DataFrame({0: 7}, index=[dates[-1] + 1])
        expected = pd.concat([df_orig, exp_other], axis=1)

        df = df_orig.copy()
        df.loc[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)
        df = df_orig.copy()
        df.at[dates[-1] + 1, 0] = 7
        tm.assert_frame_equal(df, expected)