HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/reshape/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/reshape/__pycache__/concat.cpython-36.pyc
3

���h�V�
@s�dZddlmZddlmZmZmZmZmZm	Z	ddl
Zddlm
Z
mZmZddlmZddlmZmZddlmZmZddljjZdd	lmZmZmZm Z m!Z!m"Z"ddl#jj$j%Z&dd
l'm(Z(er�ddl)m*Z*e	deedeedffe+e,e,e,e,dd�dd��Z-e	deee
eee
ffe+e,e,e,e,ed�dd��Z-deee
eee
ffe,e,e,e,ed�dd�Z-Gdd�d�Z.ed�dd�Z/d ed�dd�Z0dS)!z
Concat routines.
�)�abc)�
TYPE_CHECKING�Iterable�List�Mapping�Union�overloadN)�
FrameOrSeries�FrameOrSeriesUnion�Label)�
concat_compat)�ABCDataFrame�	ABCSeries)�factorize_from_iterable�factorize_from_iterables)�Index�
MultiIndex�all_indexes_same�ensure_index�get_consensus_names�get_objs_combined_axis)�concatenate_block_managers)�	DataFrame�outerFTr)�objs�join�ignore_index�verify_integrity�sort�copy�returnc

CsdS)N�)
r�axisrr�keys�levels�namesrrrr!r!�</tmp/pip-build-5_djhm0z/pandas/pandas/core/reshape/concat.py�concat&s
r'c

