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


Python SparseSeries.cumsum方法代码示例

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


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

示例1: TestSparseSeriesAnalytics

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import cumsum [as 别名]
class TestSparseSeriesAnalytics(tm.TestCase):

    def setUp(self):
        arr, index = _test_data1()
        self.bseries = SparseSeries(arr, index=index, kind='block',
                                    name='bseries')

        arr, index = _test_data1_zero()
        self.zbseries = SparseSeries(arr, index=index, kind='block',
                                     fill_value=0, name='zbseries')

    def test_cumsum(self):
        result = self.bseries.cumsum()
        expected = SparseSeries(self.bseries.to_dense().cumsum())
        tm.assert_sp_series_equal(result, expected)

        # TODO: gh-12855 - return a SparseSeries here
        result = self.zbseries.cumsum()
        expected = self.zbseries.to_dense().cumsum()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)

    def test_numpy_cumsum(self):
        result = np.cumsum(self.bseries)
        expected = SparseSeries(self.bseries.to_dense().cumsum())
        tm.assert_sp_series_equal(result, expected)

        # TODO: gh-12855 - return a SparseSeries here
        result = np.cumsum(self.zbseries)
        expected = self.zbseries.to_dense().cumsum()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)

        msg = "the 'dtype' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
                              self.bseries, dtype=np.int64)

        msg = "the 'out' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
                              self.zbseries, out=result)

    def test_numpy_func_call(self):
        # no exception should be raised even though
        # numpy passes in 'axis=None' or `axis=-1'
        funcs = ['sum', 'cumsum', 'var', 'mean',
                 'prod', 'cumprod', 'std', 'argsort',
                 'argmin', 'argmax', 'min', 'max']
        for func in funcs:
            for series in ('bseries', 'zbseries'):
                getattr(np, func)(getattr(self, series))
开发者ID:sunghopark1,项目名称:pandas,代码行数:52,代码来源:test_series.py

示例2: TestSparseSeriesAnalytics

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import cumsum [as 别名]
class TestSparseSeriesAnalytics(tm.TestCase):
    def setUp(self):
        arr, index = _test_data1()
        self.bseries = SparseSeries(arr, index=index, kind='block',
                                    name='bseries')

        arr, index = _test_data1_zero()
        self.zbseries = SparseSeries(arr, index=index, kind='block',
                                     fill_value=0, name='zbseries')

    def test_cumsum(self):
        result = self.bseries.cumsum()
        expected = SparseSeries(self.bseries.to_dense().cumsum())
        tm.assert_sp_series_equal(result, expected)

        # TODO: gh-12855 - return a SparseSeries here
        result = self.zbseries.cumsum()
        expected = self.zbseries.to_dense().cumsum()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)

    def test_numpy_cumsum(self):
        result = np.cumsum(self.bseries)
        expected = SparseSeries(self.bseries.to_dense().cumsum())
        tm.assert_sp_series_equal(result, expected)

        # TODO: gh-12855 - return a SparseSeries here
        result = np.cumsum(self.zbseries)
        expected = self.zbseries.to_dense().cumsum()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)

        msg = "the 'dtype' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
                              self.bseries, dtype=np.int64)

        msg = "the 'out' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
                              self.zbseries, out=result)
开发者ID:Casyfill,项目名称:Capstone_dashboard,代码行数:41,代码来源:test_series.py

示例3: TestSparseSeries

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

#.........这里部分代码省略.........
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=datetools.bday)
        _dense_series_compare(series, f)

    def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0))

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0))

    def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

    def test_shift_dtype_fill_value(self):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.int64)
        sparse = orig.to_sparse(fill_value=0)

        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0))

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0))

    def test_cumsum(self):
        result = self.bseries.cumsum()
        expected = self.bseries.to_dense().cumsum()
        tm.assertIsInstance(result, SparseSeries)
        self.assertEqual(result.name, self.bseries.name)
        tm.assert_series_equal(result.to_dense(), expected)

        result = self.zbseries.cumsum()
        expected = self.zbseries.to_dense().cumsum()
        tm.assertIsInstance(result, Series)
        tm.assert_series_equal(result, expected)

    def test_combine_first(self):
        s = self.bseries

        result = s[::2].combine_first(s)
        result2 = s[::2].combine_first(s.to_dense())

        expected = s[::2].to_dense().combine_first(s.to_dense())
        expected = expected.to_sparse(fill_value=s.fill_value)

        tm.assert_sp_series_equal(result, result2)
        tm.assert_sp_series_equal(result, expected)
