本文整理汇总了Python中pandas.core.sparse.SparseSeries类的典型用法代码示例。如果您正苦于以下问题:Python SparseSeries类的具体用法?Python SparseSeries怎么用?Python SparseSeries使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SparseSeries类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_valid
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)
示例2: setUp
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)
示例3: _check
def _check(values, index1, index2, fill_value):
first_series = SparseSeries(values, sparse_index=index1, fill_value=fill_value)
reindexed = first_series.sparse_reindex(index2)
self.assert_(reindexed.sp_index is index2)
int_indices1 = index1.to_int_index().indices
int_indices2 = index2.to_int_index().indices
expected = Series(values, index=int_indices1)
expected = expected.reindex(int_indices2).fillna(fill_value)
assert_almost_equal(expected.values, reindexed.sp_values)
示例4: test_shift
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)
示例5: test_take
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)
assert_almost_equal(dense_result, sparse_result)
_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
sp = SparseSeries(np.ones(10.0) * nan)
assert_almost_equal(sp.take([0, 1, 2, 3, 4]), np.repeat(nan, 5))
示例6: setUp
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.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)
示例7: test_constructor
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, spm.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)
sp.sp_values[:5] = 97
self.assert_(values[0] == 97)
# but can make it copy!
sp = SparseSeries(values, sparse_index=self.bseries.sp_index,
copy=True)
sp.sp_values[:5] = 100
self.assert_(values[0] == 97)
示例8: TestSparseSeries
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_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, spm.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
#.........这里部分代码省略.........