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


Python SparseSeries.to_dense方法代码示例

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


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

示例1: test_valid

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
    def test_valid(self):
        sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)

        sp_valid = sp.valid()
        assert_almost_equal(sp_valid, sp.to_dense().valid())
        self.assert_(sp_valid.index.equals(sp.to_dense().valid().index))
        self.assertEquals(len(sp_valid.sp_values), 2)
开发者ID:ashokez,项目名称:pandas,代码行数:9,代码来源:test_sparse.py

示例2: test_dropna

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
    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)
开发者ID:klausz,项目名称:pandas,代码行数:14,代码来源:test_sparse.py

示例3: TestSparseSeriesAnalytics

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [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

示例4: TestSparseSeriesAnalytics

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [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

示例5: test_numpy_take

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
    def test_numpy_take(self):
        sp = SparseSeries([1.0, 2.0, 3.0])
        indices = [1, 2]

        tm.assert_series_equal(np.take(sp, indices, axis=0).to_dense(),
                               np.take(sp.to_dense(), indices, axis=0))

        msg = "the 'out' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.take,
                              sp, indices, out=np.empty(sp.shape))

        msg = "the 'mode' parameter is not supported"
        tm.assertRaisesRegexp(ValueError, msg, np.take,
                              sp, indices, mode='clip')
开发者ID:sunghopark1,项目名称:pandas,代码行数:16,代码来源:test_series.py

示例6: test_dropna

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
    def test_dropna(self):
        sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)

        sp_valid = sp.valid()

        expected = sp.to_dense().valid()
        expected = expected[expected != 0]

        tm.assert_almost_equal(sp_valid.values, expected.values)
        self.assertTrue(sp_valid.index.equals(expected.index))
        self.assertEqual(len(sp_valid.sp_values), 2)

        result = self.bseries.dropna()
        expected = self.bseries.to_dense().dropna()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)
开发者ID:jcfr,项目名称:pandas,代码行数:18,代码来源:test_series.py

示例7: test_dropna

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
    def test_dropna(self):
        sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)

        sp_valid = sp.valid()

        expected = sp.to_dense().valid()
        expected = expected[expected != 0]
        exp_arr = pd.SparseArray(expected.values, fill_value=0, kind='block')
        tm.assert_sp_array_equal(sp_valid.values, exp_arr)
        self.assert_index_equal(sp_valid.index, expected.index)
        self.assertEqual(len(sp_valid.sp_values), 2)

        result = self.bseries.dropna()
        expected = self.bseries.to_dense().dropna()
        self.assertNotIsInstance(result, SparseSeries)
        tm.assert_series_equal(result, expected)
开发者ID:sunghopark1,项目名称:pandas,代码行数:18,代码来源:test_series.py

示例8: TestSparseSeries

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
class TestSparseSeries(tm.TestCase, SharedWithSparse):
    _multiprocess_can_split_ = True

    def setUp(self):
        arr, index = _test_data1()

        date_index = bdate_range('1/1/2011', periods=len(index))

        self.bseries = SparseSeries(arr, index=index, kind='block',
                                    name='bseries')
        self.ts = self.bseries

        self.btseries = SparseSeries(arr, index=date_index, kind='block')

        self.iseries = SparseSeries(arr, index=index, kind='integer',
                                    name='iseries')

        arr, index = _test_data2()
        self.bseries2 = SparseSeries(arr, index=index, kind='block')
        self.iseries2 = SparseSeries(arr, index=index, kind='integer')

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

        arr, index = _test_data2_zero()
        self.zbseries2 = SparseSeries(arr, index=index, kind='block',
                                      fill_value=0)
        self.ziseries2 = SparseSeries(arr, index=index, kind='integer',
                                      fill_value=0)

    def test_constructor_dtype(self):
        arr = SparseSeries([np.nan, 1, 2, np.nan])
        self.assertEqual(arr.dtype, np.float64)
        self.assertTrue(np.isnan(arr.fill_value))

        arr = SparseSeries([np.nan, 1, 2, np.nan], fill_value=0)
        self.assertEqual(arr.dtype, np.float64)
        self.assertEqual(arr.fill_value, 0)

        arr = SparseSeries([0, 1, 2, 4], dtype=np.int64)
        self.assertEqual(arr.dtype, np.int64)
        self.assertTrue(np.isnan(arr.fill_value))

        arr = SparseSeries([0, 1, 2, 4], fill_value=0, dtype=np.int64)
        self.assertEqual(arr.dtype, np.int64)
        self.assertEqual(arr.fill_value, 0)

    def test_iteration_and_str(self):
        [x for x in self.bseries]
        str(self.bseries)

    def test_TimeSeries_deprecation(self):

        # deprecation TimeSeries, #10890
        with tm.assert_produces_warning(FutureWarning):
            pd.SparseTimeSeries(1, index=pd.date_range('20130101', periods=3))

    def test_construct_DataFrame_with_sp_series(self):
        # it works!
        df = DataFrame({'col': self.bseries})

        # printing & access
        df.iloc[:1]
        df['col']
        df.dtypes
        str(df)

        tm.assert_sp_series_equal(df['col'], self.bseries, check_names=False)

        result = df.iloc[:, 0]
        tm.assert_sp_series_equal(result, self.bseries, check_names=False)

        # blocking
        expected = Series({'col': 'float64:sparse'})
        result = df.ftypes
        tm.assert_series_equal(expected, result)

    def test_series_density(self):
        # GH2803
        ts = Series(np.random.randn(10))
        ts[2:-2] = nan
        sts = ts.to_sparse()
        density = sts.density  # don't die
        self.assertEqual(density, 4 / 10.0)

    def test_sparse_to_dense(self):
        arr, index = _test_data1()
        series = self.bseries.to_dense()
        assert_equal(series, arr)

        series = self.bseries.to_dense(sparse_only=True)
        assert_equal(series, arr[np.isfinite(arr)])

        series = self.iseries.to_dense()
        assert_equal(series, arr)

        arr, index = _test_data1_zero()