开发者ID:jcfr,项目名称:pandas,代码行数:104,代码来源:test_series.py

示例4: TestSparseSeries

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

#.........这里部分代码省略.........
        _compare_all(self.bseries)

        _compare_all(self.zbseries)
        self.zbseries.sp_values[5:10] = np.NaN
        _compare_all(self.zbseries)

        series = self.zbseries.copy()
        series.fill_value = 2
        _compare_all(series)

    def test_dropna(self):
        sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)

        sp_valid = sp.valid()
        assert_almost_equal(sp_valid.values, sp.to_dense().valid().values)
        self.assert_(sp_valid.index.equals(sp.to_dense().valid().index))
        self.assertEquals(len(sp_valid.sp_values), 2)

        result = self.bseries.dropna()
        expected = self.bseries.to_dense().dropna()
        self.assert_(not isinstance(result, SparseSeries))
        tm.assert_series_equal(result, expected)

    def test_homogenize(self):
        def _check_matches(indices, expected):
            data = {}
            for i, idx in enumerate(indices):
                data[i] = SparseSeries(idx.to_int_index().indices, sparse_index=idx)
            homogenized = spf.homogenize(data)

            for k, v in homogenized.iteritems():
                assert v.sp_index.equals(expected)

        indices1 = [BlockIndex(10, [2], [7]), BlockIndex(10, [1, 6], [3, 4]), BlockIndex(10, [0], [10])]
        expected1 = BlockIndex(10, [2, 6], [2, 3])
        _check_matches(indices1, expected1)

        indices2 = [BlockIndex(10, [2], [7]), BlockIndex(10, [2], [7])]
        expected2 = indices2[0]
        _check_matches(indices2, expected2)

        # must have NaN fill value
        data = {"a": SparseSeries(np.arange(7), sparse_index=expected2, fill_value=0)}
        nose.tools.assert_raises(Exception, spf.homogenize, data)

    def test_fill_value_corner(self):
        cop = self.zbseries.copy()
        cop.fill_value = 0
        result = self.bseries / cop

        self.assert_(np.isnan(result.fill_value))

        cop2 = self.zbseries.copy()
        cop2.fill_value = 1
        result = cop2 / cop
        self.assert_(np.isnan(result.fill_value))

    def test_shift(self):
        series = SparseSeries([nan, 1.0, 2.0, 3.0, nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        self.assert_(shifted is not series)
        assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1.0, 2.0, 3.0, nan, nan], index=DateRange("1/1/2000", periods=6))
        f = lambda s: s.shift(2, timeRule="WEEKDAY")
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, offset=datetools.bday)
        _dense_series_compare(series, f)

    def test_cumsum(self):
        result = self.bseries.cumsum()
        expected = self.bseries.to_dense().cumsum()
        self.assert_(isinstance(result, SparseSeries))
        self.assertEquals(result.name, self.bseries.name)
        assert_series_equal(result.to_dense(), expected)

        result = self.zbseries.cumsum()
        expected = self.zbseries.to_dense().cumsum()
        self.assert_(isinstance(result, Series))
        assert_series_equal(result, expected)

    def test_combine_first(self):
        s = self.bseries

        result = s[::2].combine_first(s)
        result2 = s[::2].combine_first(s.to_dense())

        expected = s[::2].to_dense().combine_first(s.to_dense())
        expected = expected.to_sparse(fill_value=s.fill_value)

        assert_sp_series_equal(result, result2)
        assert_sp_series_equal(result, expected)
开发者ID:klausz,项目名称:pandas,代码行数:104,代码来源:test_sparse.py


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