[docs]@singledispatchdefcombine_columns(*x:Any):"""Combine n-dimensional objects along the second dimension. If all elements are :py:class:`~numpy.ndarray`, we combine them using numpy's :py:func:`~numpy.concatenate`. If all elements are either :py:class:`~scipy.sparse.spmatrix` or :py:class:`~scipy.sparse.sparray`, these objects are combined using scipy's :py:class:`~scipy.sparse.hstack`. If all elements are :py:class:`~pandas.DataFrame` objects, they are combined using :py:func:`~pandas.concat` along the second axis. Args: x: n-dimensional objects to combine. All elements of x are expected to be the same class. Returns: Combined object, typically the same type as the first entry of ``x`` """raiseNotImplementedError("no `combine_columns` method implemented for '"+type(x[0]).__name__+"' objects")
@combine_columns.registerdef_combine_columns_dense_arrays(*x:numpy.ndarray):_check_array_dimensions(x,active=1)x=[convert_to_dense(y)foryinx]foryinx:ifnumpy.ma.is_masked(y):returnnumpy.ma.concatenate(x,axis=1)returnnumpy.concatenate(x,axis=1)ifis_package_installed("scipy")isTrue:importscipy.sparseasspdef_combine_columns_sparse_matrices(*x):_check_array_dimensions(x,1)ifis_list_of_type(x,sp.spmatrix):combined=sp.hstack(x)return_coerce_sparse_matrix(x[0],combined,sp)warn("not all elements are scipy sparse matrices")x=[convert_to_dense(y)foryinx]returnnumpy.concatenate(x,axis=1)try:combine_columns.register(sp.spmatrix,_combine_columns_sparse_matrices)exceptException:passdef_combine_columns_sparse_arrays(*x):_check_array_dimensions(x,1)ifis_list_of_type(x,sp.sparray):combined=sp.hstack(x)return_coerce_sparse_array(x[0],combined,sp)warn("not all elements are scipy sparse arrays")x=[convert_to_dense(y)foryinx]returnnumpy.concatenate(x,axis=1)ifis_package_installed("pandas")isTrue:frompandasimportDataFrame,concat@combine_columns.register(DataFrame)def_combine_columns_pandas_dataframe(*x):returnconcat(x,axis=1)