Python pandas.util.testing.assert_sp_frame_equal() Examples
The following are 30
code examples of pandas.util.testing.assert_sp_frame_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_indexing.py From vnpy_crypto with MIT License | 6 votes |
def test_take_fill_value(self): orig = pd.DataFrame([[1, np.nan, 0], [2, 3, np.nan], [0, np.nan, 4], [0, np.nan, 5]], columns=list('xyz')) sparse = orig.to_sparse(fill_value=0) exp = orig.take([0]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([0]), exp) exp = orig.take([0, 1]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([0, 1]), exp) exp = orig.take([-1, -2]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([-1, -2]), exp)
Example #2
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_reindex(self): orig = pd.DataFrame([[1, np.nan, 0], [2, 3, np.nan], [0, np.nan, 4], [0, np.nan, 5]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse() res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse() tm.assert_sp_frame_equal(res, exp) orig = pd.DataFrame([[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse() res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse() tm.assert_sp_frame_equal(res, exp)
Example #3
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_take_fill_value(self): orig = pd.DataFrame([[1, np.nan, 0], [2, 3, np.nan], [0, np.nan, 4], [0, np.nan, 5]], columns=list('xyz')) sparse = orig.to_sparse(fill_value=0) exp = orig.take([0]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([0]), exp) exp = orig.take([0, 1]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([0, 1]), exp) exp = orig.take([-1, -2]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(sparse.take([-1, -2]), exp)
Example #4
Source File: test_indexing.py From vnpy_crypto with MIT License | 6 votes |
def test_reindex(self): orig = pd.DataFrame([[1, np.nan, 0], [2, 3, np.nan], [0, np.nan, 4], [0, np.nan, 5]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse() res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse() tm.assert_sp_frame_equal(res, exp) orig = pd.DataFrame([[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse() res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse() tm.assert_sp_frame_equal(res, exp)
Example #5
Source File: test_series.py From vnpy_crypto with MIT License | 6 votes |
def test_to_frame(self): # GH 9850 s = pd.SparseSeries([1, 2, 0, nan, 4, nan, 0], name='x') exp = pd.SparseDataFrame({'x': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_sp_frame_equal(s.to_frame(), exp) exp = pd.SparseDataFrame({'y': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_sp_frame_equal(s.to_frame(name='y'), exp) s = pd.SparseSeries([1, 2, 0, nan, 4, nan, 0], name='x', fill_value=0) exp = pd.SparseDataFrame({'x': [1, 2, 0, nan, 4, nan, 0]}, default_fill_value=0) tm.assert_sp_frame_equal(s.to_frame(), exp) exp = pd.DataFrame({'y': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_frame_equal(s.to_frame(name='y').to_dense(), exp)
Example #6
Source File: test_subclass.py From recruit with Apache License 2.0 | 6 votes |
def test_subclass_sparse_slice(self): rows = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] ssdf = tm.SubclassedSparseDataFrame(rows) ssdf.testattr = "testattr" tm.assert_sp_frame_equal(ssdf.loc[:2], tm.SubclassedSparseDataFrame(rows[:3])) tm.assert_sp_frame_equal(ssdf.iloc[:2], tm.SubclassedSparseDataFrame(rows[:2])) tm.assert_sp_frame_equal(ssdf[:2], tm.SubclassedSparseDataFrame(rows[:2])) assert ssdf.loc[:2].testattr == "testattr" assert ssdf.iloc[:2].testattr == "testattr" assert ssdf[:2].testattr == "testattr" tm.assert_sp_series_equal(ssdf.loc[1], tm.SubclassedSparseSeries(rows[1]), check_names=False, check_kind=False) tm.assert_sp_series_equal(ssdf.iloc[1], tm.SubclassedSparseSeries(rows[1]), check_names=False, check_kind=False)
Example #7
Source File: test_indexing.py From vnpy_crypto with MIT License | 6 votes |
def test_getitem(self): orig = pd.DataFrame([[1, np.nan, np.nan], [2, 3, np.nan], [np.nan, np.nan, 4], [0, np.nan, 5]], columns=list('xyz')) sparse = orig.to_sparse() tm.assert_sp_series_equal(sparse['x'], orig['x'].to_sparse()) tm.assert_sp_frame_equal(sparse[['x']], orig[['x']].to_sparse()) tm.assert_sp_frame_equal(sparse[['z', 'x']], orig[['z', 'x']].to_sparse()) tm.assert_sp_frame_equal(sparse[[True, False, True, True]], orig[[True, False, True, True]].to_sparse()) tm.assert_sp_frame_equal(sparse.iloc[[1, 2]], orig.iloc[[1, 2]].to_sparse())
Example #8
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
def test_to_frame(self): # GH 9850 s = pd.SparseSeries([1, 2, 0, nan, 4, nan, 0], name='x') exp = pd.SparseDataFrame({'x': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_sp_frame_equal(s.to_frame(), exp) exp = pd.SparseDataFrame({'y': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_sp_frame_equal(s.to_frame(name='y'), exp) s = pd.SparseSeries([1, 2, 0, nan, 4, nan, 0], name='x', fill_value=0) exp = pd.SparseDataFrame({'x': [1, 2, 0, nan, 4, nan, 0]}, default_fill_value=0) tm.assert_sp_frame_equal(s.to_frame(), exp) exp = pd.DataFrame({'y': [1, 2, 0, nan, 4, nan, 0]}) tm.assert_frame_equal(s.to_frame(name='y').to_dense(), exp)
Example #9
Source File: test_subclass.py From recruit with Apache License 2.0 | 6 votes |
def test_subclass_sparse_to_frame(self): s = tm.SubclassedSparseSeries([1, 2], index=list('ab'), name='xxx') res = s.to_frame() exp_arr = pd.SparseArray([1, 2], dtype=np.int64, kind='block', fill_value=0) exp = tm.SubclassedSparseDataFrame({'xxx': exp_arr}, index=list('ab'), default_fill_value=0) tm.assert_sp_frame_equal(res, exp) # create from int dict res = tm.SubclassedSparseDataFrame({'xxx': [1, 2]}, index=list('ab'), default_fill_value=0) tm.assert_sp_frame_equal(res, exp) s = tm.SubclassedSparseSeries([1.1, 2.1], index=list('ab'), name='xxx') res = s.to_frame() exp = tm.SubclassedSparseDataFrame({'xxx': [1.1, 2.1]}, index=list('ab')) tm.assert_sp_frame_equal(res, exp)
Example #10
Source File: test_to_from_scipy.py From recruit with Apache License 2.0 | 6 votes |
def test_from_scipy_correct_ordering(spmatrix): # GH 16179 arr = np.arange(1, 5).reshape(2, 2) try: spm = spmatrix(arr) assert spm.dtype == arr.dtype except (TypeError, AssertionError): # If conversion to sparse fails for this spmatrix type and arr.dtype, # then the combination is not currently supported in NumPy, so we # can just skip testing it thoroughly return sdf = SparseDataFrame(spm) expected = SparseDataFrame(arr) tm.assert_sp_frame_equal(sdf, expected) tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())
Example #11
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_constructor_ndarray(self, float_frame): # no index or columns sp = SparseDataFrame(float_frame.values) # 1d sp = SparseDataFrame(float_frame['A'].values, index=float_frame.index, columns=['A']) tm.assert_sp_frame_equal(sp, float_frame.reindex(columns=['A'])) # raise on level argument pytest.raises(TypeError, float_frame.reindex, columns=['A'], level=1) # wrong length index / columns with pytest.raises(ValueError, match="^Index length"): SparseDataFrame(float_frame.values, index=float_frame.index[:-1]) with pytest.raises(ValueError, match="^Column length"): SparseDataFrame(float_frame.values, columns=float_frame.columns[:-1]) # GH 9272
Example #12
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_notna(self): # GH 8276 df = pd.SparseDataFrame({'A': [np.nan, np.nan, 1, 2, np.nan], 'B': [0, np.nan, np.nan, 2, np.nan]}) res = df.notna() exp = pd.SparseDataFrame({'A': [False, False, True, True, False], 'B': [True, False, False, True, False]}, default_fill_value=False) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp) # if fill_value is not nan, True can be included in sp_values df = pd.SparseDataFrame({'A': [0, 0, 1, 2, np.nan], 'B': [0, np.nan, 0, 2, np.nan]}, default_fill_value=0.) res = df.notna() assert isinstance(res, pd.SparseDataFrame) exp = pd.DataFrame({'A': [True, True, True, True, False], 'B': [True, False, True, True, False]}) tm.assert_frame_equal(res.to_dense(), exp)
Example #13
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_isna(self): # GH 8276 df = pd.SparseDataFrame({'A': [np.nan, np.nan, 1, 2, np.nan], 'B': [0, np.nan, np.nan, 2, np.nan]}) res = df.isna() exp = pd.SparseDataFrame({'A': [True, True, False, False, True], 'B': [False, True, True, False, True]}, default_fill_value=True) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp) # if fill_value is not nan, True can be included in sp_values df = pd.SparseDataFrame({'A': [0, 0, 1, 2, np.nan], 'B': [0, np.nan, 0, 2, np.nan]}, default_fill_value=0.) res = df.isna() assert isinstance(res, pd.SparseDataFrame) exp = pd.DataFrame({'A': [False, False, False, False, True], 'B': [False, True, False, False, True]}) tm.assert_frame_equal(res.to_dense(), exp)
Example #14
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_astype_bool(self): sparse = pd.SparseDataFrame({'A': SparseArray([0, 2, 0, 4], fill_value=0, dtype=np.int64), 'B': SparseArray([0, 5, 0, 7], fill_value=0, dtype=np.int64)}, default_fill_value=0) assert sparse['A'].dtype == SparseDtype(np.int64) assert sparse['B'].dtype == SparseDtype(np.int64) res = sparse.astype(SparseDtype(bool, False)) exp = pd.SparseDataFrame({'A': SparseArray([False, True, False, True], dtype=np.bool, fill_value=False, kind='integer'), 'B': SparseArray([False, True, False, True], dtype=np.bool, fill_value=False, kind='integer')}, default_fill_value=False) tm.assert_sp_frame_equal(res, exp) assert res['A'].dtype == SparseDtype(np.bool) assert res['B'].dtype == SparseDtype(np.bool)
Example #15
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_transpose(self, float_frame, float_frame_int_kind, float_frame_dense, float_frame_fill0, float_frame_fill0_dense, float_frame_fill2, float_frame_fill2_dense): def _check(frame, orig): transposed = frame.T untransposed = transposed.T tm.assert_sp_frame_equal(frame, untransposed) tm.assert_frame_equal(frame.T.to_dense(), orig.T) tm.assert_frame_equal(frame.T.T.to_dense(), orig.T.T) tm.assert_sp_frame_equal(frame, frame.T.T, exact_indices=False) _check(float_frame, float_frame_dense) _check(float_frame_int_kind, float_frame_dense) _check(float_frame_fill0, float_frame_fill0_dense) _check(float_frame_fill2, float_frame_fill2_dense)
Example #16
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_getitem(self): orig = pd.DataFrame([[1, np.nan, np.nan], [2, 3, np.nan], [np.nan, np.nan, 4], [0, np.nan, 5]], columns=list('xyz')) sparse = orig.to_sparse() tm.assert_sp_series_equal(sparse['x'], orig['x'].to_sparse()) tm.assert_sp_frame_equal(sparse[['x']], orig[['x']].to_sparse()) tm.assert_sp_frame_equal(sparse[['z', 'x']], orig[['z', 'x']].to_sparse()) tm.assert_sp_frame_equal(sparse[[True, False, True, True]], orig[[True, False, True, True]].to_sparse()) tm.assert_sp_frame_equal(sparse.iloc[[1, 2]], orig.iloc[[1, 2]].to_sparse())
Example #17
Source File: test_indexing.py From vnpy_crypto with MIT License | 5 votes |
def test_reindex_fill_value(self): orig = pd.DataFrame([[1, np.nan, 0], [2, 3, np.nan], [0, np.nan, 4], [0, np.nan, 5]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse(fill_value=0) res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0) tm.assert_sp_frame_equal(res, exp) # all missing orig = pd.DataFrame([[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse(fill_value=0) res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0) tm.assert_sp_frame_equal(res, exp) # all fill_value orig = pd.DataFrame([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], index=list('ABCD'), columns=list('xyz')) sparse = orig.to_sparse(fill_value=0) res = sparse.reindex(['A', 'C', 'B']) exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0) tm.assert_sp_frame_equal(res, exp)
Example #18
Source File: test_indexing.py From vnpy_crypto with MIT License | 5 votes |
def test_loc_slice(self): orig = pd.DataFrame([[1, np.nan, np.nan], [2, 3, np.nan], [np.nan, np.nan, 4]], columns=list('xyz')) sparse = orig.to_sparse() tm.assert_sp_frame_equal(sparse.loc[2:], orig.loc[2:].to_sparse())
Example #19
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_assign_with_sparse_frame(self): # GH 19163 df = pd.DataFrame({"a": [1, 2, 3]}) res = df.to_sparse(fill_value=False).assign(newcol=False) exp = df.assign(newcol=False).to_sparse(fill_value=False) tm.assert_sp_frame_equal(res, exp) for column in res.columns: assert type(res[column]) is SparseSeries
Example #20
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_numpy_cumsum(self, float_frame): result = np.cumsum(float_frame) expected = SparseDataFrame(float_frame.to_dense().cumsum()) tm.assert_sp_frame_equal(result, expected) msg = "the 'dtype' parameter is not supported" with pytest.raises(ValueError, match=msg): np.cumsum(float_frame, dtype=np.int64) msg = "the 'out' parameter is not supported" with pytest.raises(ValueError, match=msg): np.cumsum(float_frame, out=result)
Example #21
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_cumsum(self, float_frame): expected = SparseDataFrame(float_frame.to_dense().cumsum()) result = float_frame.cumsum() tm.assert_sp_frame_equal(result, expected) result = float_frame.cumsum(axis=None) tm.assert_sp_frame_equal(result, expected) result = float_frame.cumsum(axis=0) tm.assert_sp_frame_equal(result, expected)
Example #22
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_numeric_op_scalar(self): df = pd.DataFrame({'A': [nan, nan, 0, 1, ], 'B': [0, 1, 2, nan], 'C': [1., 2., 3., 4.], 'D': [nan, nan, nan, nan]}) sparse = df.to_sparse() tm.assert_sp_frame_equal(sparse + 1, (df + 1).to_sparse())
Example #23
Source File: test_combine_concat.py From vnpy_crypto with MIT License | 5 votes |
def test_concat_axis1(self): val1 = np.array([1, 2, np.nan, np.nan, 0, np.nan]) val2 = np.array([3, np.nan, 4, 0, 0]) sparse1 = pd.SparseSeries(val1, name='x') sparse2 = pd.SparseSeries(val2, name='y') res = pd.concat([sparse1, sparse2], axis=1) exp = pd.concat([pd.Series(val1, name='x'), pd.Series(val2, name='y')], axis=1) exp = pd.SparseDataFrame(exp) tm.assert_sp_frame_equal(res, exp)
Example #24
Source File: test_combine_concat.py From vnpy_crypto with MIT License | 5 votes |
def test_concat_different_fill_value(self): # 1st fill_value will be used sparse = self.dense1.to_sparse() sparse2 = self.dense2.to_sparse(fill_value=0) res = pd.concat([sparse, sparse2]) exp = pd.concat([self.dense1, self.dense2]).to_sparse() tm.assert_sp_frame_equal(res, exp) res = pd.concat([sparse2, sparse]) exp = pd.concat([self.dense2, self.dense1]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp)
Example #25
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_combine_first_with_dense(self): # We could support this if we allow # pd.core.dtypes.cast.find_common_type to special case SparseDtype # but I don't think that's worth it. df = self.frame result = df[::2].combine_first(df.to_dense()) expected = df[::2].to_dense().combine_first(df.to_dense()) expected = expected.to_sparse(fill_value=df.default_fill_value) tm.assert_sp_frame_equal(result, expected)
Example #26
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_combine_first(self, float_frame): df = float_frame result = df[::2].combine_first(df) expected = df[::2].to_dense().combine_first(df.to_dense()) expected = expected.to_sparse(fill_value=df.default_fill_value) tm.assert_sp_frame_equal(result, expected)
Example #27
Source File: test_combine_concat.py From vnpy_crypto with MIT License | 5 votes |
def test_concat_series(self): # fill_value = np.nan sparse = self.dense1.to_sparse() sparse2 = self.dense2.to_sparse() for col in ['A', 'D']: res = pd.concat([sparse, sparse2[col]]) exp = pd.concat([self.dense1, self.dense2[col]]).to_sparse() tm.assert_sp_frame_equal(res, exp) res = pd.concat([sparse2[col], sparse]) exp = pd.concat([self.dense2[col], self.dense1]).to_sparse() tm.assert_sp_frame_equal(res, exp) # fill_value = 0 sparse = self.dense1.to_sparse(fill_value=0) sparse2 = self.dense2.to_sparse(fill_value=0) for col in ['C', 'D']: res = pd.concat([sparse, sparse2[col]]) exp = pd.concat([self.dense1, self.dense2[col]]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp) res = pd.concat([sparse2[col], sparse]) exp = pd.concat([self.dense2[col], self.dense1]).to_sparse(fill_value=0) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp)
Example #28
Source File: test_frame.py From recruit with Apache License 2.0 | 5 votes |
def test_numpy_transpose(self): sdf = SparseDataFrame([1, 2, 3], index=[1, 2, 3], columns=['a']) result = np.transpose(np.transpose(sdf)) tm.assert_sp_frame_equal(result, sdf) msg = "the 'axes' parameter is not supported" with pytest.raises(ValueError, match=msg): np.transpose(sdf, axes=1)
Example #29
Source File: test_series.py From vnpy_crypto with MIT License | 5 votes |
def test_concat_axis1(self): val1 = np.array([1, 2, np.nan, np.nan, 0, np.nan]) val2 = np.array([3, np.nan, 4, 0, 0]) sparse1 = pd.SparseSeries(val1, name='x') sparse2 = pd.SparseSeries(val2, name='y') res = pd.concat([sparse1, sparse2], axis=1) exp = pd.concat([pd.Series(val1, name='x'), pd.Series(val2, name='y')], axis=1) exp = pd.SparseDataFrame(exp) tm.assert_sp_frame_equal(res, exp)
Example #30
Source File: test_format.py From vnpy_crypto with MIT License | 5 votes |
def test_sparse_repr_after_set(self): # GH 15488 sdf = pd.SparseDataFrame([[np.nan, 1], [2, np.nan]]) res = sdf.copy() # Ignore the warning with pd.option_context('mode.chained_assignment', None): sdf[0][1] = 2 # This line triggers the bug repr(sdf) tm.assert_sp_frame_equal(sdf, res)