Exemple #1
0
def SparseDataFrame(X, default_fill_value=0.0):
    if sparse.issparse(X):
        X = pd.DataFrame.sparse.from_spmatrix(X)
        X.sparse.fill_value = default_fill_value
    elif is_SparseDataFrame(X) or not isinstance(X, pd.DataFrame):
        X = pd.DataFrame(X)
    return X.astype(pd.SparseDtype(float, fill_value=default_fill_value))
Exemple #2
0
def _typename(X):
    if (
        isinstance(X, pd.DataFrame)
        and not is_SparseDataFrame(X)
        and hasattr(X, "sparse")
    ):
        return "DataFrame[SparseArray]"
    else:
        return type(X).__name__
Exemple #3
0
def assert_matrix_class_equivalent(X, Y):
    """Check the format of X and Y are the same.

    We expect:
        * shape hasn't changed
        * sparsity hasn't changed
        * class hasn't changed (except perhaps which kind of spmatrix)
        * column and index names haven't changed
    """
    assert X.shape == Y.shape
    if sparse.issparse(X):
        assert sparse.issparse(Y)
        assert X.tocoo().nnz == Y.tocoo().nnz
    elif is_SparseDataFrame(X):
        assert _is_sparse_dataframe(Y)
    else:
        assert type(X) == type(Y), (type(X), type(Y))
    if _is_sparse_dataframe(X):
        assert _sparse_dataframe_density(X) == _sparse_dataframe_density(Y)
        assert _sparse_dataframe_density(X) == _sparse_dataframe_density(Y)
    if isinstance(X, pd.DataFrame):
        assert np.all(X.columns == Y.columns)
        assert np.all(X.index == Y.index)
    return True
Exemple #4
0
def _is_sparse_dataframe(X):
    return is_SparseDataFrame(X) or (
        isinstance(X, pd.DataFrame) and hasattr(X, "sparse")
    )