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


Python testing.assert_sp_frame_equal方法代码示例

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


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

示例1: test_getitem

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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()) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_indexing.py

示例2: test_take_fill_value

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:21,代码来源:test_indexing.py

示例3: test_reindex

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:24,代码来源:test_indexing.py

示例4: test_to_frame

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_series.py

示例5: test_constructor_ndarray

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:24,代码来源:test_frame.py

示例6: test_astype_bool

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:26,代码来源:test_frame.py

示例7: test_transpose

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_frame.py

示例8: test_isna

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_frame.py

示例9: test_notna

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_frame.py

示例10: test_from_scipy_correct_ordering

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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()) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_to_from_scipy.py

示例11: test_subclass_sparse_to_frame

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:25,代码来源:test_subclass.py

示例12: test_subclass_sparse_slice

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:25,代码来源:test_subclass.py

示例13: test_getitem_fill_value

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
def test_getitem_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)

        result = sparse[['z']]
        expected = orig[['z']].to_sparse(fill_value=0)
        tm.assert_sp_frame_equal(result, expected, check_fill_value=False)

        tm.assert_sp_series_equal(sparse['y'],
                                  orig['y'].to_sparse(fill_value=0))

        exp = orig[['x']].to_sparse(fill_value=0)
        exp._default_fill_value = np.nan
        tm.assert_sp_frame_equal(sparse[['x']], exp)

        exp = orig[['z', 'x']].to_sparse(fill_value=0)
        exp._default_fill_value = np.nan
        tm.assert_sp_frame_equal(sparse[['z', 'x']], exp)

        indexer = [True, False, True, True]
        exp = orig[indexer].to_sparse(fill_value=0)
        exp._default_fill_value = np.nan
        tm.assert_sp_frame_equal(sparse[indexer], exp)

        exp = orig.iloc[[1, 2]].to_sparse(fill_value=0)
        exp._default_fill_value = np.nan
        tm.assert_sp_frame_equal(sparse.iloc[[1, 2]], exp) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:33,代码来源:test_indexing.py

示例14: test_loc_slice

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
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()) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:test_indexing.py

示例15: test_iloc_slice

# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_sp_frame_equal [as 别名]
def test_iloc_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.iloc[2:], orig.iloc[2:].to_sparse()) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:test_indexing.py


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