CsdS)Nr!)
rr"rrr#r$r%rrrr!r!r&r'6s
)rrrrrr c
Cs$t|||||||||	|d�
}
|
j�S)a�
    Concatenate pandas objects along a particular axis with optional set logic
    along the other axes.

    Can also add a layer of hierarchical indexing on the concatenation axis,
    which may be useful if the labels are the same (or overlapping) on
    the passed axis number.

    Parameters
    ----------
    objs : a sequence or mapping of Series or DataFrame objects
        If a mapping is passed, the sorted keys will be used as the `keys`
        argument, unless it is passed, in which case the values will be
        selected (see below). Any None objects will be dropped silently unless
        they are all None in which case a ValueError will be raised.
    axis : {0/'index', 1/'columns'}, default 0
        The axis to concatenate along.
    join : {'inner', 'outer'}, default 'outer'
        How to handle indexes on other axis (or axes).
    ignore_index : bool, default False
        If True, do not use the index values along the concatenation axis. The
        resulting axis will be labeled 0, ..., n - 1. This is useful if you are
        concatenating objects where the concatenation axis does not have
        meaningful indexing information. Note the index values on the other
        axes are still respected in the join.
    keys : sequence, default None
        If multiple levels passed, should contain tuples. Construct
        hierarchical index using the passed keys as the outermost level.
    levels : list of sequences, default None
        Specific levels (unique values) to use for constructing a
        MultiIndex. Otherwise they will be inferred from the keys.
    names : list, default None
        Names for the levels in the resulting hierarchical index.
    verify_integrity : bool, default False
        Check whether the new concatenated axis contains duplicates. This can
        be very expensive relative to the actual data concatenation.
    sort : bool, default False
        Sort non-concatenation axis if it is not already aligned when `join`
        is 'outer'.
        This has no effect when ``join='inner'``, which already preserves
        the order of the non-concatenation axis.

        .. versionadded:: 0.23.0
        .. versionchanged:: 1.0.0

           Changed to not sort by default.

    copy : bool, default True
        If False, do not copy data unnecessarily.

    Returns
    -------
    object, type of objs
        When concatenating all ``Series`` along the index (axis=0), a
        ``Series`` is returned. When ``objs`` contains at least one
        ``DataFrame``, a ``DataFrame`` is returned. When concatenating along
        the columns (axis=1), a ``DataFrame`` is returned.

    See Also
    --------
    Series.append : Concatenate Series.
    DataFrame.append : Concatenate DataFrames.
    DataFrame.join : Join DataFrames using indexes.
    DataFrame.merge : Merge DataFrames by indexes or columns.

    Notes
    -----
    The keys, levels, and names arguments are all optional.

    A walkthrough of how this method fits in with other tools for combining
    pandas objects can be found `here
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html>`__.

    Examples
    --------
    Combine two ``Series``.

    >>> s1 = pd.Series(['a', 'b'])
    >>> s2 = pd.Series(['c', 'd'])
    >>> pd.concat([s1, s2])
    0    a
    1    b
    0    c
    1    d
    dtype: object

    Clear the existing index and reset it in the result
    by setting the ``ignore_index`` option to ``True``.

    >>> pd.concat([s1, s2], ignore_index=True)
    0    a
    1    b
    2    c
    3    d
    dtype: object

    Add a hierarchical index at the outermost level of
    the data with the ``keys`` option.

    >>> pd.concat([s1, s2], keys=['s1', 's2'])
    s1  0    a
        1    b
    s2  0    c
        1    d
    dtype: object

    Label the index keys you create with the ``names`` option.

    >>> pd.concat([s1, s2], keys=['s1', 's2'],
    ...           names=['Series name', 'Row ID'])
    Series name  Row ID
    s1           0         a
                 1         b
    s2           0         c
                 1         d
    dtype: object

    Combine two ``DataFrame`` objects with identical columns.

    >>> df1 = pd.DataFrame([['a', 1], ['b', 2]],
    ...                    columns=['letter', 'number'])
    >>> df1
      letter  number
    0      a       1
    1      b       2
    >>> df2 = pd.DataFrame([['c', 3], ['d', 4]],
    ...                    columns=['letter', 'number'])
    >>> df2
      letter  number
    0      c       3
    1      d       4
    >>> pd.concat([df1, df2])
      letter  number
    0      a       1
    1      b       2
    0      c       3
    1      d       4

    Combine ``DataFrame`` objects with overlapping columns
    and return everything. Columns outside the intersection will
    be filled with ``NaN`` values.

    >>> df3 = pd.DataFrame([['c', 3, 'cat'], ['d', 4, 'dog']],
    ...                    columns=['letter', 'number', 'animal'])
    >>> df3
      letter  number animal
    0      c       3    cat
    1      d       4    dog
    >>> pd.concat([df1, df3], sort=False)
      letter  number animal
    0      a       1    NaN
    1      b       2    NaN
    0      c       3    cat
    1      d       4    dog

    Combine ``DataFrame`` objects with overlapping columns
    and return only those that are shared by passing ``inner`` to
    the ``join`` keyword argument.

    >>> pd.concat([df1, df3], join="inner")
      letter  number
    0      a       1
    1      b       2
    0      c       3
    1      d       4

    Combine ``DataFrame`` objects horizontally along the x axis by
    passing in ``axis=1``.

    >>> df4 = pd.DataFrame([['bird', 'polly'], ['monkey', 'george']],
    ...                    columns=['animal', 'name'])
    >>> pd.concat([df1, df4], axis=1)
      letter  number  animal    name
    0      a       1    bird   polly
    1      b       2  monkey  george

    Prevent the result from including duplicate index values with the
    ``verify_integrity`` option.

    >>> df5 = pd.DataFrame([1], index=['a'])
    >>> df5
       0
    a  1
    >>> df6 = pd.DataFrame([2], index=['a'])
    >>> df6
       0
    a  2
    >>> pd.concat([df5, df6], verify_integrity=True)
    Traceback (most recent call last):
        ...
    ValueError: Indexes have overlapping values: ['a']
    )	r"rrr#r$r%rrr)�
_Concatenator�
get_result)rr"rrr#r$r%rrr�opr!r!r&r'FsMc	@s�eZdZdZdeeeeeeffe	e
e
e
d�dd	�Zd
d�Ze
d�d
d�Zeed�dd�Ze
ed�dd�Zed�dd�Zed�dd�ZdS)r(zB
    Orchestrates a concatenation operation for BlockManagers
    rrNFT)rrrrrcs�t�tttf�r&tdt��j�d���|dkr6d|_n|dkrFd|_ntd��t�t	j
