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


Python SparseSeries.reindex方法代码示例

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


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

示例1: test_constructor_scalar

# 需要导入模块: from pandas.sparse.api import SparseSeries [as 别名]
# 或者: from pandas.sparse.api.SparseSeries import reindex [as 别名]
    def test_constructor_scalar(self):
        data = 5
        sp = SparseSeries(data, np.arange(100))
        sp = sp.reindex(np.arange(200))
        self.assertTrue((sp.ix[:99] == data).all())
        self.assertTrue(isnull(sp.ix[100:]).all())

        data = np.nan
        sp = SparseSeries(data, np.arange(100))
        self.assertEqual(len(sp), 100)
        self.assertEqual(sp.shape, (100, ))
开发者ID:jcfr,项目名称:pandas,代码行数:13,代码来源:test_series.py

示例2: TestSparseSeries

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

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

        # Sparse time series works
        date_index = bdate_range('1/1/2000', periods=len(self.bseries))
        s5 = SparseSeries(self.bseries, index=date_index)
        tm.assertIsInstance(s5, SparseSeries)

        # 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(self.bseries.npoints)
        sp = SparseSeries(values, sparse_index=self.bseries.sp_index)
        sp.sp_values[:5] = 97
        self.assertEqual(values[0], 97)

        self.assertEqual(len(sp), 20)
        self.assertEqual(sp.shape, (20, ))

        # but can make it copy!
        sp = SparseSeries(values, sparse_index=self.bseries.sp_index,
                          copy=True)
        sp.sp_values[:5] = 100
        self.assertEqual(values[0], 97)

        self.assertEqual(len(sp), 20)
        self.assertEqual(sp.shape, (20, ))

    def test_constructor_scalar(self):
        data = 5
        sp = SparseSeries(data, np.arange(100))
        sp = sp.reindex(np.arange(200))
        self.assertTrue((sp.ix[:99] == data).all())
        self.assertTrue(isnull(sp.ix[100:]).all())

        data = np.nan
        sp = SparseSeries(data, np.arange(100))
        self.assertEqual(len(sp), 100)
        self.assertEqual(sp.shape, (100, ))

    def test_constructor_ndarray(self):
        pass

    def test_constructor_nonnan(self):
        arr = [0, 0, 0, nan, nan]
        sp_series = SparseSeries(arr, fill_value=0)
        assert_equal(sp_series.values.values, arr)
        self.assertEqual(len(sp_series), 5)
        self.assertEqual(sp_series.shape, (5, ))

    # GH 9272
    def test_constructor_empty(self):
        sp = SparseSeries()
        self.assertEqual(len(sp.index), 0)
        self.assertEqual(sp.shape, (0, ))

    def test_copy_astype(self):
        cop = self.bseries.astype(np.float64)
        self.assertIsNot(cop, self.bseries)
        self.assertIs(cop.sp_index, self.bseries.sp_index)
        self.assertEqual(cop.dtype, np.float64)

        cop2 = self.iseries.copy()
开发者ID:jcfr,项目名称:pandas,代码行数:69,代码来源:test_series.py

示例3: TestSparseSeries

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

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

        # exception handling
        self.assertRaises(Exception, self.bseries.__getitem__, len(self.bseries) + 1)

        # index not contained
        self.assertRaises(Exception, self.btseries.__getitem__, self.btseries.index[-1] + BDay())

    def test_get_get_value(self):
        assert_almost_equal(self.bseries.get(10), self.bseries[10])
        self.assert_(self.bseries.get(len(self.bseries) + 1) is None)

        dt = self.btseries.index[10]
        result = self.btseries.get(dt)
        expected = self.btseries.to_dense()[dt]
        assert_almost_equal(result, expected)

        assert_almost_equal(self.bseries.get_value(10), self.bseries[10])

    def test_set_value(self):
        idx = self.btseries.index[7]
        res = self.btseries.set_value(idx, 0)
        self.assert_(res is not self.btseries)
        self.assertEqual(res[idx], 0)

        res = self.iseries.set_value("foobar", 0)
        self.assert_(res is not self.iseries)
        self.assert_(res.index[-1] == "foobar")
        self.assertEqual(res["foobar"], 0)

    def test_getitem_slice(self):
        idx = self.bseries.index
        res = self.bseries[::2]
        self.assert_(isinstance(res, SparseSeries))
        assert_sp_series_equal(res, self.bseries.reindex(idx[::2]))

        res = self.bseries[:5]
        self.assert_(isinstance(res, SparseSeries))
        assert_sp_series_equal(res, self.bseries.reindex(idx[:5]))

        res = self.bseries[5:]
        assert_sp_series_equal(res, self.bseries.reindex(idx[5:]))

        # negative indices
        res = self.bseries[:-3]
        assert_sp_series_equal(res, self.bseries.reindex(idx[:-3]))

    def test_take(self):
        def _compare_with_dense(sp):
            dense = sp.to_dense()

            def _compare(idx):
                dense_result = dense.take(idx).values
                sparse_result = sp.take(idx)
                self.assert_(isinstance(sparse_result, SparseSeries))
                assert_almost_equal(dense_result, sparse_result.values)

            _compare([1.0, 2.0, 3.0, 4.0, 5.0, 0.0])
            _compare([7, 2, 9, 0, 4])
            _compare([3, 6, 3, 4, 7])

        self._check_all(_compare_with_dense)

        self.assertRaises(Exception, self.bseries.take, [-1, 0])
        self.assertRaises(Exception, self.bseries.take, [0, len(self.bseries) + 1])

        # Corner case
开发者ID:klausz,项目名称:pandas,代码行数:70,代码来源:test_sparse.py


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