当前位置: 首页>>代码示例>>Python>>正文


Python pandas.SparseDtype方法代码示例

本文整理汇总了Python中pandas.SparseDtype方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.SparseDtype方法的具体用法?Python pandas.SparseDtype怎么用?Python pandas.SparseDtype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas的用法示例。


在下文中一共展示了pandas.SparseDtype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_isna

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_isna(self, data_missing):
        expected_dtype = SparseDtype(bool,
                                     pd.isna(data_missing.dtype.fill_value))
        expected = SparseArray([True, False], dtype=expected_dtype)

        result = pd.isna(data_missing)
        self.assert_equal(result, expected)

        result = pd.Series(data_missing).isna()
        expected = pd.Series(expected)
        self.assert_series_equal(result, expected)

        # GH 21189
        result = pd.Series(data_missing).drop([0, 1]).isna()
        expected = pd.Series([], dtype=expected_dtype)
        self.assert_series_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_sparse.py

示例2: test_fillna_frame

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_fillna_frame(self, data_missing):
        # Have to override to specify that fill_value will change.
        fill_value = data_missing[1]

        result = pd.DataFrame({
            "A": data_missing,
            "B": [1, 2]
        }).fillna(fill_value)

        if pd.isna(data_missing.fill_value):
            dtype = SparseDtype(data_missing.dtype, fill_value)
        else:
            dtype = data_missing.dtype

        expected = pd.DataFrame({
            "A": data_missing._from_sequence([fill_value, fill_value],
                                             dtype=dtype),
            "B": [1, 2],
        })

        self.assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_sparse.py

示例3: test_where_series

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_where_series(self, data, na_value):
        assert data[0] != data[1]
        cls = type(data)
        a, b = data[:2]

        ser = pd.Series(cls._from_sequence([a, a, b, b], dtype=data.dtype))

        cond = np.array([True, True, False, False])
        result = ser.where(cond)

        new_dtype = SparseDtype('float', 0.0)
        expected = pd.Series(cls._from_sequence([a, a, na_value, na_value],
                                                dtype=new_dtype))
        self.assert_series_equal(result, expected)

        other = cls._from_sequence([a, b, a, b], dtype=data.dtype)
        cond = np.array([True, False, True, True])
        result = ser.where(cond, other)
        expected = pd.Series(cls._from_sequence([a, b, b, b],
                                                dtype=data.dtype))
        self.assert_series_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_sparse.py

示例4: table_type

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def table_type(df_column):
    # Note - this only works with Pandas >= 1.0.0

    if sys.version_info < (3, 0):  # Pandas 1.0.0 does not support Python 2
        return 'any'

    if isinstance(df_column.dtype, pd.DatetimeTZDtype):
        return 'datetime',
    elif (isinstance(df_column.dtype, pd.StringDtype) or
            isinstance(df_column.dtype, pd.BooleanDtype) or
            isinstance(df_column.dtype, pd.CategoricalDtype) or
            isinstance(df_column.dtype, pd.PeriodDtype)):
        return 'text'
    elif (isinstance(df_column.dtype, pd.SparseDtype) or
            isinstance(df_column.dtype, pd.IntervalDtype) or
            isinstance(df_column.dtype, pd.Int8Dtype) or
            isinstance(df_column.dtype, pd.Int16Dtype) or
            isinstance(df_column.dtype, pd.Int32Dtype) or
            isinstance(df_column.dtype, pd.Int64Dtype)):
        return 'numeric'
    else:
        return 'any' 
开发者ID:plotly,项目名称:dash-docs,代码行数:24,代码来源:filtering_fe_autotype.py

示例5: test_is_sparse_dataframe

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_is_sparse_dataframe():
    X = data.load_10X(sparse=False)
    Y = X.astype(pd.SparseDtype(float, fill_value=0.0))
    assert scprep.utils.is_sparse_dataframe(Y)

    def test_fun(X):
        assert not scprep.utils.is_sparse_dataframe(X)

    types = (
        matrix._scipy_matrix_types
        + matrix._numpy_matrix_types
        + matrix._pandas_dense_matrix_types
    )
    if matrix._pandas_0:
        types.append(matrix.SparseDataFrame_deprecated)
    matrix.test_matrix_types(
        X, test_fun, types,
    ) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:20,代码来源:test_utils.py

示例6: test_SparseDataFrame

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_SparseDataFrame():
    X = data.load_10X(sparse=False)
    Y = X.astype(pd.SparseDtype(float, fill_value=0.0))
    index = X.index
    columns = X.columns

    def test_fun(X):
        X = scprep.utils.SparseDataFrame(X, index=index, columns=columns)
        utils.assert_matrix_class_equivalent(X, Y)

    matrix.test_all_matrix_types(X, test_fun)
    matrix.test_pandas_matrix_types(
        X,
        utils.assert_transform_equivalent,
        Y=Y,
        transform=scprep.utils.SparseDataFrame,
    ) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:19,代码来源:test_utils.py

示例7: dtype

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def dtype():
    return SparseDtype() 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:4,代码来源:test_sparse.py