�r�|dkrnt�j��}�fdd	�|D��nt���t
��d
kr�td��|dkr�ttj����nZg}g}x4t|��D]&\}
}|dkr�q�|j|
�|j|�q�W|�t|dd�}t||d
�}t
��d
k�r&td��t�}xN�D]F}t|ttf��s^dt|��d�}t|��|jdd�|j|j��q2Wd}t
|�dk�r�t|�}x|�D]&}|j|k�r�tj|j��r�|}P�q�WnLdd	��D�}t
|��r|dk�r|dk�r|dk�r|j�r|��d
}|dk�r&�d
}�|_t|t��rF|jj|�}n
|j|�}t|t�|_|j�rn|j |�}t|t�|_!d
|k�o�|jkn�s�t"d|j�d|����t
|�dk�rfd
}|j}g|j|_�x��D]�}|j}||k�r�nd||dk�rtd��nLt|dd�}|�s$|dk�r0|}|d7}|j�rF|dk�rFd
}|j#||i�}|jj|��q�W||_$|j�r~d|j$nd
|_%||_|�p�t|dd�|_&||_'|
|_(||_)||_*|	|_+|j,�|_-dS)NzTfirst argument must be an iterable of pandas objects, you passed an object of type "�"rF�innerTz?Only can inner (intersect) or outer (union) join the other axiscsg|]}�|�qSr!r!)�.0�k)rr!r&�
<listcomp>Fsz*_Concatenator.__init__.<locals>.<listcomp>rzNo objects to concatenate�name)r0zAll objects passed were Nonez#cannot concatenate object of type 'z+'; only Series and DataFrame objs are valid)Zinplace�cSs(g|] }t|j�dks t|t�r|�qS)r)�sum�shape�
isinstancer)r-�objr!r!r&r/|szaxis must be between 0 and z, input was z>cannot concatenate unaligned mixed dimensional NDFrame objectsr%).r4rr
�str�	TypeError�type�__name__�	intersect�
ValueErrorrr�listr#�len�comZnot_none�zip�append�getattrr�setZ_consolidate�add�ndim�max�npr2r3r�_constructor_expanddimZ_get_axis_numberZ	_is_frame�_get_block_manager_axis�
_is_series�AssertionError�_constructor�bm_axisr"r%r$rrrr�
_get_new_axes�new_axes)�selfrr"rr#r$r%rrrrZ
clean_keysZ
clean_objsr.�vr0Zndimsr5�msg�sampleZmax_ndimZnon_emptiesZcurrent_columnrDr!)rr&�__init__'s�





(




z_Concatenator.__init__cCsz|jr�|jdkrjtj|j�}|jdj}dd�|jD�}t|dd�}|||jd||jd�}|j	|dd�St
ttt
|j��|j��}|jdj}|j\}}|||d�}	||	_|	j	|dd�Sn�g}
xp|jD]f}i}xLt|j�D]>\}
}|
|jkr�q�|jd	|
}|j|�s�|j|�d	||
<q�W|
j|j|f�q�Wt|
|j|j|jd
�}|j�sX|j�|jdj}||�j	|dd�SdS)NrcSsg|]
}|j�qSr!)Z_values)r-Zserr!r!r&r/�sz,_Concatenator.get_result.<locals>.<listcomp>)r")�indexr0�dtyper')�method)rTr1)�concat_axisr)rIrLr>Zconsensus_name_attrrrKrrNrUZ__finalize__�dictr?�ranger=rG�columns�	enumerate�axes�equalsZreindexr@Z_mgrrrZ_consolidate_inplace)rOr0�consZarrs�res�result�datarTrZZdfZ
mgrs_indexersr5ZindexersZaxZ
new_labelsZ
obj_labelsZnew_datar!r!r&r)�s<



