B
    W0dx                 @  s  d dl mZ d dlZd dlmZmZ d dlZd dlm	  m
Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZ d d	lmZ d d
l m!Z! d dl"m#  m$Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5m6Z6m7Z7m8Z8 er>d dl&m9Z9 G dd dZ:d8ddZ;d9ddZ<d:ddZ=dd Z>d;dd Z?d<d!d"Z@d#d#d$d%d&ZAd=d'd(ZBd>d+d+d+d,d-d.d/d0ZCd?d+d+d+d,d-d.d1d2ZDd3d4d4d3d5d6d7ZEdS )@    )annotationsN)TYPE_CHECKINGcast)IntIndex)Dtype)cache_readonly)maybe_promote)	ensure_platform_intis_1d_only_ea_dtypeis_bool_dtypeis_extension_array_dtype
is_integeris_integer_dtypeis_list_likeis_object_dtypeneeds_i8_conversion)ExtensionDtype)notna)SparseArray)factorize_from_iterable)ensure_wrapped_if_datetimelike)	DataFrame)Index
MultiIndex)Series)compress_group_indexdecons_obs_group_idsget_compressed_idsget_group_indexget_group_index_sorter)ExtensionArrayc               @  s|   e Zd ZdZdddddZedd	d
dZedd ZdddddZdd Z	dd Z
dddZdd Zedd ZdS )
_Unstackera	  
    Helper class to unstack data / pivot with multi-level index

    Parameters
    ----------
    index : MultiIndex
    level : int or str, default last level
        Level to "unstack". Accepts a name for the level.
    fill_value : scalar, optional
        Default value to fill in missing values if subgroups do not have the
        same set of labels. By default, missing values will be replaced with
        the default fill value for that data type, NaN for float, NaT for
        datetimelike, etc. For integer types, by default data will converted to
        float and missing values will be set to NaN.
    constructor : object
        Pandas ``DataFrame`` or subclass used to create unstacked
        response.  If None, DataFrame will be used.

    Examples
    --------
    >>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
    ...                                    ('two', 'a'), ('two', 'b')])
    >>> s = pd.Series(np.arange(1, 5, dtype=np.int64), index=index)
    >>> s
    one  a    1
         b    2
    two  a    3
         b    4
    dtype: int64

    >>> s.unstack(level=-1)
         a  b
    one  1  2
    two  3  4

    >>> s.unstack(level=0)
       one  two
    a    1    3
    b    2    4

    Returns
    -------
    unstacked : DataFrame
    Nr   )indexc             C  s   |d krt }|| _| | _| j|| _d| jj| j kr@dnd| _t| jj	| _
t| jj| _| j| j| _| j
| j| _|j	| j | _tdd | j
D }| jj}tj||tjd}|dkr|dkr|dkrtd|   d S )Nr"      r   c             S  s   g | ]
}|j qS  )size).0Zindex_levelr%   r%   M/var/www/html/venv/lib/python3.7/site-packages/pandas/core/reshape/reshape.py
<listcomp>|   s    z'_Unstacker.__init__.<locals>.<listcomp>)dtypez6Unstacked DataFrame is too big, causing int32 overflow)r   constructorZremove_unused_levelsr#   _get_level_numberlevelcodesliftlistlevelsnew_index_levelsnamesnew_index_namespopremoved_nameremoved_levelremoved_level_fullnpmaxr&   multiplyZint32
ValueError_make_selectors)selfr#   r-   r+   Znum_rowsZnum_columnsZ	num_cellsr%   r%   r(   __init__c   s"    
z_Unstacker.__init__z#tuple[np.ndarray, list[np.ndarray]])returnc       
      C  s   | j }t| jj}t| jj}|d | ||d d   || g }tdd |d | ||d d   || g D }t||\}}t|}t||}	|	|fS )Nr$   c             s  s   | ]}t |V  qd S )N)len)r'   xr%   r%   r(   	<genexpr>   s    z2_Unstacker._indexer_and_to_sort.<locals>.<genexpr>)	r-   r0   r#   r.   r1   tupler   rA   r   )
