本文整理匯總了Python中scipy.sparse.shift方法的典型用法代碼示例。如果您正苦於以下問題:Python sparse.shift方法的具體用法?Python sparse.shift怎麽用?Python sparse.shift使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.sparse
的用法示例。
在下文中一共展示了sparse.shift方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_shift
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
def test_shift(self):
series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))
shifted = series.shift(0)
# assert shifted is not series
tm.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., 2., 3., nan, nan],
index=bdate_range('1/1/2000', periods=6))
f = lambda s: s.shift(2, freq='B')
_dense_series_compare(series, f)
f = lambda s: s.shift(2, freq=BDay())
_dense_series_compare(series, f)
示例2: test_shift
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
def test_shift(self):
series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))
shifted = series.shift(0)
assert shifted is not series
tm.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., 2., 3., nan, nan],
index=bdate_range('1/1/2000', periods=6))
f = lambda s: s.shift(2, freq='B')
_dense_series_compare(series, f)
f = lambda s: s.shift(2, freq=BDay())
_dense_series_compare(series, f)
示例3: test_shift_dtype
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
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())
sparse = orig.to_sparse(fill_value=np.nan)
tm.assert_sp_series_equal(sparse.shift(0),
orig.shift(0).to_sparse(fill_value=np.nan))
# shift(1) or more span changes dtype to float64
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())
示例4: test_shift_dtype_fill_value
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
def test_shift_dtype_fill_value(self):
# GH 12908
orig = pd.Series([1, 0, 0, 4], dtype=np.int64)
for v in [0, 1, np.nan]:
sparse = orig.to_sparse(fill_value=v)
tm.assert_sp_series_equal(sparse.shift(0),
orig.shift(0).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(1),
orig.shift(1).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(2),
orig.shift(2).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(3),
orig.shift(3).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(-1),
orig.shift(-1).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(-2),
orig.shift(-2).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(-3),
orig.shift(-3).to_sparse(fill_value=v))
tm.assert_sp_series_equal(sparse.shift(-4),
orig.shift(-4).to_sparse(fill_value=v))
示例5: test_shift_dtype
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
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())
sparse = orig.to_sparse(fill_value=np.nan)
tm.assert_sp_series_equal(sparse.shift(0),
orig.shift(0).to_sparse(fill_value=np.nan))
# shift(1) or more span changes dtype to float64
# XXX: SparseSeries doesn't need to shift dtype here.
# Do we want to astype in shift, for backwards compat?
# If not, document it.
tm.assert_sp_series_equal(sparse.shift(1).astype('f8'),
orig.shift(1).to_sparse(kind='integer'))
tm.assert_sp_series_equal(sparse.shift(2).astype('f8'),
orig.shift(2).to_sparse(kind='integer'))
tm.assert_sp_series_equal(sparse.shift(3).astype('f8'),
orig.shift(3).to_sparse(kind='integer'))
tm.assert_sp_series_equal(sparse.shift(-1).astype('f8'),
orig.shift(-1).to_sparse(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-2).astype('f8'),
orig.shift(-2).to_sparse(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-3).astype('f8'),
orig.shift(-3).to_sparse(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-4).astype('f8'),
orig.shift(-4).to_sparse(),
check_kind=False)
示例6: test_shift_dtype_fill_value
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
def test_shift_dtype_fill_value(self, fill_value, periods):
# GH 12908
orig = pd.Series([1, 0, 0, 4], dtype=np.dtype('int64'))
sparse = orig.to_sparse(fill_value=fill_value)
result = sparse.shift(periods)
expected = orig.shift(periods).to_sparse(fill_value=fill_value)
tm.assert_sp_series_equal(result, expected,
check_kind=False,
consolidate_block_indices=True)
示例7: test_shift_nan
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
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))
示例8: test_shift_nan
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import shift [as 別名]
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(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse(),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse(),
check_kind=False)
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=sparse.fill_value)
)
tm.assert_sp_series_equal(sparse.shift(1),
orig.shift(1).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(2),
orig.shift(2).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(3),
orig.shift(3).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-1),
orig.shift(-1).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-2),
orig.shift(-2).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-3),
orig.shift(-3).to_sparse(fill_value=0),
check_kind=False)
tm.assert_sp_series_equal(sparse.shift(-4),
orig.shift(-4).to_sparse(fill_value=0),
check_kind=False)