本文整理汇总了Python中pandas.util.testing.assert_sp_array_equal方法的典型用法代码示例。如果您正苦于以下问题:Python testing.assert_sp_array_equal方法的具体用法?Python testing.assert_sp_array_equal怎么用?Python testing.assert_sp_array_equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.util.testing
的用法示例。
在下文中一共展示了testing.assert_sp_array_equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dropna
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_dropna(self):
sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)
sp_valid = sp.dropna()
expected = sp.to_dense().dropna()
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)
tm.assert_index_equal(sp_valid.index, expected.index)
assert len(sp_valid.sp_values) == 2
result = self.bseries.dropna()
expected = self.bseries.to_dense().dropna()
assert not isinstance(result, SparseSeries)
tm.assert_series_equal(result, expected)
示例2: test_take_filling_all_nan
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_take_filling_all_nan(self):
sparse = SparseArray([np.nan, np.nan, np.nan, np.nan, np.nan])
# XXX: did the default kind from take change?
result = sparse.take(np.array([1, 0, -1]))
expected = SparseArray([np.nan, np.nan, np.nan], kind='block')
tm.assert_sp_array_equal(result, expected)
result = sparse.take(np.array([1, 0, -1]), fill_value=True)
expected = SparseArray([np.nan, np.nan, np.nan], kind='block')
tm.assert_sp_array_equal(result, expected)
with pytest.raises(IndexError):
sparse.take(np.array([1, -6]))
with pytest.raises(IndexError):
sparse.take(np.array([1, 5]))
with pytest.raises(IndexError):
sparse.take(np.array([1, 5]), fill_value=True)
示例3: test_astype
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_astype(self):
# float -> float
arr = SparseArray([None, None, 0, 2])
result = arr.astype("Sparse[float32]")
expected = SparseArray([None, None, 0, 2], dtype=np.dtype('float32'))
tm.assert_sp_array_equal(result, expected)
dtype = SparseDtype("float64", fill_value=0)
result = arr.astype(dtype)
expected = SparseArray._simple_new(np.array([0., 2.],
dtype=dtype.subtype),
IntIndex(4, [2, 3]),
dtype)
tm.assert_sp_array_equal(result, expected)
dtype = SparseDtype("int64", 0)
result = arr.astype(dtype)
expected = SparseArray._simple_new(np.array([0, 2], dtype=np.int64),
IntIndex(4, [2, 3]),
dtype)
tm.assert_sp_array_equal(result, expected)
arr = SparseArray([0, np.nan, 0, 1], fill_value=0)
with pytest.raises(ValueError, match='NA'):
arr.astype('Sparse[i8]')
示例4: test_getslice_tuple
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_getslice_tuple(self):
dense = np.array([np.nan, 0, 3, 4, 0, 5, np.nan, np.nan, 0])
sparse = SparseArray(dense)
res = sparse[4:, ]
exp = SparseArray(dense[4:, ])
tm.assert_sp_array_equal(res, exp)
sparse = SparseArray(dense, fill_value=0)
res = sparse[4:, ]
exp = SparseArray(dense[4:, ], fill_value=0)
tm.assert_sp_array_equal(res, exp)
with pytest.raises(IndexError):
sparse[4:, :]
with pytest.raises(IndexError):
# check numpy compat
dense[4:, :]
示例5: test_map
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_map():
arr = SparseArray([0, 1, 2])
expected = SparseArray([10, 11, 12], fill_value=10)
# dict
result = arr.map({0: 10, 1: 11, 2: 12})
tm.assert_sp_array_equal(result, expected)
# series
result = arr.map(pd.Series({0: 10, 1: 11, 2: 12}))
tm.assert_sp_array_equal(result, expected)
# function
result = arr.map(pd.Series({0: 10, 1: 11, 2: 12}))
expected = SparseArray([10, 11, 12], fill_value=10)
tm.assert_sp_array_equal(result, expected)
示例6: test_sparseseries_roundtrip
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_sparseseries_roundtrip(self):
# GH 13999
for kind in ['integer', 'block']:
for fill in [1, np.nan, 0]:
arr = SparseArray([np.nan, 1, np.nan, 2, 3], kind=kind,
fill_value=fill)
res = SparseArray(SparseSeries(arr))
tm.assert_sp_array_equal(arr, res)
arr = SparseArray([0, 0, 0, 1, 1, 2], dtype=np.int64,
kind=kind, fill_value=fill)
res = SparseArray(SparseSeries(arr), dtype=np.int64)
tm.assert_sp_array_equal(arr, res)
res = SparseArray(SparseSeries(arr))
tm.assert_sp_array_equal(arr, res)
for fill in [True, False, np.nan]:
arr = SparseArray([True, False, True, True], dtype=np.bool,
kind=kind, fill_value=fill)
res = SparseArray(SparseSeries(arr))
tm.assert_sp_array_equal(arr, res)
res = SparseArray(SparseSeries(arr))
tm.assert_sp_array_equal(arr, res)
示例7: test_take_filling_all_nan
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_take_filling_all_nan(self):
sparse = SparseArray([np.nan, np.nan, np.nan, np.nan, np.nan])
result = sparse.take(np.array([1, 0, -1]))
expected = SparseArray([np.nan, np.nan, np.nan])
tm.assert_sp_array_equal(result, expected)
result = sparse.take(np.array([1, 0, -1]), fill_value=True)
expected = SparseArray([np.nan, np.nan, np.nan])
tm.assert_sp_array_equal(result, expected)
with pytest.raises(IndexError):
sparse.take(np.array([1, -6]))
with pytest.raises(IndexError):
sparse.take(np.array([1, 5]))
with pytest.raises(IndexError):
sparse.take(np.array([1, 5]), fill_value=True)
示例8: tests_indexing_with_sparse
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def tests_indexing_with_sparse(self, kind, fill):
# see gh-13985
arr = pd.SparseArray([1, 2, 3], kind=kind)
indexer = pd.SparseArray([True, False, True],
fill_value=fill,
dtype=bool)
expected = arr[indexer]
result = pd.SparseArray([1, 3], kind=kind)
tm.assert_sp_array_equal(result, expected)
s = pd.SparseSeries(arr, index=["a", "b", "c"], dtype=np.float64)
expected = pd.SparseSeries([1, 3], index=["a", "c"], kind=kind,
dtype=SparseDtype(np.float64, s.fill_value))
tm.assert_sp_series_equal(s[indexer], expected)
tm.assert_sp_series_equal(s.loc[indexer], expected)
tm.assert_sp_series_equal(s.iloc[indexer], expected)
indexer = pd.SparseSeries(indexer, index=["a", "b", "c"])
tm.assert_sp_series_equal(s[indexer], expected)
tm.assert_sp_series_equal(s.loc[indexer], expected)
msg = ("iLocation based boolean indexing cannot "
"use an indexable as a mask")
with pytest.raises(ValueError, match=msg):
s.iloc[indexer]
示例9: test_to_sparse
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_to_sparse():
# https://github.com/pandas-dev/pandas/issues/22389
arr = pd.SparseArray([1, 2, None, 3])
result = pd.Series(arr).to_sparse()
assert len(result) == 4
tm.assert_sp_array_equal(result.values, arr, check_kind=False)
示例10: test_with_list
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_with_list(op):
arr = pd.SparseArray([0, 1], fill_value=0)
result = op(arr, [0, 1])
expected = op(arr, pd.SparseArray([0, 1]))
tm.assert_sp_array_equal(result, expected)
示例11: test_sparray_inplace
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_sparray_inplace():
sparray = pd.SparseArray([0, 2, 0, 0])
ndarray = np.array([0, 1, 2, 3])
sparray += ndarray
expected = pd.SparseArray([0, 3, 2, 3], fill_value=0)
tm.assert_sp_array_equal(sparray, expected)
示例12: test_invert
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_invert(fill_value):
arr = np.array([True, False, False, True])
sparray = pd.SparseArray(arr, fill_value=fill_value)
result = ~sparray
expected = pd.SparseArray(~arr, fill_value=not fill_value)
tm.assert_sp_array_equal(result, expected)
示例13: test_unary_op
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_unary_op(op, fill_value):
arr = np.array([0, 1, np.nan, 2])
sparray = pd.SparseArray(arr, fill_value=fill_value)
result = op(sparray)
expected = pd.SparseArray(op(arr), fill_value=op(fill_value))
tm.assert_sp_array_equal(result, expected)
示例14: test_constructor_dtype_str
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_constructor_dtype_str(self):
result = SparseArray([1, 2, 3], dtype='int')
expected = SparseArray([1, 2, 3], dtype=int)
tm.assert_sp_array_equal(result, expected)
示例15: test_constructor_sparse_dtype
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_array_equal [as 别名]
def test_constructor_sparse_dtype(self):
result = SparseArray([1, 0, 0, 1], dtype=SparseDtype('int64', -1))
expected = SparseArray([1, 0, 0, 1], fill_value=-1, dtype=np.int64)
tm.assert_sp_array_equal(result, expected)
assert result.sp_values.dtype == np.dtype('int64')