當前位置: 首頁>>代碼示例>>Python>>正文


Python pandas.SparseDataFrame方法代碼示例

本文整理匯總了Python中pandas.SparseDataFrame方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.SparseDataFrame方法的具體用法?Python pandas.SparseDataFrame怎麽用?Python pandas.SparseDataFrame使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas的用法示例。


在下文中一共展示了pandas.SparseDataFrame方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_concat_sparse_dense_rows

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def test_concat_sparse_dense_rows(self, fill_value, sparse_idx, dense_idx):
        frames = [self.dense1, self.dense2]
        sparse_frame = [frames[dense_idx],
                        frames[sparse_idx].to_sparse(fill_value=fill_value)]
        dense_frame = [frames[dense_idx], frames[sparse_idx]]

        # This will try both directions sparse + dense and dense + sparse
        for _ in range(2):
            res = pd.concat(sparse_frame)
            exp = pd.concat(dense_frame)

            assert isinstance(res, pd.SparseDataFrame)
            tm.assert_frame_equal(res.to_dense(), exp)

            sparse_frame = sparse_frame[::-1]
            dense_frame = dense_frame[::-1] 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_combine_concat.py

示例2: test_to_frame

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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

示例3: test_constructor_ndarray

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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

示例4: test_constructor_from_series

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def test_constructor_from_series(self):

        # GH 2873
        x = Series(np.random.randn(10000), name='a')
        x = x.to_sparse(fill_value=0)
        assert isinstance(x, SparseSeries)
        df = SparseDataFrame(x)
        assert isinstance(df, SparseDataFrame)

        x = Series(np.random.randn(10000), name='a')
        y = Series(np.random.randn(10000), name='b')
        x2 = x.astype(float)
        x2.loc[:9998] = np.NaN
        # TODO: x_sparse is unused...fix
        x_sparse = x2.to_sparse(fill_value=np.NaN)  # noqa

        # Currently fails too with weird ufunc error
        # df1 = SparseDataFrame([x_sparse, y])

        y.loc[:9998] = 0
        # TODO: y_sparse is unsused...fix
        y_sparse = y.to_sparse(fill_value=0)  # noqa
        # without sparse value raises error
        # df2 = SparseDataFrame([x2_sparse, y]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:26,代碼來源:test_frame.py

示例5: test_dense_to_sparse

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def test_dense_to_sparse(self):
        df = DataFrame({'A': [nan, nan, nan, 1, 2],
                        'B': [1, 2, nan, nan, nan]})
        sdf = df.to_sparse()
        assert isinstance(sdf, SparseDataFrame)
        assert np.isnan(sdf.default_fill_value)
        assert isinstance(sdf['A'].sp_index, BlockIndex)
        tm.assert_frame_equal(sdf.to_dense(), df)

        sdf = df.to_sparse(kind='integer')
        assert isinstance(sdf['A'].sp_index, IntIndex)

        df = DataFrame({'A': [0, 0, 0, 1, 2],
                        'B': [1, 2, 0, 0, 0]}, dtype=float)
        sdf = df.to_sparse(fill_value=0)
        assert sdf.default_fill_value == 0
        tm.assert_frame_equal(sdf.to_dense(), df) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:19,代碼來源:test_frame.py

示例6: test_astype_bool

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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_isna

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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

示例8: test_notna

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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

示例9: test_comparison_op_scalar

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def test_comparison_op_scalar(self):
        # GH 13001
        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()

        # comparison changes internal repr, compare with dense
        res = sparse > 1
        assert isinstance(res, pd.SparseDataFrame)
        tm.assert_frame_equal(res.to_dense(), df > 1)

        res = sparse != 0
        assert isinstance(res, pd.SparseDataFrame)
        tm.assert_frame_equal(res.to_dense(), df != 0) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_frame.py

示例10: test_from_scipy_correct_ordering

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [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: get_node_id_feature_sparse

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def get_node_id_feature_sparse(self,X):


        pool = ThreadPool(40)
        #results = map(self.get_feaure, np.array(X.values))
        results = pool.map(self.get_feaure, np.array(X.values))

        results = list(results)
        #print(results)
        #results = np.array(results)
        #print(results)
        results = pd.DataFrame(results)

        print(results.columns)
        print("-------------")
        results = pd.SparseDataFrame(pd.get_dummies(results)).astype("float")



        print(results)

        # columns = results.columns
        # results = scipy.sparse.csr_matrix(results)
        print(results.columns)
        return results 
開發者ID:DominickZhang,項目名稱:KDDCup2019_admin,代碼行數:27,代碼來源:InferenceLightGBM.py

示例12: setup_method

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def setup_method(self, method):
        self.cols = ['string', 'int', 'float', 'object']

        self.string_series = pd.SparseSeries(['a', 'b', 'c'])
        self.int_series = pd.SparseSeries([1, 2, 3])
        self.float_series = pd.SparseSeries([1.1, 1.2, 1.3])
        self.object_series = pd.SparseSeries([[], {}, set()])
        self.sdf = pd.SparseDataFrame({
            'string': self.string_series,
            'int': self.int_series,
            'float': self.float_series,
            'object': self.object_series,
        })
        self.sdf = self.sdf[self.cols]
        self.ss = pd.SparseSeries(['a', 1, 1.1, []], index=self.cols) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:17,代碼來源:test_indexing.py

示例13: test_frame_indexing_multiple

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def test_frame_indexing_multiple(self):
        tm.assert_sp_frame_equal(self.sdf, self.sdf[:])
        tm.assert_sp_frame_equal(self.sdf, self.sdf.loc[:])
        tm.assert_sp_frame_equal(self.sdf.iloc[[1, 2]],
                                 pd.SparseDataFrame({
                                     'string': self.string_series.iloc[[1, 2]],
                                     'int': self.int_series.iloc[[1, 2]],
                                     'float': self.float_series.iloc[[1, 2]],
                                     'object': self.object_series.iloc[[1, 2]]
                                 }, index=[1, 2])[self.cols])
        tm.assert_sp_frame_equal(self.sdf[['int', 'string']],
                                 pd.SparseDataFrame({
                                     'int': self.int_series,
                                     'string': self.string_series,
                                 })) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:17,代碼來源:test_indexing.py

示例14: sparse_df

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
def sparse_df():
    return pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}})  # eye 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:4,代碼來源:test_reshape.py

示例15: test_sparse_repr_after_set

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import SparseDataFrame [as 別名]
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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:13,代碼來源:test_format.py


注:本文中的pandas.SparseDataFrame方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。