r>   vr.   levsto_sortsizes
comp_indexobs_idsngroupsindexerr%   r%   r(   _indexer_and_to_sort   s    &4
z_Unstacker._indexer_and_to_sortc               s   | j \ } fdd|D S )Nc               s   g | ]}|  qS r%   )take)r'   line)rL   r%   r(   r)      s    z,_Unstacker.sorted_labels.<locals>.<listcomp>)rM   )r>   rG   r%   )rL   r(   sorted_labels   s    
z_Unstacker.sorted_labelsz
np.ndarray)valuesr@   c             C  s   | j \}}tj||dd}|S )Nr   )axis)rM   algosZtake_nd)r>   rQ   rL   _sorted_valuesr%   r%   r(   _make_sorted_values   s    
z_Unstacker._make_sorted_valuesc       
      C  s   | j }| jd d }tdd |D }t||\}}t|}t|}| jj| j | j	 }||f| _
| jd ||  | j	 }tjt| j
td}	|	|d |	 t| jk rtd|| _|	| _|| _|t|| _d S )Nr"   c             s  s   | ]}t |V  qd S )N)rA   )r'   rB   r%   r%   r(   rC      s    z-_Unstacker._make_selectors.<locals>.<genexpr>)r*   Tz0Index contains duplicate entries, cannot reshape)r2   rP   rD   r   rA   r	   r#   Zlevshaper-   r/   
full_shaper9   zerosprodboolputsumr<   group_indexmaskZunique_groupsZsearchsortedarange
compressor)
r>   
new_levelsZremaining_labelsZlevel_sizesrI   rJ   rK   strideselectorr^   r%   r%   r(   r=      s"    
z_Unstacker._make_selectorsc             C  sj   |j dkr|d d tjf }|d kr:|jd dkr:td| ||\}}| |}| j}| j|||dS )Nr$   z-must pass column labels for multi-column data)r#   columns)	ndimr9   newaxisshaper<   get_new_valuesget_new_columns	new_indexr+   )r>   rQ   value_columns
fill_valuerT   rd   r#   r%   r%   r(   
get_result   s    

z_Unstacker.get_resultc             C  s  |j dkr|d d tjf }| |}| j\}}|jd }|| }||f}| j}	|	 }
|
rt|r|	|||
dd	|}tj|td}||fS |
r|j}tj||d}t|j}njt|j|\}}t|tr| }|j||d}||d d < |j}n$tj||d}|| t|j}tj|td}t|jrV|d}|d}n0t|jrx|d}|d}n|j|dd}t||	d|||||d t|jr|d	}t|}||j}||fS )
Nr$      )r*   i8objectF)copyu1zM8[ns])re   r9   rf   rV   rW   rg   r^   allrA   reshapeZswapaxesonesrZ   r*   emptynamer   
isinstancer   construct_array_type_emptyfillrX   r   viewr   Zastype
libreshapeunstackr   )r>   rQ   rl   rU   lengthwidthrb   Zresult_widthZresult_shaper^   Zmask_all
new_valuesZnew_maskr*   rw   clsr%   r%   r(   rh      sb    







z_Unstacker.get_new_valuesc       	        s.  |d krB| j dkr"| jj| jdS | jjd| jjd}|| jS t| j| j  }t|}t	t
|| t|tr|j| jf }|j| jf } fdd|jD }n|| jg}|j| jg} g}t| jt| jkr| j| j}| j rt|dd}nt
|| j  }|t|| t|||ddS )	Nr   )rw   )itemc               s   g | ]}|  qS r%   )rN   )r'   lab)
propagatorr%   r(   r)   .  s    z._Unstacker.get_new_columns.<locals>.<listcomp>r"   F)r1   r.   r3   verify_integrity)r/   r7   _renamer6   insert	_na_valuerenamerA   r9   repeatr_   rx   r   r1   r8   r3   r.   rw   Zget_indexerappendtile)	r>   rk   levrb   r   ra   	new_names	new_codesZrepeaterr%   )r   r(   ri     s.    