示例8: _check_unsupported

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def _check_unsupported(self, data):
        if data.dtype == SparseDtype(int, 0):
            pytest.skip("Can't store nan in int array.") 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:5,代码来源:test_sparse.py

示例9: test_subclass_sparse_slice

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_subclass_sparse_slice(self):
        # int64
        s = tm.SubclassedSparseSeries([1, 2, 3, 4, 5])
        exp = tm.SubclassedSparseSeries([2, 3, 4], index=[1, 2, 3])
        tm.assert_sp_series_equal(s.loc[1:3], exp)
        assert s.loc[1:3].dtype == SparseDtype(np.int64)

        exp = tm.SubclassedSparseSeries([2, 3], index=[1, 2])
        tm.assert_sp_series_equal(s.iloc[1:3], exp)
        assert s.iloc[1:3].dtype == SparseDtype(np.int64)

        exp = tm.SubclassedSparseSeries([2, 3], index=[1, 2])
        tm.assert_sp_series_equal(s[1:3], exp)
        assert s[1:3].dtype == SparseDtype(np.int64)

        # float64
        s = tm.SubclassedSparseSeries([1., 2., 3., 4., 5.])
        exp = tm.SubclassedSparseSeries([2., 3., 4.], index=[1, 2, 3])
        tm.assert_sp_series_equal(s.loc[1:3], exp)
        assert s.loc[1:3].dtype == SparseDtype(np.float64)

        exp = tm.SubclassedSparseSeries([2., 3.], index=[1, 2])
        tm.assert_sp_series_equal(s.iloc[1:3], exp)
        assert s.iloc[1:3].dtype == SparseDtype(np.float64)

        exp = tm.SubclassedSparseSeries([2., 3.], index=[1, 2])
        tm.assert_sp_series_equal(s[1:3], exp)
        assert s[1:3].dtype == SparseDtype(np.float64) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:30,代码来源:test_subclass.py

示例10: ensure_df_homogeneous

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def ensure_df_homogeneous(
    df: pd.DataFrame, name: str
) -> Union[np.ndarray, sparse.csr_matrix]:
    # TODO: rename this function, I would not expect this to return a non-dataframe
    if all(isinstance(dt, pd.SparseDtype) for dt in df.dtypes):
        arr = df.sparse.to_coo().tocsr()
    else:
        arr = df.to_numpy()
    if df.dtypes.nunique() != 1:
        warnings.warn(f"{name} converted to numpy array with dtype {arr.dtype}")
    return arr 
开发者ID:theislab,项目名称:anndata,代码行数:13,代码来源:utils.py

示例11: dataframe_to_sparse

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def dataframe_to_sparse(x, fill_value=0.0):
    return x.astype(pd.SparseDtype(float, fill_value=fill_value)) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:4,代码来源:utils.py

示例12: SparseSeries

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def SparseSeries(X, default_fill_value=0.0):
    return pd.Series(X).astype(pd.SparseDtype(float, fill_value=default_fill_value)) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:4,代码来源:matrix.py

示例13: SparseDataFrame

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
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)) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:9,代码来源:matrix.py

示例14: test_fill_value

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_fill_value():
    values = pd.Series(np.arange(3), dtype=pd.UInt16Dtype())
    custom_block = CustomBlock(values, placement=slice(1, 2))
    assert pd.isna(custom_block.fill_value)
    values = pd.Series(np.arange(3), dtype=pd.SparseDtype(float, 0.0))
    custom_block = CustomBlock(values, placement=slice(1, 2))
    assert not pd.isna(custom_block.fill_value) 
开发者ID:KrishnaswamyLab,项目名称:scprep,代码行数:9,代码来源:test_patch.py

示例15: test_basic_dataframe

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import SparseDtype [as 别名]
def test_basic_dataframe(sparse, method, dask_data, dtype):
    a = sklearn.preprocessing.OneHotEncoder(sparse=sparse, dtype=dtype)
    b = dask_ml.preprocessing.OneHotEncoder(sparse=sparse, dtype=dtype)

    if method == "fit":
        a.fit(df)
        b.fit(dask_data)
        expected = a.transform(df)
        result = b.transform(dask_data)
    else:
        expected = a.fit_transform(df)
        result = b.fit_transform(dask_data)

    assert_estimator_equal(
        a,
        b,
        exclude={
            "n_values_",
            "feature_indices_",
            "active_features_",
            "dtypes_",
            "drop_idx_",
        },
    )

    assert isinstance(result, type(dask_data))
    assert len(result.columns) == expected.shape[1]
    if sparse and PANDAS_VERSION >= packaging.version.parse("0.24.0"):
        # pandas sparse ExtensionDtype interface
        dtype = pd.SparseDtype(dtype, dtype(0))
    assert (result.dtypes == dtype).all()

    da.utils.assert_eq(result.values, expected) 
开发者ID:dask,项目名称:dask-ml,代码行数:35,代码来源:test_encoders.py


注:本文中的pandas.SparseDtype方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。