#.........这里部分代码省略.........
开发者ID:jcfr,项目名称:pandas,代码行数:103,代码来源:test_series.py

示例9: TestSparseSeries

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import to_dense [as 别名]
class TestSparseSeries(TestCase, test_series.CheckNameIntegration):
    def setUp(self):
        arr, index = _test_data1()

        date_index = DateRange("1/1/2011", periods=len(index))

        self.bseries = SparseSeries(arr, index=index, kind="block")
        self.bseries.name = "bseries"

        self.ts = self.bseries

        self.btseries = SparseSeries(arr, index=date_index, kind="block")

        self.iseries = SparseSeries(arr, index=index, kind="integer")

        arr, index = _test_data2()
        self.bseries2 = SparseSeries(arr, index=index, kind="block")
        self.iseries2 = SparseSeries(arr, index=index, kind="integer")

        arr, index = _test_data1_zero()
        self.zbseries = SparseSeries(arr, index=index, kind="block", fill_value=0)
        self.ziseries = SparseSeries(arr, index=index, kind="integer", fill_value=0)

        arr, index = _test_data2_zero()
        self.zbseries2 = SparseSeries(arr, index=index, kind="block", fill_value=0)
        self.ziseries2 = SparseSeries(arr, index=index, kind="integer", fill_value=0)

    def test_construct_DataFrame_with_sp_series(self):
        # it works!
        df = DataFrame({"col": self.bseries})

    def test_sparse_to_dense(self):
        arr, index = _test_data1()
        series = self.bseries.to_dense()
        assert_equal(series, arr)

        series = self.bseries.to_dense(sparse_only=True)
        assert_equal(series, arr[np.isfinite(arr)])

        series = self.iseries.to_dense()
        assert_equal(series, arr)

        arr, index = _test_data1_zero()
        series = self.zbseries.to_dense()
        assert_equal(series, arr)

        series = self.ziseries.to_dense()
        assert_equal(series, arr)

    def test_dense_to_sparse(self):
        series = self.bseries.to_dense()
        bseries = series.to_sparse(kind="block")
        iseries = series.to_sparse(kind="integer")
        assert_sp_series_equal(bseries, self.bseries)
        assert_sp_series_equal(iseries, self.iseries)

        # non-NaN fill value
        series = self.zbseries.to_dense()
        zbseries = series.to_sparse(kind="block", fill_value=0)
        ziseries = series.to_sparse(kind="integer", fill_value=0)
        assert_sp_series_equal(zbseries, self.zbseries)
        assert_sp_series_equal(ziseries, self.ziseries)

    def test_to_dense_preserve_name(self):
        assert self.bseries.name is not None
        result = self.bseries.to_dense()
        self.assertEquals(result.name, self.bseries.name)

    def test_constructor(self):
        # test setup guys
        self.assert_(np.isnan(self.bseries.fill_value))
        self.assert_(isinstance(self.bseries.sp_index, BlockIndex))
        self.assert_(np.isnan(self.iseries.fill_value))
        self.assert_(isinstance(self.iseries.sp_index, IntIndex))

        self.assertEquals(self.zbseries.fill_value, 0)
        assert_equal(self.zbseries.values, self.bseries.to_dense().fillna(0))

        # pass SparseSeries
        s2 = SparseSeries(self.bseries)
        s3 = SparseSeries(self.iseries)
        s4 = SparseSeries(self.zbseries)
        assert_sp_series_equal(s2, self.bseries)
        assert_sp_series_equal(s3, self.iseries)
        assert_sp_series_equal(s4, self.zbseries)

        # Sparse time series works
        date_index = DateRange("1/1/2000", periods=len(self.bseries))
        s5 = SparseSeries(self.bseries, index=date_index)
        self.assert_(isinstance(s5, SparseTimeSeries))

        # pass Series
        bseries2 = SparseSeries(self.bseries.to_dense())
        assert_equal(self.bseries.sp_values, bseries2.sp_values)

        # pass dict?

        # don't copy the data by default
        values = np.ones(len(self.bseries.sp_values))
        sp = SparseSeries(values, sparse_index=self.bseries.sp_index)
#.........这里部分代码省略.........
开发者ID:klausz,项目名称:pandas,代码行数:103,代码来源:test_sparse.py


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