[docs]@singledispatchdefcombine_rows(*x:Any):"""Combine n-dimensional objects along their first 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.vstack`. If all elements are :py:class:`~pandas.DataFrame` objects, they are combined using :py:func:`~pandas.concat` along the first axis. Args: x: One or more 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_rows` method implemented for '"+type(x[0]).__name__+"' objects")
@combine_rows.register(numpy.ndarray)def_combine_rows_dense_arrays(*x:numpy.ndarray):_check_array_dimensions(x,active=0)x=[convert_to_dense(y)foryinx]foryinx:ifnumpy.ma.is_masked(y):returnnumpy.ma.concatenate(x)returnnumpy.concatenate(x)ifis_package_installed("scipy"):importscipy.sparseasspdef_combine_rows_sparse_matrices(*x):_check_array_dimensions(x,0)ifis_list_of_type(x,sp.spmatrix):combined=sp.vstack(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)try:combine_rows.register(sp.sparray,_combine_rows_sparse_arrays)exceptException:passdef_combine_rows_sparse_arrays(*x):_check_array_dimensions(x,0)ifis_list_of_type(x,sp.sparray):combined=sp.vstack(x)return_coerce_sparse_array(first,combined,sp)warn("not all elements are SciPy sparse arrays")x=[convert_to_dense(y)foryinx]returnnumpy.concatenate(x)try:combine_rows.register(sp.spmatrix,_combine_rows_sparse_matrices)exceptException:passifis_package_installed("pandas"):frompandasimportDataFrame,concat@combine_rows.register(DataFrame)def_combine_rows_pandas_dataframe(*x):returnconcat(x,axis=0)