示例#1
0
def _df_append_right(self, df_or_s):
    misc.start(
        'appending to the right.  note, this is a destructive operation')
    if (type(df_or_s) is scipy.sparse.coo.coo_matrix):
        self_sparse = None
        for c in self.columns:
            misc.debug('\tappending column: ' + c)
            c_coo = scipy.sparse.coo_matrix(self[[c]])
            self.drop([c], 1, inplace=True)
            gc.collect()
            if self_sparse == None: self_sparse = c_coo
            else: self_sparse = scipy.sparse.hstack((self_sparse, c_coo))
        self_sparse = scipy.sparse.hstack((self_sparse, df_or_s))
        misc.stop('done appending to the right')
        return self_sparse
    elif utils.is_sparse(df_or_s) and not utils.is_sparse(self):
        misc.debug('converting data frame to a sparse frame')
        self = self.to_sparse(fill_value=0)
    if type(df_or_s) is pd.Series: self[df_or_s.name] = df_or_s.values
    else:
        if type(df_or_s) is pd.DataFrame:
            columns = df_or_s.columns.tolist()
            right = df_or_s.values
        else:
            columns = [ ` i ` + '_2' for i in range(df_or_s.shape[1])]
            right = df_or_s
示例#2
0
def _df_append_fit_transformer(self, fit_transformer, method='transform'):
    misc.start('append_fit_transformer')
    if 'fit' not in method: fit_transformer.fit(self)
    new_X = getattr(fit_transformer, method)(self)
    if utils.is_sparse(new_X): new_X = new_X.todense()
    columns = map(lambda i: 'n_new_col_' + ` i `, range(new_X.shape[1]))
    new_df = pd.DataFrame(new_X, columns=columns)
    X = self.copy().append_right(new_df)
    misc.start('done append_fit_transformer')
    return X