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


Python SparseDataFrame.set_value方法代码示例

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


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

示例1: TestSparseDataFrame

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

#.........这里部分代码省略.........
        tm.assertIsInstance(foo.index, DatetimeIndex)
        tm.assert_frame_equal(foo, self.frame * np.nan)

        foo = self.empty + self.frame
        tm.assert_frame_equal(foo, self.frame * np.nan)

    def test_scalar_ops(self):
        pass

    def test_getitem(self):
        # 1585 select multiple columns
        sdf = SparseDataFrame(index=[0, 1, 2], columns=['a', 'b', 'c'])

        result = sdf[['a', 'b']]
        exp = sdf.reindex(columns=['a', 'b'])
        tm.assert_sp_frame_equal(result, exp)

        self.assertRaises(Exception, sdf.__getitem__, ['a', 'd'])

    def test_icol(self):
        # 10711 deprecated

        # 2227
        result = self.frame.iloc[:, 0]
        self.assertTrue(isinstance(result, SparseSeries))
        tm.assert_sp_series_equal(result, self.frame['A'])

        # preserve sparse index type. #2251
        data = {'A': [0, 1]}
        iframe = SparseDataFrame(data, default_kind='integer')
        self.assertEqual(type(iframe['A'].sp_index),
                         type(iframe.iloc[:, 0].sp_index))

    def test_set_value(self):

        # ok as the index gets conver to object
        frame = self.frame.copy()
        res = frame.set_value('foobar', 'B', 1.5)
        self.assertEqual(res.index.dtype, 'object')

        res = self.frame
        res.index = res.index.astype(object)

        res = self.frame.set_value('foobar', 'B', 1.5)
        self.assertIsNot(res, self.frame)
        self.assertEqual(res.index[-1], 'foobar')
        self.assertEqual(res.get_value('foobar', 'B'), 1.5)

        res2 = res.set_value('foobar', 'qux', 1.5)
        self.assertIsNot(res2, res)
        self.assert_index_equal(res2.columns,
                                pd.Index(list(self.frame.columns) + ['qux']))
        self.assertEqual(res2.get_value('foobar', 'qux'), 1.5)

    def test_fancy_index_misc(self):
        # axis = 0
        sliced = self.frame.ix[-2:, :]
        expected = self.frame.reindex(index=self.frame.index[-2:])
        tm.assert_sp_frame_equal(sliced, expected)

        # axis = 1
        sliced = self.frame.ix[:, -2:]
        expected = self.frame.reindex(columns=self.frame.columns[-2:])
        tm.assert_sp_frame_equal(sliced, expected)

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

示例2: TestSparseDataFrame

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

#.........这里部分代码省略.........
        for op in ops:
            _compare_to_dense(frame, frame[::2], frame.to_dense(), frame[::2].to_dense(), op)
            for s in series:
                _compare_to_dense(frame, s, frame.to_dense(), s.to_dense(), op)
                _compare_to_dense(s, frame, s.to_dense(), frame.to_dense(), op)

        # cross-sectional operations
        series = [frame.xs(fidx[0]), frame.xs(fidx[3]), frame.xs(fidx[5]), frame.xs(fidx[7]), frame.xs(fidx[5])[:2]]

        for op in ops:
            for s in series:
                _compare_to_dense(frame, s, frame.to_dense(), s, op)
                _compare_to_dense(s, frame, s, frame.to_dense(), op)

        # it works!
        result = self.frame + self.frame.ix[:, ["A", "B"]]

    def test_op_corners(self):
        empty = self.empty + self.empty
        self.assert_(not empty)

        foo = self.frame + self.empty
        assert_frame_equal(foo, self.frame * np.nan)

        foo = self.empty + self.frame
        assert_frame_equal(foo, self.frame * np.nan)

    def test_scalar_ops(self):
        pass

    def test_getitem(self):
        pass

    def test_set_value(self):
        res = self.frame.set_value("foobar", "B", 1.5)
        self.assert_(res is not self.frame)
        self.assert_(res.index[-1] == "foobar")
        self.assertEqual(res.get_value("foobar", "B"), 1.5)

        res2 = res.set_value("foobar", "qux", 1.5)
        self.assert_(res2 is not res)
        self.assert_(np.array_equal(res2.columns, list(self.frame.columns) + ["qux"]))
        self.assertEqual(res2.get_value("foobar", "qux"), 1.5)

    def test_fancy_index_misc(self):
        # axis = 0
        sliced = self.frame.ix[-2:, :]
        expected = self.frame.reindex(index=self.frame.index[-2:])
        assert_sp_frame_equal(sliced, expected)

        # axis = 1
        sliced = self.frame.ix[:, -2:]
        expected = self.frame.reindex(columns=self.frame.columns[-2:])
        assert_sp_frame_equal(sliced, expected)

    def test_getitem_overload(self):
        # slicing
        sl = self.frame[:20]
        assert_sp_frame_equal(sl, self.frame.reindex(self.frame.index[:20]))

        # boolean indexing
        d = self.frame.index[5]
        indexer = self.frame.index > d

        subindex = self.frame.index[indexer]
        subframe = self.frame[indexer]
开发者ID:klausz,项目名称:pandas,代码行数:70,代码来源:test_sparse.py


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