Python pandas.util.testing.assert_sp_array_equal() Examples
The following are 30
code examples of pandas.util.testing.assert_sp_array_equal().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas.util.testing
, or try the search function
.
Example #1
Source File: test_array.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #2
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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]')
Example #3
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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:, :]
Example #5
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: test_array.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: test_array.py From recruit with Apache License 2.0 | 6 votes |
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]')
Example #8
Source File: test_array.py From recruit with Apache License 2.0 | 6 votes |
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:, :]
Example #9
Source File: test_series.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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)
Example #10
Source File: test_array.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #11
Source File: test_series.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #12
Source File: test_array.py From vnpy_crypto with MIT License | 6 votes |
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:, :]
Example #13
Source File: test_array.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #14
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_constructor_spindex_dtype_scalar_broadcasts(self): arr = SparseArray(data=[1, 2], sparse_index=IntIndex(4, [1, 2]), fill_value=0, dtype=None) exp = SparseArray([0, 1, 2, 0], fill_value=0, dtype=None) tm.assert_sp_array_equal(arr, exp) assert arr.dtype == SparseDtype(np.int64) assert arr.fill_value == 0
Example #15
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_constructor_spindex_dtype_scalar(self, sparse_index): # scalar input arr = SparseArray(data=1, sparse_index=sparse_index, dtype=None) exp = SparseArray([1], dtype=None) tm.assert_sp_array_equal(arr, exp) assert arr.dtype == SparseDtype(np.int64) assert arr.fill_value == 0 arr = SparseArray(data=1, sparse_index=IntIndex(1, [0]), dtype=None) exp = SparseArray([1], dtype=None) tm.assert_sp_array_equal(arr, exp) assert arr.dtype == SparseDtype(np.int64) assert arr.fill_value == 0
Example #16
Source File: test_series.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #17
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_scalar_with_index_infer_dtype(self, scalar, dtype): # GH 19163 arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar) exp = SparseArray([scalar, scalar, scalar], fill_value=scalar) tm.assert_sp_array_equal(arr, exp) assert arr.dtype == dtype assert exp.dtype == dtype
Example #18
Source File: test_indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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]
Example #19
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_ufunc_args(self): # GH 13853 make sure ufunc is applied to fill_value, including its arg sparse = SparseArray([1, np.nan, 2, np.nan, -2]) result = SparseArray([2, np.nan, 3, np.nan, -1]) tm.assert_sp_array_equal(np.add(sparse, 1), result) sparse = SparseArray([1, -1, 2, -2], fill_value=1) result = SparseArray([2, 0, 3, -1], fill_value=2) tm.assert_sp_array_equal(np.add(sparse, 1), result) sparse = SparseArray([1, -1, 0, -2], fill_value=0) result = SparseArray([2, 0, 1, -1], fill_value=1) tm.assert_sp_array_equal(np.add(sparse, 1), result)
Example #20
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_ufunc(self): # GH 13853 make sure ufunc is applied to fill_value sparse = SparseArray([1, np.nan, 2, np.nan, -2]) result = SparseArray([1, np.nan, 2, np.nan, 2]) tm.assert_sp_array_equal(abs(sparse), result) tm.assert_sp_array_equal(np.abs(sparse), result) sparse = SparseArray([1, -1, 2, -2], fill_value=1) result = SparseArray([1, 2, 2], sparse_index=sparse.sp_index, fill_value=1) tm.assert_sp_array_equal(abs(sparse), result) tm.assert_sp_array_equal(np.abs(sparse), result) sparse = SparseArray([1, -1, 2, -2], fill_value=-1) result = SparseArray([1, 2, 2], sparse_index=sparse.sp_index, fill_value=1) tm.assert_sp_array_equal(abs(sparse), result) tm.assert_sp_array_equal(np.abs(sparse), result) sparse = SparseArray([1, np.nan, 2, np.nan, -2]) result = SparseArray(np.sin([1, np.nan, 2, np.nan, -2])) tm.assert_sp_array_equal(np.sin(sparse), result) sparse = SparseArray([1, -1, 2, -2], fill_value=1) result = SparseArray(np.sin([1, -1, 2, -2]), fill_value=np.sin(1)) tm.assert_sp_array_equal(np.sin(sparse), result) sparse = SparseArray([1, -1, 0, -2], fill_value=0) result = SparseArray(np.sin([1, -1, 0, -2]), fill_value=np.sin(0)) tm.assert_sp_array_equal(np.sin(sparse), result)
Example #21
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_numpy_cumsum(self): non_null_data = np.array([1, 2, 3, 4, 5], dtype=float) non_null_expected = SparseArray(non_null_data.cumsum()) null_data = np.array([1, 2, np.nan, 4, 5], dtype=float) null_expected = SparseArray(np.array([1.0, 3.0, np.nan, 7.0, 12.0])) for data, expected in [ (null_data, null_expected), (non_null_data, non_null_expected) ]: out = np.cumsum(SparseArray(data)) tm.assert_sp_array_equal(out, expected) out = np.cumsum(SparseArray(data, fill_value=np.nan)) tm.assert_sp_array_equal(out, expected) out = np.cumsum(SparseArray(data, fill_value=2)) tm.assert_sp_array_equal(out, expected) msg = "the 'dtype' parameter is not supported" tm.assert_raises_regex(ValueError, msg, np.cumsum, SparseArray(data), dtype=np.int64) msg = "the 'out' parameter is not supported" tm.assert_raises_regex(ValueError, msg, np.cumsum, SparseArray(data), out=out)
Example #22
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_fillna_overlap(self): s = SparseArray([1, np.nan, np.nan, 3, np.nan]) # filling with existing value doesn't replace existing value with # fill_value, i.e. existing 3 remains in sp_values res = s.fillna(3) exp = np.array([1, 3, 3, 3, 3], dtype=np.float64) tm.assert_numpy_array_equal(res.to_dense(), exp) s = SparseArray([1, np.nan, np.nan, 3, np.nan], fill_value=0) res = s.fillna(3) exp = SparseArray([1, 3, 3, 3, 3], fill_value=0, dtype=np.float64) tm.assert_sp_array_equal(res, exp)
Example #23
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_astype_bool(self): a = pd.SparseArray([1, 0, 0, 1], dtype=SparseDtype(int, 0)) result = a.astype(bool) expected = SparseArray([True, 0, 0, True], dtype=SparseDtype(bool, 0)) tm.assert_sp_array_equal(result, expected) # update fill value result = a.astype(SparseDtype(bool, False)) expected = SparseArray([True, False, False, True], dtype=SparseDtype(bool, False)) tm.assert_sp_array_equal(result, expected)
Example #24
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_pickle(self): def _check_roundtrip(obj): unpickled = tm.round_trip_pickle(obj) tm.assert_sp_array_equal(unpickled, obj) _check_roundtrip(self.arr) _check_roundtrip(self.zarr)
Example #25
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_astype_more(self, array, dtype, expected): result = array.astype(dtype) tm.assert_sp_array_equal(result, expected)
Example #26
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_getitem_arraylike_mask(self): arr = SparseArray([0, 1, 2]) result = arr[[True, False, True]] expected = SparseArray([0, 2]) tm.assert_sp_array_equal(result, expected)
Example #27
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_take_fill_value(self): data = np.array([1, np.nan, 0, 3, 0]) sparse = SparseArray(data, fill_value=0) exp = SparseArray(np.take(data, [0]), fill_value=0) tm.assert_sp_array_equal(sparse.take([0]), exp) exp = SparseArray(np.take(data, [1, 3, 4]), fill_value=0) tm.assert_sp_array_equal(sparse.take([1, 3, 4]), exp)
Example #28
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_take_filling_fill_value(self): # same tests as GH 12631 sparse = SparseArray([np.nan, 0, 1, 0, 4], fill_value=0) result = sparse.take(np.array([1, 0, -1])) expected = SparseArray([0, np.nan, 4], fill_value=0) tm.assert_sp_array_equal(result, expected) # fill_value result = sparse.take(np.array([1, 0, -1]), fill_value=True) expected = SparseArray([0, np.nan, 0], fill_value=0) tm.assert_sp_array_equal(result, expected) # allow_fill=False result = sparse.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True) expected = SparseArray([0, np.nan, 4], fill_value=0) tm.assert_sp_array_equal(result, expected) msg = ('When allow_fill=True and fill_value is not None, ' 'all indices must be >= -1') with tm.assert_raises_regex(ValueError, msg): sparse.take(np.array([1, 0, -2]), fill_value=True) with tm.assert_raises_regex(ValueError, msg): sparse.take(np.array([1, 0, -5]), fill_value=True) 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)
Example #29
Source File: test_array.py From vnpy_crypto with MIT License | 5 votes |
def test_take_filling(self): # similar tests as GH 12631 sparse = SparseArray([np.nan, np.nan, 1, np.nan, 4]) result = sparse.take(np.array([1, 0, -1])) expected = SparseArray([np.nan, np.nan, 4]) tm.assert_sp_array_equal(result, expected) # fill_value 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) # allow_fill=False result = sparse.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True) expected = SparseArray([np.nan, np.nan, 4]) tm.assert_sp_array_equal(result, expected) msg = ('When allow_fill=True and fill_value is not None, ' 'all indices must be >= -1') with tm.assert_raises_regex(ValueError, msg): sparse.take(np.array([1, 0, -2]), fill_value=True) with tm.assert_raises_regex(ValueError, msg): sparse.take(np.array([1, 0, -5]), fill_value=True) 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)
Example #30
Source File: test_arithmetics.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)