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


Python SparseDataFrame.apply方法代码示例

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


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

示例1: TestSparseDataFrame

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

#.........这里部分代码省略.........
        assert 'B' not in self.frame
        tm.assert_sp_series_equal(self.frame['A'], A)
        tm.assert_sp_series_equal(self.frame['C'], C)

        del self.frame['D']
        assert 'D' not in self.frame

        del self.frame['A']
        assert 'A' not in self.frame

    def test_set_columns(self):
        self.frame.columns = self.frame.columns
        pytest.raises(Exception, setattr, self.frame, 'columns',
                      self.frame.columns[:-1])

    def test_set_index(self):
        self.frame.index = self.frame.index
        pytest.raises(Exception, setattr, self.frame, 'index',
                      self.frame.index[:-1])

    def test_append(self):
        a = self.frame[:5]
        b = self.frame[5:]

        appended = a.append(b)
        tm.assert_sp_frame_equal(appended, self.frame, exact_indices=False)

        a = self.frame.iloc[:5, :3]
        b = self.frame.iloc[5:]
        appended = a.append(b)
        tm.assert_sp_frame_equal(appended.iloc[:, :3], self.frame.iloc[:, :3],
                                 exact_indices=False)

    def test_apply(self):
        applied = self.frame.apply(np.sqrt)
        assert isinstance(applied, SparseDataFrame)
        tm.assert_almost_equal(applied.values, np.sqrt(self.frame.values))

        applied = self.fill_frame.apply(np.sqrt)
        assert applied['A'].fill_value == np.sqrt(2)

        # agg / broadcast
        broadcasted = self.frame.apply(np.sum, broadcast=True)
        assert isinstance(broadcasted, SparseDataFrame)

        exp = self.frame.to_dense().apply(np.sum, broadcast=True)
        tm.assert_frame_equal(broadcasted.to_dense(), exp)

        assert self.empty.apply(np.sqrt) is self.empty

        from pandas.core import nanops
        applied = self.frame.apply(np.sum)
        tm.assert_series_equal(applied,
                               self.frame.to_dense().apply(nanops.nansum))

    def test_apply_nonuq(self):
        orig = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                         index=['a', 'a', 'c'])
        sparse = orig.to_sparse()
        res = sparse.apply(lambda s: s[0], axis=1)
        exp = orig.apply(lambda s: s[0], axis=1)
        # dtype must be kept
        assert res.dtype == np.int64
        # ToDo: apply must return subclassed dtype
        assert isinstance(res, pd.Series)
        tm.assert_series_equal(res.to_dense(), exp)
开发者ID:jess010,项目名称:pandas,代码行数:70,代码来源:test_frame.py


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