z_Concatenator.get_result)r cCs$|jr|jdkrdS|jdjSdS)Nr1�r)rIrLrrD)rOr!r!r&�_get_result_dim�sz_Concatenator._get_result_dimcs�j�}�fdd�t|�D�S)Ncs(g|] }|�jkr�j�n�j|��qSr!)rL�_get_concat_axis�_get_comb_axis)r-�i)rOr!r&r/sz/_Concatenator._get_new_axes.<locals>.<listcomp>)rcrY)rOrDr!)rOr&rMs
z_Concatenator._get_new_axes)rfr cCs*|jdj|�}t|j||j|j|jd�S)Nr)r"r:rr)rrHrr:rr)rOrfZ	data_axisr!r!r&resz_Concatenator._get_comb_axisc	sb�jr�jdkr"dd��jD�}nΈjr<tjt�j��}|S�jdkr�dgt�j�}d}d}x`t�j�D]R\}}t	|t
�s�tdt|�j
�d���|jdk	r�|j||<d}qj|||<|d	7}qjW|r�t|�Stjt�j��Snt�j�j�j�Sn�fd
d��jD�}�j�r*tjtdd�|D���}|S�jdk�r@t|�}nt|�j�j�j�}�j|�|S)
zC
        Return index to be used along concatenation axis.
        rcSsg|]
}|j�qSr!)rT)r-�xr!r!r&r/sz2_Concatenator._get_concat_axis.<locals>.<listcomp>NFz6Cannot concatenate type 'Series' with object of type '�'Tr1csg|]}|j�j�qSr!)r\r")r-rg)rOr!r&r/7scss|]}t|�VqdS)N)r=)r-rfr!r!r&�	<genexpr>:sz1_Concatenator._get_concat_axis.<locals>.<genexpr>)rIrLrr�ibaseZ
default_indexr=r#r[r4rr7r8r9r0rrZ	set_namesr%r2�_concat_indexes�_make_concat_multiindexr$�_maybe_check_integrity)	rO�indexes�idxr%�numZ	has_namesrfrgrWr!)rOr&rdsB






z_Concatenator._get_concat_axis)�concat_indexcCs.|jr*|js*||j�j�}td|����dS)Nz!Indexes have overlapping values: )rZ	is_uniqueZ
duplicated�uniquer;)rOrq�overlapr!r!r&rmHsz$_Concatenator._maybe_check_integrity)	rrNNNFFTF)r9�
__module__�__qualname__�__doc__rrr	rrr6�boolrSr)�intrcrrrMrerdrmr!r!r!r&r("s",7
2r()r cCs|dj|dd��S)Nrr1)r@)rnr!r!r&rkOsrkcs|dkrt|dt�s*|dk	rrt|�dkrrtt|��}|dkrLdgt|�}|dkrbt|�\}}q�dd�|D�}n6|g}|dkr�dg}|dkr�t|�g}ndd�|D�}t|��s�g}x�t||�D]�\}}g}	xht||�D]Z\}
}||
k}|j��st	d|
�d|����t
j||
k�dd}
|	jt
j
|
t|���q�W|jt
j|	��q�Wt|�}t|t��rz|j|j�|j|j�n t|�\}}|j|�|j|�t|�t|�k�r�t|�}n,tdd	�|D��dk�s�td
��|t|�}t|||dd�S|d}t|�}t|��t|�}t|�}g}x`t||�D]R\}}t|�}|j|�}|dk}|j��rht	d
||����|jt
j
||���q*Wt|t��r�|j|j�|j�fdd�|jD��n"|j|�|jt
jt
j|����t|�t|�k�r�|j|j�t|||dd�S)Nrr1cSsg|]}t|��qSr!)r)r-rgr!r!r&r/_sz+_make_concat_multiindex.<locals>.<listcomp>cSsg|]}t|��qSr!)r)r-rgr!r!r&r/hszKey z not in level cSsh|]
}|j�qSr!)Znlevels)r-ror!r!r&�	<setcomp>�sz*_make_concat_multiindex.<locals>.<setcomp>z@Cannot concat indices that do not have the same number of levelsF)r$�codesr%rz"Values not found in passed level: csg|]}tj|���qSr!)rF�tile)r-Zlab)�kpiecesr!r&r/�s���)r4�tupler=r<r?rrr�anyr;rFZnonzeror@�repeatZconcatenaterkr�extendr$rzrrJrZget_indexerr{Zaranger%)rnr#r$r%Zzipped�_Z
codes_listZhlevel�levelZ	to_concat�keyrT�maskrfrqrz�
categoriesZ	new_index�nZ	new_namesZ
new_levelsZ	new_codesZmappedr!)r|r&rlSs|







rl)	rrFNNNFFT)	rrFNNNFFT)	rrFNNNFFT)NN)1rv�collectionsrZtypingrrrrrrZnumpyrFZpandas._typingr	r
rZpandas.core.dtypes.concatrZpandas.core.dtypes.genericr
rZpandas.core.arrays.categoricalrrZpandas.core.common�core�commonr>Zpandas.core.indexes.apirrrrrrZpandas.core.indexes.basern�baserjZpandas.core.internalsrZpandasrr6rwr'r(rkrlr!r!r!r&�<module>sf  "" R/