z_Unstacker.get_new_columnsc               s    fdd j d d D }t jdkrr jd |d  }}|dk r\|t||j}|| jd S t	 j| jddS )Nc               s   g | ]}|  jqS r%   )rN   r`   )r'   r   )r>   r%   r(   r)   G  s    z(_Unstacker.new_index.<locals>.<listcomp>r"   r$   r   F)r1   r.   r3   r   )
rP   rA   r2   anyr   r   rN   r   r4   r   )r>   Zresult_codesr-   level_codesr%   )r>   r(   rj   D  s    z_Unstacker.new_index)r"   N)N)__name__
__module____qualname____doc__r?   r   rM   rP   rV   r=   rm   rh   ri   rj   r%   r%   r%   r(   r!   5   s   ,$
Q%r!   c               s  t  dkr| S | j jkr& g fdd D   fddtjD }fdd D }fdd D }fdd D }fdd|D }fd	d|D }fd
d|D }	tdd |D }
t||
ddd}t|dd\}}t|||
|dd}|st	|dd}n"t
||g ||g |	dg dd}t| trl|  }||_|jd|d}|}|}|}nt| jt
r| }x>tt  D ].} | |j|d}fdd D  qW |S | jdd}||_|jd|d}t|tr|j}n|j}t|t
st|jd g| }| jjg| }|jd g}x$|D ]}|||jd  q<W t
|||dd}t|tr||_n||_|S )Nr   c               s   g | ]}  |qS r%   )r,   )r'   i)r#   r%   r(   r)   d  s    z%_unstack_multiple.<locals>.<listcomp>c               s   g | ]}| kr|qS r%   r%   )r'   r   )clocsr%   r(   r)   f  s    c               s   g | ]} j | qS r%   )r1   )r'   r   )r#   r%   r(   r)   h  s    c               s   g | ]} j | qS r%   )r.   )r'   r   )r#   r%   r(   r)   i  s    c               s   g | ]} j | qS r%   )r3   )r'   r   )r#   r%   r(   r)   j  s    c               s   g | ]} j | qS r%   )r1   )r'   r   )r#   r%   r(   r)   k  s    c               s   g | ]} j | qS r%   )r.   )r'   r   )r#   r%   r(   r)   l  s    c               s   g | ]} j | qS r%   )r3   )r'   r   )r#   r%   r(   r)   m  s    c             s  s   | ]}t |V  qd S )N)rA   )r'   rB   r%   r%   r(   rC   o  s    z$_unstack_multiple.<locals>.<genexpr>F)sortxnull)r   )r   __placeholder__)rw   )r1   r.   r3   r   )rl   c               s    g | ]}| k r|n|d  qS )r$   r%   )r'   rE   )valr%   r(   r)     s    )deepr"   )rA   r#   r3   rangenlevelsrD   r   r   r   r   r   rx   r   rq   r~   rd   AssertionErrorr1   rw   r.   r   rN   )datar   rl   ZrlocsZclevelsZccodesZcnamesZrlevelsZrcodesZrnamesrg   r]   Zcomp_idsrJ   Zrecons_codesZdummy_indexdummyZ	unstackedra   r   r   resultr   ZunstcolsZrecnew_columnsr%   )r   r#   r   r(   _unstack_multipleX  sp    

r   c             C  s   t |ttfr0t|dkr(t| ||dS |d }t|sL|dksL| j|}t | trt | jt	rpt
| ||dS | jjddS n^t | jt	stdt| j dn:t| jrt| ||S t| j|| jd	}|j| jd |d
S d S )Nr$   )rl   r   r   F)dropnaz'index must be a MultiIndex to unstack, z was passed)r-   r+   )rk   rl   )rx   rD   r0   rA   r   r   r#   r,   r   r   _unstack_frameTstackr<   typer
   r*   _unstack_extension_seriesr!   Z_constructor_expanddimrm   _values)objr-   rl   	unstackerr%   r%   r(   r~     s&    

r~   c             C  sX   | j s.t| j|d}| jj||d}| |S t| j|| jd}|j| j| j|dS d S )N)r-   )rl   )r-   r+   )rk   rl   )	Z_can_fast_transposer!   r#   Z_mgrr~   _constructorrm   r   rd   )r   r-   rl   r   Zmgrr%   r%   r(   r     s    
r   c             C  s$   |   }|j||d}|jdddS )a'  
    Unstack an ExtensionArray-backed Series.

    The ExtensionDtype is preserved.

    Parameters
    ----------
    series : Series
        A Series with an ExtensionArray for values
    level : Any
        The level name or number.
    fill_value : Any
        The user-level (not physical storage) fill value to use for
        missing values introduced by the reshape. Passed to
        ``series.values.take``.

    Returns
    -------
    DataFrame
        Each column of the DataFrame will have the same dtype as
        the input Series.
    )r-   rl   r   r$   )r-   rR   )Zto_framer~   Z	droplevel)Zseriesr-   rl   Zdfr   r%   r%   r(   r     s    r   r"   Tc               s  dd }| j \} | j|}t| jtr8t| ||dS t| jtrt| jj} fdd| jj	D }|| j\}}	|
| |
t|	|  t| jj}
|

| jj t|||
dd}nTtt|| j| jf \}\}}	| t|	| f}t||| jj| jjgdd}| js|| jr|t| jj}|d }t|rp| }|d	d |  D }t|| }n
| j }n
| j }|rt|}|| }|| }| j||d
S )z
    Convert DataFrame to Series with multi-level Index. Columns become the
    second level of the resulting hierarchical index

    Returns
    -------
    stacked : Series
    c             S  s,   | j r| tt| fS t| \}}||fS )N)Z	is_uniquer9   r_   rA   r   )r#   r.   
categoriesr%   r%   r(   	factorize  s    zstack.<locals>.factorize)	level_numr   c               s   g | ]}|  qS r%   )r   )r'   r   )Kr%   r(   r)     s    zstack.<locals>.<listcomp>F)r1   r.   r3   r   r   c             S  s   g | ]\}}|j qS r%   )r   )r'   rT   colr%   r%   r(   r)   4  s    )r#   )rg   rd   r,   rx   r   _stack_multi_columnsr#   r0   r1   r.   r   r9   r   ravelr3   rw   zipmapr   rv   _is_homogeneous_typedtypesr   r   ry   _concat_same_typeitems"_reorder_for_extension_array_stackr   Z_constructor_sliced)framer-   r   r   Nr   ra   r   ZclevZclabr   rj   r1   Zilabr.   r   r*   arrr   r^   r%   )r   r(   r     sJ    




r   c               s   t  fdd|D r8 }x|D ]}t|||d}q W nt dd |D rƈ } fdd|D }xltt|D ]R}|| }t|||d}g }x.|D ]&}||kr||d  q|| qW |}qnW ntd|S )	Nc             3  s   | ]}| j jkV  qd S )N)rd   r3   )r'   r   )r   r%   r(   rC   J  s    z!stack_multiple.<locals>.<genexpr>)r   c             s  s   | ]}t |tV  qd S )N)rx   int)r'   r   r%   r%   r(   rC   P  s    c               s   g | ]} j |qS r%   )rd   r,   )r'   r   )r   r%   r(   r)   V  s    z"stack_multiple.<locals>.<listcomp>r$   zTlevel should contain all level names or all level numbers, not a mixture of the two.)rs   r   r   rA   r   r<   )r   r-   r   r   r   r#   Zupdated_levelotherr%   )r   r(   stack_multipleG  s&    


r   r   )rd   r@   c             C  s   t | jdkr&| jd j| jd dS dd t| jdd | jdd D }t| }dd	 t|D }t| }tj	d
d t|| jD | jdd dS )zBCreates a MultiIndex from the first N-1 levels of this MultiIndex.rn   r   )rw   c               s"   g | ]\ } fd d|D qS )c               s    g | ]}|d kr | ndqS )r   Nr%   )r'   c)r   r%   r(   r)   v  s    z8_stack_multi_column_index.<locals>.<listcomp>.<listcomp>r%   )r'   r.   r%   )r   r(   r)   v  s   z-_stack_multi_column_index.<locals>.<listcomp>Nr"   c             s  s   | ]\}}|V  qd S )Nr%   )r'   keyrT   r%   r%   r(   rC   |  s    z,_stack_multi_column_index.<locals>.<genexpr>c             S  s*   g | ]"\}}d |kr"t ||jdn|qS )N)r*   )r   r*   )r'   Znew_levr   r%   r%   r(   r)     s   )r3   )
rA   r1   r   r3   r   r.   	itertoolsgroupbyr   Zfrom_arrays)rd   rF   ZtuplesZunique_tuplesZnew_levsr%   r%   r(   _stack_multi_column_indexp  s    "r   c       "   	     sV  dd }|   }|| jjd krl|j}x>t|| jjd D ](}|||}||d |}|||}q:W ||_|j s|d|j}	|j|	dd}t|j}
i }|jjd }t	t
|jjd }|t|d }t||}t| g }x~|
D ]t}y|j|}W n" tk
r&   || wY nX t|ts>t|}n|j|j }| kr|jd d |j| f }||jjd |_|j|dj}n| jrt| jjd r||j|  jjd }||j|  }| dd	 |  D }|j!\}}t"|| #||j$% }||}n,| j&r6||j|  j}n|jd d |f }|j'dkr\|% }|||< qW t|dkr|
(|}
t|}t|j)t*rt+|j)j}t+|j)j,} fd
d	|j)jD }n*t-|j)\}}|g}|. g}|j)j/g}|| |t0|| || jj,|  t*|||dd} | j1|| |
d}!|rR|!j2ddd}!|!S )Nc             S  s   | |j kr|j |  S | S )z
        Logic for converting the level number to something we can safely pass
        to swaplevel.

        If `level_num` matches a column name return the name from
        position `level_num`, otherwise return `level_num`.
        )r3   )r   rd   r%   r%   r(   _convert_level_number  s    

z3_stack_multi_columns.<locals>._convert_level_numberr$   r   )r-   rR   r"   )rd   c             S  s   g | ]\}}|j qS r%   )r   )r'   rT   rB   r%   r%   r(   r)     s    z(_stack_multi_columns.<locals>.<listcomp>c               s   g | ]}|  qS r%   )r   )r'   r   )levsizer%   r(   r)     s    F)r1   r.   r3   r   )r#   rd   rs   )rR   how)3rq   rd   r   r   Z	swaplevelZ_is_lexsortedZ
sort_indexr   r1   sortedsetr.   r   rA   r9   rN   Zget_locKeyErrorr   rx   slicestopstartlocZreindexrQ   r   r   r   Zilocry   r   r   rg   r_   rt   r   r   Z_is_mixed_typere   
differencer#   r   r0   r3   r   r   rw   r   r   r   )"r   r   r   r   thisZroll_columnsr   Zlev1Zlev2Zlevel_to_sortr   Znew_dataZ
level_valsr   Zlevel_vals_nanZlevel_vals_usedZ	drop_colsr   r   Z	slice_lenchunkZvalue_slicer*   Zsubsetr   r   idxra   r   r   Z	old_codesZ
old_levelsrj   r   r%   )r   r(   r     s    










r   rT   FrZ   zDtype | Noner   )dummy_nasparse
drop_firstr*   r@   c          
     s  ddl m} ddg}	t| trx|dkr6| j|	d nt|sHtdn| |   fdd	}
|
d
 |
d ttrt	gtt
rfdd jD dkr jttrt	gntt
rfdd jD  j| jkrg }n*|dk	r| j|ddg}n| j|	dg}xDt  D ]0\}}}t|d ||||||d}|| q6W ||dd}nt| ||||d}|S )ay
  
    Convert categorical variable into dummy/indicator variables.

    Parameters
    ----------
    data : array-like, Series, or DataFrame
        Data of which to get dummy indicators.
    prefix : str, list of str, or dict of str, default None
        String to append DataFrame column names.
        Pass a list with length equal to the number of columns
        when calling get_dummies on a DataFrame. Alternatively, `prefix`
        can be a dictionary mapping column names to prefixes.
    prefix_sep : str, default '_'
        If appending prefix, separator/delimiter to use. Or pass a
        list or dictionary as with `prefix`.
    dummy_na : bool, default False
        Add a column to indicate NaNs, if False NaNs are ignored.
    columns : list-like, default None
        Column names in the DataFrame to be encoded.
        If `columns` is None then all the columns with
        `object` or `category` dtype will be converted.
    sparse : bool, default False
        Whether the dummy-encoded columns should be backed by
        a :class:`SparseArray` (True) or a regular NumPy array (False).
    drop_first : bool, default False
        Whether to get k-1 dummies out of k categorical levels by removing the
        first level.
    dtype : dtype, default np.uint8
        Data type for new columns. Only a single dtype is allowed.

    Returns
    -------
    DataFrame
        Dummy-coded data.

    See Also
    --------
    Series.str.get_dummies : Convert Series to dummy codes.

    Examples
    --------
    >>> s = pd.Series(list('abca'))

    >>> pd.get_dummies(s)
       a  b  c
    0  1  0  0
    1  0  1  0
    2  0  0  1
    3  1  0  0

    >>> s1 = ['a', 'b', np.nan]

    >>> pd.get_dummies(s1)
       a  b
    0  1  0
    1  0  1
    2  0  0

    >>> pd.get_dummies(s1, dummy_na=True)
       a  b  NaN
    0  1  0    0
    1  0  1    0
    2  0  0    1

    >>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'],
    ...                    'C': [1, 2, 3]})

    >>> pd.get_dummies(df, prefix=['col1', 'col2'])
       C  col1_a  col1_b  col2_a  col2_b  col2_c
    0  1       1       0       0       1       0
    1  2       0       1       1       0       0
    2  3       1       0       0       0       1

    >>> pd.get_dummies(pd.Series(list('abcaa')))
       a  b  c
    0  1  0  0
    1  0  1  0
    2  0  0  1
    3  1  0  0
    4  1  0  0

    >>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True)
       b  c
    0  0  0
    1  1  0
    2  0  1
    3  0  0
    4  0  0

    >>> pd.get_dummies(pd.Series(list('abc')), dtype=float)
         a    b    c
    0  1.0  0.0  0.0
    1  0.0  1.0  0.0
    2  0.0  0.0  1.0
    r   )concatrp   categoryN)includez1Input must be a list-like for parameter `columns`c               sH   t | rDt|  jd ksDd| dt|  d jd  d}t|d S )Nr$   zLength of 'z' (z9) did not match the length of the columns being encoded (z).)r   rA   rg   r<   )r   rw   Zlen_msg)data_to_encoder%   r(   	check_len}  s    "zget_dummies.<locals>.check_lenprefix
prefix_sepc               s   g | ]} | qS r%   r%   )r'   r   )r   r%   r(   r)     s    zget_dummies.<locals>.<listcomp>c               s   g | ]} | qS r%   r%   )r'   r   )r   r%   r(   r)     s    r$   )rR   )exclude)r   r   r   r   r   r*   )r   r   r*   )pandas.core.reshape.concatr   rx   r   Zselect_dtypesr   	TypeErrorstrr   cycledictrd   rg   Zdropr   r   _get_dummies_1dr   )r   r   r   r   rd   r   r   r*   r   Zdtypes_to_encoder   Zwith_dummiesr   presepr   r   r%   )r   r   r   r(   get_dummies  s\    i







r   c               sr  ddl m} tt| \}}	|d kr*tj}t|}t|rDtddddd}
|sjt	|	dkrj|
| S |
 }|rt	|	||dk< t|	tj}	|rt	|	d	kr|
| S t	|	} d kr|	}nt fd
d|	D }t| tr| j}nd }|rt|rd}n|tkrd}nd}g }t	| }dd tt	|D }|dk}|| }t|| }x&t||D ]\}}|| | qfW |r|d	d  }|d	d  }xPt||D ]B\}}ttjt	||dt||||d}|t|||d qW ||d	dd}tt|}|S tj||dj|d	dj}|s:d||dk< |r`|d d d	d f }|d	d  }t|||dS d S )Nr   )r   z1dtype=object is not a valid dtype for get_dummiesr   )r@   c             S  s*   t | tr| j}ntt| }t|dS )N)r#   )rx   r   r#   r9   r_   rA   r   )r   r#   r%   r%   r(   get_empty_frame  s    
z(_get_dummies_1d.<locals>.get_empty_framer"   r$   c               s   g | ]}   | qS r%   r%   )r'   r-   )r   r   r%   r(   r)     s    z#_get_dummies_1d.<locals>.<listcomp>Fg        c             S  s   g | ]}g qS r%   r%   )r'   rT   r%   r%   r(   r)     s    )r*   )Zsparse_indexrl   r*   )r   r#   rw   )rR   rq   )rR   )r#   rd   )r   r   r   r   r9   Zuint8r*   r   r<   rA   rq   r   nanr   rx   r#   r   rZ   r   r_   r   r   ru   r   r   r   eyerN   r   )r   r   r   r   r   r   r*   r   r.   r1   r   Znumber_of_colsZ
dummy_colsr#   rl   Zsparse_seriesr   Z
sp_indicesr^   Zn_idxZndxcoder   ZixsZsarroutZ	dummy_matr%   )r   r   r(   r     sr    	




r   r    r   )r   n_rows	n_columnsr@   c             C  s&   t || ||j }| |S )a  
    Re-orders the values when stacking multiple extension-arrays.

    The indirect stacking method used for EAs requires a followup
    take to get the order correct.

    Parameters
    ----------
    arr : ExtensionArray
    n_rows, n_columns : int
        The number of rows and columns in the original DataFrame.

    Returns
    -------
    taken : ExtensionArray
        The original `arr` with elements re-ordered appropriately

    Examples
    --------
    >>> arr = np.array(['a', 'b', 'c', 'd', 'e', 'f'])
    >>> _reorder_for_extension_array_stack(arr, 2, 3)
    array(['a', 'c', 'e', 'b', 'd', 'f'], dtype='<U1')

    >>> _reorder_for_extension_array_stack(arr, 3, 2)
    array(['a', 'd', 'b', 'e', 'c', 'f'], dtype='<U1')
    )r9   r_   rt   r   r   rN   )r   r   r   r   r%   r%   r(   r   0  s    !r   )N)N)N)r"   T)T)r"   T)NrT   FNFFN)rT   FFFN)F
__future__r   r   typingr   r   numpyr9   Zpandas._libs.reshapeZ_libsrt   r}   Zpandas._libs.sparser   Zpandas._typingr   Zpandas.util._decoratorsr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr	   r
   r   r   r   r   r   r   r   Zpandas.core.dtypes.dtypesr   Zpandas.core.dtypes.missingr   Zpandas.core.algorithmscoreZ
algorithmsrS   Zpandas.core.arraysr   Zpandas.core.arrays.categoricalr   Zpandas.core.constructionr   Zpandas.core.framer   Zpandas.core.indexes.apir   r   Zpandas.core.seriesr   Zpandas.core.sortingr   r   r   r   r   r    r!   r   r~   r   r   r   r   r   r   r   r   r   r%   r%   r%   r(   <module>   s\   ,  %
W
%

J
)
}       6    i