当前位置: 首页>>代码示例>>Python>>正文


Python SparseDataFrame.take方法代码示例

本文整理汇总了Python中pandas.sparse.api.SparseDataFrame.take方法的典型用法代码示例。如果您正苦于以下问题:Python SparseDataFrame.take方法的具体用法?Python SparseDataFrame.take怎么用?Python SparseDataFrame.take使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas.sparse.api.SparseDataFrame的用法示例。


在下文中一共展示了SparseDataFrame.take方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestSparseDataFrame

# 需要导入模块: from pandas.sparse.api import SparseDataFrame [as 别名]
# 或者: from pandas.sparse.api.SparseDataFrame import take [as 别名]

#.........这里部分代码省略.........
            self.assertEqual(len(length_n), len(frame))
            self.assertEqual(len(length_n.columns), len(frame.columns))
            self.assertEqual(len(length_n['A']), len(frame))

            # reindex columns
            reindexed = frame.reindex(columns=['A', 'B', 'Z'])
            self.assertEqual(len(reindexed.columns), 3)
            tm.assert_almost_equal(reindexed['Z'].fill_value,
                                   frame.default_fill_value)
            self.assertTrue(np.isnan(reindexed['Z'].sp_values).all())

        _check_frame(self.frame)
        _check_frame(self.iframe)
        _check_frame(self.zframe)
        _check_frame(self.fill_frame)

        # with copy=False
        reindexed = self.frame.reindex(self.frame.index, copy=False)
        reindexed['F'] = reindexed['A']
        self.assertIn('F', self.frame)

        reindexed = self.frame.reindex(self.frame.index)
        reindexed['G'] = reindexed['A']
        self.assertNotIn('G', self.frame)

    def test_reindex_fill_value(self):
        rng = bdate_range('20110110', periods=20)

        result = self.zframe.reindex(rng, fill_value=0)
        exp = self.zorig.reindex(rng, fill_value=0)
        exp = exp.to_sparse(self.zframe.default_fill_value)
        tm.assert_sp_frame_equal(result, exp)

    def test_take(self):
        result = self.frame.take([1, 0, 2], axis=1)
        expected = self.frame.reindex(columns=['B', 'A', 'C'])
        tm.assert_sp_frame_equal(result, expected)

    def test_to_dense(self):
        def _check(frame, orig):
            dense_dm = frame.to_dense()
            tm.assert_frame_equal(frame, dense_dm)
            tm.assert_frame_equal(dense_dm, orig, check_dtype=False)

        self._check_all(_check)

    def test_stack_sparse_frame(self):
        def _check(frame):
            dense_frame = frame.to_dense()  # noqa

            wp = Panel.from_dict({'foo': frame})
            from_dense_lp = wp.to_frame()

            from_sparse_lp = spf.stack_sparse_frame(frame)

            self.assert_numpy_array_equal(from_dense_lp.values,
                                          from_sparse_lp.values)

        _check(self.frame)
        _check(self.iframe)

        # for now
        self.assertRaises(Exception, _check, self.zframe)
        self.assertRaises(Exception, _check, self.fill_frame)

    def test_transpose(self):
开发者ID:aechase,项目名称:pandas,代码行数:70,代码来源:test_frame.py

示例2: TestSparseDataFrame

# 需要导入模块: from pandas.sparse.api import SparseDataFrame [as 别名]
# 或者: from pandas.sparse.api.SparseDataFrame import take [as 别名]

#.........这里部分代码省略.........

            # length zero
            length_zero = frame.reindex([])
            self.assertEquals(len(length_zero), 0)
            self.assertEquals(len(length_zero.columns), len(frame.columns))
            self.assertEquals(len(length_zero["A"]), 0)

            # frame being reindexed has length zero
            length_n = length_zero.reindex(index)
            self.assertEquals(len(length_n), len(frame))
            self.assertEquals(len(length_n.columns), len(frame.columns))
            self.assertEquals(len(length_n["A"]), len(frame))

            # reindex columns
            reindexed = frame.reindex(columns=["A", "B", "Z"])
            self.assertEquals(len(reindexed.columns), 3)
            assert_almost_equal(reindexed["Z"].fill_value, frame.default_fill_value)
            self.assert_(np.isnan(reindexed["Z"].sp_values).all())

        _check_frame(self.frame)
        _check_frame(self.iframe)
        _check_frame(self.zframe)
        _check_frame(self.fill_frame)

        # with copy=False
        reindexed = self.frame.reindex(self.frame.index, copy=False)
        reindexed["F"] = reindexed["A"]
        self.assert_("F" in self.frame)

        reindexed = self.frame.reindex(self.frame.index)
        reindexed["G"] = reindexed["A"]
        self.assert_("G" not in self.frame)

    def test_take(self):
        result = self.frame.take([1, 0, 2], axis=1)
        expected = self.frame.reindex(columns=["B", "A", "C"])
        assert_sp_frame_equal(result, expected)

    def test_density(self):
        df = SparseDataFrame(
            {
                "A": [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6],
                "B": [0, 1, 2, nan, nan, nan, 3, 4, 5, 6],
                "C": np.arange(10),
                "D": [0, 1, 2, 3, 4, 5, nan, nan, nan, nan],
            }
        )

        self.assertEquals(df.density, 0.75)

    def test_to_dense(self):
        def _check(frame):
            dense_dm = frame.to_dense()
            assert_frame_equal(frame, dense_dm)

        self._check_all(_check)

    def test_stack_sparse_frame(self):
        def _check(frame):
            dense_frame = frame.to_dense()

            wp = Panel.from_dict({"foo": frame})
            from_dense_lp = wp.to_frame()

            from_sparse_lp = spf.stack_sparse_frame(frame)
开发者ID:klausz,项目名称:pandas,代码行数:69,代码来源:test_sparse.py


注:本文中的pandas.sparse.api.SparseDataFrame.take方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。