
    h                       d Z ddlmZ ddlZddlmZmZmZ ddlZ	ddl
mZmZ ddlmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZmZ dd	lmZ dd
lmZm Z  ddl!m"Z"m#Z#m$Z$ ddl%m&c m'Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZEmFZFmGZGmHZHmIZI ddlJmKZK erddlLmMZM ddlNmOZOmPZPmQZQmRZRmSZSmTZT  G d de?      ZU G d deU      ZV G d deUe@      ZW G d d      ZXd!d ZYy)"zA
Experimental manager based on storing a collection of 1D arrays
    )annotationsN)TYPE_CHECKINGCallableLiteral)NaTlib)astype_arrayastype_array_safe)ensure_dtype_can_hold_nafind_common_typeinfer_dtype_from_scalarnp_find_common_type)ensure_platform_intis_datetime64_ns_dtype
is_integeris_numeric_dtypeis_object_dtypeis_timedelta64_ns_dtype)ExtensionDtype)ABCDataFrame	ABCSeries)array_equalsisnana_value_for_dtype)quantile_compat)take_1d)DatetimeArrayExtensionArrayNumpyExtensionArrayTimedeltaArray)ensure_wrapped_if_datetimelikeextract_arraysanitize_array)maybe_convert_indicesvalidate_indices)Indexensure_index)get_values_for_csv)DataManagerSingleDataManagerensure_np_dtypeinterleaved_dtype)BlockPlacementensure_block_shapeexternal_valuesextract_pandas_arraymaybe_coerce_values	new_block)make_na_array)Hashable)	ArrayLikeAxisIntDtypeObjQuantileInterpolationSelfnptc                     e Zd ZU dZddgZded<   ded<   	 d(	 	 	 	 	 	 	 d)dZd*d+dZed,d	       Z	ed-d
       Z
ed.d       Zed/d       Zd0dZd1dZd2dZd Zd3dZd4dZ	 d*	 	 	 d5dZd*d+dZd(d6dZd7dZd8d9dZd:dZddd	 	 	 d;dZed<d       Zed<d       Zed<d       Zd=dZd>d?d Zd>d?d!Z d(d@d"Z!	 	 	 	 	 dA	 	 	 	 	 	 	 	 	 	 	 dBd#Z"	 	 	 	 dC	 	 	 	 	 	 	 	 	 	 	 dDd$Z#	 	 dE	 	 	 	 	 	 	 dFd%Z$dGdHd&Z%d<d'Z&y)IBaseArrayManageraO  
    Core internal data structure to implement DataFrame and Series.

    Alternative to the BlockManager, storing a list of 1D arrays instead of
    Blocks.

    This is *not* a public API class

    Parameters
    ----------
    arrays : Sequence of arrays
    axes : Sequence of Index
    verify_integrity : bool, default True

    _axesarrays!list[np.ndarray | ExtensionArray]list[Index]c                    t         NNotImplementedError)selfr>   axesverify_integritys       HD:\jyotish\venv\Lib\site-packages\pandas/core/internals/array_manager.py__init__zBaseArrayManager.__init__~   s
     "!    Nc                b    || j                   dd t        g       g}g } t        |       ||      S )zFReturn an empty ArrayManager with the items axis of len 0 (no columns)N   )rF   r&   type)rE   rF   r>   s      rH   
make_emptyzBaseArrayManager.make_empty   s7    <IIabM59-D46tDz&$''rJ   c                     | j                   d   S )Nr=   rE   s    rH   itemszBaseArrayManager.items   s    zz"~rJ   c                >    | j                   d   | j                   d   gS )z5Axes is BlockManager-compatible order (columns, rows)rL   r   rQ   rR   s    rH   rF   zBaseArrayManager.axes   s     

1tzz!}--rJ   c                :    t        d | j                  D              S )Nc              3  2   K   | ]  }t        |        y wrB   )len).0axs     rH   	<genexpr>z0BaseArrayManager.shape_proper.<locals>.<genexpr>   s     2zSWzs   )tupler=   rR   s    rH   shape_properzBaseArrayManager.shape_proper   s     2tzz222rJ   c                    | dk(  rd} | S d} | S Nr   rL    axiss    rH   _normalize_axisz BaseArrayManager._normalize_axis   s      AIq $%rJ   c                h    | j                  ||       | j                  |      }|| j                  |<   y rB   )_validate_set_axisrb   r=   )rE   ra   
new_labelss      rH   set_axiszBaseArrayManager.set_axis   s0    j1##D)%

4rJ   c                ~    t        j                  | j                  D cg c]  }|j                   c}d      S c c}w )Nobjectdtype)nparrayr>   rj   rE   arrs     rH   
get_dtypeszBaseArrayManager.get_dtypes   s-    xxdkk:ksk:(KK:s   :c                     y)z<
        Only implemented on the BlockManager level
        Nr_   )rE   mgrs     rH   add_referenceszBaseArrayManager.add_references   s     	rJ   c                2    | j                   | j                  fS rB   r>   r=   rR   s    rH   __getstate__zBaseArrayManager.__getstate__   s    {{DJJ&&rJ   c                ,    |d   | _         |d   | _        y r^   rt   )rE   states     rH   __setstate__zBaseArrayManager.__setstate__   s    Ah1X
rJ   c                    t        |       j                  }|d| j                  d    z  }| j                  dk(  r|d| j                  d    z  }|dt	        | j
                         dz  }| j
                  D ]  }|d|j                   z  } |S )Nz
Index: r      z

Columns: rL   
z arrays:)rM   __name__r=   ndimrW   r>   rj   )rE   outputrn   s      rH   __repr__zBaseArrayManager.__repr__   s    d$$Idjjm_--99>DJJqM?33FBs4;;'(11;;C399+&&F rJ   c                V   d|vsJ |xs g }g }|D ci c]  }|||   
 }}|dk(  r|j                  d      }t        | j                        D ]  \  }}|ry|j                         D ]f  \  }}	t	        |	t
        t        f      rC|	j                  dk(  r|	j                  |   ||<   >|	j                  dd|f   j                  ||<   _|	|   ||<   h t        |      r
 ||fi |}
n t        ||      di |}
|j                  |
        | j                  } t        |       ||      S c c}w )ab  
        Iterate over the arrays, collect and create a new ArrayManager.

        Parameters
        ----------
        f : str or callable
            Name of the Array method to apply.
        align_keys: List[str] or None, default None
        **kwargs
            Keywords to pass to `f`

        Returns
        -------
        ArrayManager
        filterapplyfuncrL   Nr_   )pop	enumerater>   rS   
isinstancer   r   r}   iloc_valuescallablegetattrappendr=   rM   )rE   f
align_keyskwargsresult_arrayskaligned_argsirn   objappliednew_axess               rH   r   zBaseArrayManager.apply   s:   * v%%%%2
)+ /99j6!9j9<

6"A,FAs*002FAs!#	<'@A 88q=(+F1I(+A(>(>F1I %(Fq	 3 {C*6*)'#q/3F3  )' -* ::tDz-227 :s   D&c           	     >   d}|dk(  rd}|r"d|v r| j                   dk(  r|d   dk(  rdnd|d<   |xs g }|D ci c]  }|||   
 }}g }t        | j                        D ]  \  }}	|r|j                         D ]  \  }}
t	        |
t
        t        f      r|
j                   dk(  rV| j                   dk(  r*|
j                  t        ||dz            j                  ||<   d|
j                  d d  j                  ||<   |
j                  d d |gf   j                  ||<   |
j                   dk(  s|
|g   ||<    t	        |	j                  t        j                        r/t	        |	t        j                        st        j                  |	      }	t        |	      }	| j                   dk(  r1t        |	d      }	t!        t        ddd            }t#        |	|d      }n-t!        t        dt%        |       d            }t#        |	|d      } t'        ||      d	i |}t	        |t(              r|d   }|j*                  }	| j                   dk(  r(|	j                   dk(  rt%        |	      dk(  sJ |	dd d f   }	|j-                  |	         t/        |       || j0                        S c c}w )
NTinterpolateFra   rz   r   rL   )	placementr}   r_   )r}   r   r>   rS   r   r   r   r   slicer   rj   rk   ndarrayasarrayr1   r.   r-   r2   rW   r   listvaluesr   rM   r=   )rE   r   r   r   	swap_axisr   r   r   r   rn   r   bpblockr   s                 rH   apply_with_blockz!BaseArrayManager.apply_with_block   s_   	I6)dii1n"(.A"5Q1F6N%2
.89j6!9j9,FAs*002FAs!#	<'@A 88q=#yyA~,/HHU1a!e_,E,M,Mq	,/HHQK,?,?q	(+QC(8(@(@F1I 88q=(+QCF1I 3  #))RXX.z#rzz7R jjo%c*CyyA~(a0#E!QN3!#!<#E!SY$:;!#!<'geQ'1&1G'4(!!*..CyyA~#((a-3x1}$} !Q$i  %U -X tDz-44a :s   Jc                *    | j                  d||      S )Nsetitemindexervalue)r   rE   r   r   warns       rH   r   zBaseArrayManager.setitem8  s    $$Yu$MMrJ   c                f    | j                   dk(  sJ | j                  t        j                  |      S )Nrz   n)r}   r   algosdiffrE   r   s     rH   r   zBaseArrayManager.diff;  s)    yyA~~zz%**z**rJ   c                <    |d}| j                  t        |||      S )NT)rj   copyerrors)r   r
   )rE   rj   r   r   s       rH   astypezBaseArrayManager.astype?  s$    <Dzz+5tFzSSrJ   c                8    dfd}| j                  |      S )NTc                    t        | j                        rDt        j                  |       } t	        j
                  | d      }|| u rr| j                         S |S r| j                         S | S )NT)convert_non_numeric)r   rj   rk   r   r   maybe_convert_objectsr   )rn   resultr   s     rH   _convertz*BaseArrayManager.convert.<locals>._convertI  sc    syy) jjo22(, S=T88:%%)sxxz2s2rJ   )r   )rE   r   r   s    ` rH   convertzBaseArrayManager.convertE  s#    <D	3 zz(##rJ   nan)na_repquotingc               8    | j                  t        |||||      S )N)r   r   float_formatdate_formatdecimal)r   r(   )rE   r   r   r   r   r   s         rH   r(   z#BaseArrayManager.get_values_for_csvZ  s,     zz%#  
 	
rJ   c                     y)z>Whether any of the blocks in this manager are extension blocksFr_   rR   s    rH   any_extension_typesz$BaseArrayManager.any_extension_typesf  s     rJ   c                     y)z8return a boolean if we are a single block and are a viewFr_   rR   s    rH   is_viewzBaseArrayManager.is_viewk  s     rJ   c                2    t        | j                        dk(  S NrL   )rW   r>   rR   s    rH   is_single_blockz BaseArrayManager.is_single_blockq  s    4;;1$$rJ   c                d   t        | j                        D cg c]  \  }} ||      s| }}}|D cg c]  }| j                  |    }}t        j                  |d      }| j                  d   j                  |      }| j                  d   |g} t        |       ||d      S c c}}w c c}w )Nintpri   rL   r   FrG   )r   r>   rk   rl   r=   takerM   )	rE   	predicater   rn   indicesr>   takernew_colsr   s	            rH   _get_data_subsetz!BaseArrayManager._get_data_subsetu  s    #,T[[#9L#9CYs^1#9L*12'Q$++a.'2 /::a=%%e,JJqM8,tDz&(UCC M2s   B'B'B-c                &    | j                  d       S )z
        Select columns that are bool-dtype and object-dtype columns that are all-bool.

        Parameters
        ----------
        copy : bool, default False
            Whether to copy the blocks
        c                N    | j                   t        j                   t              k(  S rB   )rj   rk   bool)xs    rH   <lambda>z0BaseArrayManager.get_bool_data.<locals>.<lambda>  s    qww"((4./HrJ   r   rE   r   s     rH   get_bool_datazBaseArrayManager.get_bool_data  s     $$%HIIrJ   c                &    | j                  d       S )z
        Select columns that have a numeric dtype.

        Parameters
        ----------
        copy : bool, default False
            Whether to copy the blocks
        c                ^    t        | j                        xs t        | j                  dd      S )N_is_numericF)r   rj   r   )rn   s    rH   r   z3BaseArrayManager.get_numeric_data.<locals>.<lambda>  s(    (3 8syy-78rJ   r   r   s     rH   get_numeric_dataz!BaseArrayManager.get_numeric_data  s     $$8
 	
rJ   c                >   dr%fd}| j                   D cg c]
  } ||       }}nt        | j                         }r(| j                  D cg c]  }|j                          }}nt        | j                        } t	        |       ||d      S c c}w c c}w )a>  
        Make deep or shallow copy of ArrayManager

        Parameters
        ----------
        deep : bool or string, default True
            If False, return shallow copy (do not copy data)
            If 'all', copy data and a deep copy of the index

        Returns
        -------
        BlockManager
        Tc                R    dk(  r| j                  d      S | j                         S )NallTdeep)r   view)rY   r   s    rH   	copy_funcz(BaseArrayManager.copy.<locals>.copy_func  s%    -1U]rwwDw)I	IrJ   Fr   )r=   r   r>   r   rM   )rE   r   r   rY   r   rn   
new_arrayss    `     rH   r   zBaseArrayManager.copy  s     < D J 15

;
"	"
H;DJJ'H04<#((*J<dkk*JtDz*hGG <
 =s   BBc	           	     R    | j                  |      }| j                  |||||||      S rB   )rb   _reindex_indexer)	rE   new_axisr   ra   
fill_value
allow_dupsr   
only_sliceuse_na_proxys	            rH   reindex_indexerz BaseArrayManager.reindex_indexer  s<     ##D)$$
 	
rJ   c           
        |d}|R|| j                   |   u r|s| S | j                  |      }t        | j                         |_         ||j                   |<   |S |s| j                   |   j                  |       || j                  k\  rt        d      |dk(  rUg }	|D ]M  }
|
dk(  r| j                  ||      }n!| j                  |
   }|r|j                         }|	j                  |       O nit        |t        | j                   d                t        |      }|dk(  }|j                         }| j                  D cg c]  }t        |||||       }	}t        | j                         }|||<    t        |       |	|d	
      S c c}w )a!  
        Parameters
        ----------
        new_axis : Index
        indexer : ndarray[intp] or None
        axis : int
        fill_value : object, default None
        allow_dups : bool, default False
        copy : bool, default True


        pandas-indexer with -1's only.
        Tr   #Requested axis not found in managerrL   rP   )r   r   r   
allow_fillr   maskFr   )r=   r   r   _validate_can_reindexr}   
IndexError_make_na_arrayr>   r   r%   rW   r   anyr   rM   )rE   r   r   ra   r   r   r   r   r   r   r   rn   r   needs_maskingr   s                  rH   r   z!BaseArrayManager._reindex_indexer  s   . < D?4::d++DYYDY)F

+FL!)FLLM JJt227;499BCC19J7--#-L . C ++a.C!hhj!!#&  Wc$**Q-&89)'2Gb=D HHJM  ;;
 'C ,) '  
 

#!tDz*hGG
s   =Fc                   t        |t        j                        sJ t        |             |j                  t        j
                  k(  sJ |j                         | j                  |      }|j                  dk(  st        d      | j                  |   }t        |||      }| j                  |   j                  |      }| j                  |||d      S )z,
        Take items along any axis.
        rL   zindexer should be 1-dimensional)verifyT)r   r   ra   r   )r   rk   r   rM   rj   r   rb   r}   
ValueErrorr\   r$   r=   r   r   )rE   r   ra   r   r   re   s         rH   r   zBaseArrayManager.take$  s     '2::.=W=.}}'66'##D)||q >??d#'6BZZ%**73
$$t % 
 	
rJ   c                    |r|J t        | j                  d         S |t        j                  }t	        |      \  }}t        || j                  d d |      }|S r^   )NullArrayProxyr\   rk   r   r   r3   )rE   r   r   rj   array_valuess        rH   r   zBaseArrayManager._make_na_array=  sf    %%%!$"3"3A"677J3J?z$UD,=,=bq,A:NrJ   c                p    t        | j                  |j                        D ]  \  }}t        ||      r y y)z
        Used in .equals defined in base class. Only check the column values
        assuming shape and indexes have already been checked.
        FT)zipr>   r   )rE   otherleftrights       rH   _equal_valueszBaseArrayManager._equal_valuesI  s3    
 t{{ELL9KD%e, : rJ   Tr>   r?   rF   r@   rG   r   returnNonerB   r  r9   r  r&   r  r@   )r  ztuple[int, ...])ra   r6   r  int)ra   r6   re   r&   r  r  )r  znpt.NDArray[np.object_])rq   r<   r  r  r  r  )r  str)r   zlist[str] | Noner  r9   )r   r   r  r9   )r   r
  r  r9   )Fraise)r   bool | Noner   r  r  r9   )r   r  r  r9   )r   r  r  r9   r  r   )r   r   r  r9   F)r   r   r  r9   )r   zbool | Literal['all'] | Noner  r9   )NFTFF)ra   r6   r   r   r   r  r   r   r   r   r  r9   )NFTF)r   znpt.NDArray[np.intp] | Nonera   r6   r   r   r   r  r   r   r  r9   )rL   T)r   znpt.NDArray[np.intp]ra   r6   r   r   r  r9   )NF)r   r   )'r|   
__module____qualname____doc__	__slots____annotations__rI   rN   propertyrS   rF   r\   staticmethodrb   rf   ro   rr   ru   rx   r   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   r   r  r_   rJ   rH   r<   r<   e   sc   " 	I
 .- "&	"1" " 	"
 
"(   . . 3 3  
&L' (,63 %63
 
63p95vN+T$, DIRV

=@

	

    
 % %	D	J
"HR    "
 	
 
 
 
 
 

:   "MH -MH 	MH MH MH MH 
MHd 	
%
 
 	

 

2
rJ   r<   c                  L   e Zd Zedd       Z	 d	 	 	 	 	 	 	 ddZddZddZdddZd dZ	d!dZ
ed"d       Z	 	 d#	 	 	 	 	 	 	 d$dZ	 d%	 	 	 	 	 	 	 d&dZd'd(dZd)dZd*dZd*dZd+dZd	dd	 	 	 	 	 	 	 d,dZd)dZd
d	ej,                  f	 	 	 	 	 d-dZed.d       Zed.d       Zy
)/ArrayManagerc                     y)Nrz   r_   rR   s    rH   r}   zArrayManager.ndimX      rJ   c                   || _         || _        |rj|D cg c]  }t        |       c}| _         |D cg c]  }t        |d d      d    }}|D cg c]  }t	        |       c}| _        | j                          y y c c}w c c}w c c}w NrL   r   )r=   r>   r'   r0   r1   _verify_integrity)rE   r>   rF   rG   rY   r   rn   s          rH   rI   zArrayManager.__init__\  s     
59:Tr,r*T:DJCIJ6a*1dA6q96FJ?EFv.s3vFDK""$	 :JFs   A<BBc                   | j                   \  }}t        | j                        |k(  s%t        dt        | j                         d| d      | j                  D ]  }t        |      |k(  st        dt        |       d| d      t	        |t
        j                  t        f      st        dt        |       d      |j                  d	k(  rtt        d
|j                   d       y )NzANumber of passed arrays must equal the size of the column Index: z arrays vs z	 columns.z=Passed arrays should have the same length as the rows Index: z vs z rowszDPassed arrays should be np.ndarray or ExtensionArray instances, got z insteadrL   z6Passed arrays should be 1-dimensional, got array with  dimensions instead.)
r\   rW   r>   r   r   rk   r   r   rM   r}   )rE   n_rows	n_columnsrn   s       rH   r  zArrayManager._verify_integritym  s     --	4;;9,St{{#$K	{)E  ;;Cs8v% S3xjVHE3  cBJJ#?@ 9+X/  88q= Lxxj 46  rJ   c                $   t        | j                  D cg c]  }|j                   c}      }| j                  D cg c]  }||   	 }}t        |t              r"|j                         j                  ||      }nqt        |      r"t        j                  ||      j                  }nDt        |      r"t        j                  ||      j                  }nt        j                  ||      }t        |g| j                  d   g      S c c}w c c}w )z
        Return the array corresponding to `frame.iloc[loc]`.

        Parameters
        ----------
        loc : int

        Returns
        -------
        np.ndarray or ExtensionArray
        ri   rL   )r,   r>   rj   r   r   construct_array_type_from_sequencer   r   _ndarrayr   r    rk   rl   SingleArrayManagerr=   )rE   locrn   rj   r   r   s         rH   fast_xszArrayManager.fast_xs  s     ""D399"DE&*kk2ks#c(k2e^,//1@@u@UF#E*"11&FOOF$U+#226GPPFXXfE2F!6(TZZ]O<< #E2s
   DDc                   | j                  |      }|dk(  r| j                  D cg c]  }||   	 }}n|dk(  r| j                  |   }t        | j                        }||   j	                  |      ||<    t        |       |d      S c c}w )Nr   rL   Fr   )rb   r>   r   r=   _getitem_slicerM   )rE   slobjra   rn   r>   r   s         rH   	get_slicezArrayManager.get_slice  s    ##D)19,0KK8KSc%jKF8QY[['F

#!$66u=tDz&(UCC 9s   Bc                V    | j                   |   }t        |g| j                  d   g      S )z:
        Return the data as a SingleArrayManager.
        r   )r>   r'  r=   )rE   r   r   s      rH   igetzArrayManager.iget  s*     Q!6(TZZ]O<<rJ   c                     | j                   |   S )zY
        Return the data for column i as the values (ndarray or ExtensionArray).
        r>   )rE   r   s     rH   iget_valueszArrayManager.iget_values  s     {{1~rJ   c                f    | j                   D cg c]  }t        j                  |       c}S c c}w )zB
        Used in the JSON C code to access column arrays.
        )r>   rk   r   rm   s     rH   column_arrayszArrayManager.column_arrays  s(     ,0;;7;C

3;777s   .FNc                   t        j                  |      rt        |t        j                        r,|j
                  dk(  r|j                  d   dk(  sJ |dddf   }t        |      }t        |t        j                  t        f      sJ |j
                  dk(  sJ t        |      t        | j                  d         k(  sJ || j                  |<   yt        |t              rct        |j                  |j                  nd|j                  |j                  n| j                   d   |j"                  |j"                  nd      }nEt        |t        j                        sJ |j$                  dk(  sJ t        j&                  |      d   }|j
                  dk(  sJ |j                  d   t        | j                  d         k(  sJ t)        |      D ]  \  }}|dd|f   }|| j                  |<    y)a  
        Set new column(s).

        This changes the ArrayManager in-place, but replaces (an) existing
        column(s), not changing column values in-place).

        Parameters
        ----------
        loc : integer, slice or boolean mask
            Positional location (already bounds checked)
        value : np.ndarray or ExtensionArray
        inplace : bool, default False
            Whether overwrite existing array as opposed to replacing it.
        rz   rL   Nr   r   )r   r   r   rk   r   r}   shaper1   r   rW   r=   r>   r   rangestartstopr\   steprj   nonzeror   )	rE   r(  r   inplacerefsr   	value_idxmgr_idx	value_arrs	            rH   isetzArrayManager.iset  s   , >># %,q{{1~***ad (.Eebjj.%ABBB::?"?u:TZZ]!3333$DKK U#*/ YY2		HH0d6G6G6JHH0a+G c2::...99&&&jjoa(GzzQ{{1~TZZ]!3333"+G"4Iw al+I#,DKK 	 #5
 	rJ   c                   t        |      st        d      | j                  |   }t        |g| j                  d   g      }|r|j                  ||       y|j                  |f|      }|j                  d   | j                  |<   y)z
        Set values ("setitem") into a single column (not setting the full column).

        This is a method on the ArrayManager level, to avoid creating an
        intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`)
        z%The column index should be an integerr   N)r   	TypeErrorr>   r'  r=   setitem_inplacer   )rE   r(  idxr   inplace_onlyrn   rq   new_mgrs           rH   column_setitemzArrayManager.column_setitem  sy     #CDDkk# #A8U+kk3&%0G&~~a0DKKrJ   c                v   | j                   j                  ||      }t        |d      }|j                  dk(  r4|j                  d   dk(  r
|dddf   }nt        d|j                         t        |      }| j                  j                         }|j                  ||       || _        || j                  d<   y)z
        Insert item at selected position.

        Parameters
        ----------
        loc : int
        item : hashable
        value : np.ndarray or ExtensionArray
        T)extract_numpyrz   r   rL   Nz-Expected a 1D array, got an array with shape )
rS   insertr"   r}   r6  r   r1   r>   r   r=   )rE   r(  itemr   r=  r   r>   s          rH   rK  zArrayManager.insert  s     ::$$S$/e48::?{{1~" ad CEKK=Q  $E* !!#c5! 

1rJ   c                :   t        j                  | j                  d   t         j                        }d||<   t        j                  |      d   D cg c]  }| j
                  |    c}| _        | j                  d   | j                  d   |   g| _        | S c c}w )z]
        Delete selected locations in-place (new block and array, same BlockManager)
        r   ri   FrL   )rk   onesr6  bool_r;  r>   r=   )rE   r   to_keepr   s       rH   ideletezArrayManager.idelete8  s     ''$**Q-rxx8 /1zz'/B1/EF/E!t{{1~/EFjjmTZZ]7%;<
 Gs   Bc                   g }g }t        | j                        D ]d  \  }}t        |d      } ||      }|j                  dk(  r|j                  d   dk(  sJ |d   }|j                  |       |j                  |       f t        |      dk(  rd}n|d   j                  d   }t        t        |            }| j                  }	 t        |       |||	g      S )z
        Apply grouped reduction function columnwise, returning a new ArrayManager.

        Parameters
        ----------
        func : grouped reduction function

        Returns
        -------
        ArrayManager
        rz   )r}   r   rL   )r   r>   r.   r}   r6  r   rW   r&   r7  rS   rM   )
rE   r   r   result_indicesr   rn   resnrowsindexcolumnss
             rH   grouped_reducezArrayManager.grouped_reduceF  s     +-$&,FAs$Sq1Cs)Cxx1}yy|q(((!f  %!!!$ - }"E!!$**1-EeEl#** tDz-%)9::rJ   c                ^   g }t        | j                        D ]C  \  }} ||d      }|t        u r|j                  nd}|j	                  t        |gd|             E t        j                  t        j                  dgt                    }| j                  } t        |       |||g      }	|	S )z
        Apply reduction function column-wise, returning a single-row ArrayManager.

        Parameters
        ----------
        func : reduction function

        Returns
        -------
        ArrayManager
        r   r`   Nri   )r   r>   r   rj   r   r#   r&   _simple_newrk   rl   rh   rS   rM   )
rE   r   r   r   rn   rT  rj   rV  rW  rG  s
             rH   reducezArrayManager.reducem  s     +-,FAss#C
 "%CIIE  ud%8 - !!"((D6"@A** $t*]UG,<=rJ   c                    | j                   }|j                   }t        ||      D cg c]  \  }} |||       }}} t        |       || j                        S c c}}w )zO
        Apply array_op blockwise with another (aligned) BlockManager.
        )r>   r   rM   r=   )rE   r   array_opleft_arraysright_arraysr   r  r   s           rH   operate_blockwisezArrayManager.operate_blockwise  sc    
 kk||58l5S
5SkdEHT5!5S 	 
 tDz-44
s   Alinear)
transposedinterpolationc          	        | j                   D cg c]  }t        |d       }}|D cg c],  }t        |t        j                  |j
                        |      . }}t        |      D ]@  \  }}|j                  dk(  s|j                  d   dk(  sJ |j                         |d   ||<   B || j                  d   g}	 t        |       ||	      S c c}w c c}w )Nrz   r   rL   )r>   r.   r   rk   r   r   r   r}   r6  r=   rM   )
rE   qsrb  rc  r   arrsnew_arrsr   rn   rF   s
             rH   quantilezArrayManager.quantile  s     37++>+Q"1a(+>OS
OS!OArzz"**5}Et 	 
  )FAsxx1}yy|q(3#))3(!!f *
 DJJqM"tDz(D)) ?
s
   C1Cc                   |j                   \  }}|j                  j                         r	|}d}d}d}nqt        j                  |j                  j
                  d      }|||j                  <   d} |j                   j                  |j                   }|j                  d      } |j                  |j                   }	t        |	      }	g }
| j                  D ]i  }t        |j                  d         D ]L  }|r"t        ||	dd|f   ||   ||dd|f         }nt        ||	dd|f   d	      }|
j                  |       N k |j                  }|j                  | j                   d         }||g} t#        |       |
|d
      S )a'  
        Return a BlockManager with all blocks unstacked.

        Parameters
        ----------
        unstacker : reshape._Unstacker
        fill_value : Any
            fill_value for newly introduced missing values.

        Returns
        -------
        unstacked : BlockManager
        FNrP   Tr   r`   rL   r   r   r   )_indexer_and_to_sortr   r   rk   fullr6  reshape
full_shaper   r   r>   r7  r   r   	new_indexget_new_columnsr=   rM   )rE   	unstackerr   r   _new_indexerr   
new_mask2Dr   new_indexer2Dr   rn   r   new_arrro  new_columnsr   s                    rH   unstackzArrayManager.unstack  s    33
>>!KJJ M'').."6"6;K*1K	'J 39>>/22I4H4HIJ&NNN2M+++Y-A-AB+M:
;;C9//23%%ad+#0#3#-'1-G &c=A+>5QG!!'* 4  ''	//

1>{+tDz*hGGrJ   c                >   t        | j                        dk(  r5t        j                  | j                  t
              }|j                         S |xs |t        j                  u}|s-t        | j                  D cg c]  }|j                   c}      }t        |      }t        j                  | j                  |      }t        | j                        D ]!  \  }}|j                  ||      }||dd|f<   # |t        j                  ur||t        |      <   |S c c}w )a#  
        Convert the blockmanager data into an numpy array.

        Parameters
        ----------
        dtype : object, default None
            Data type of the return array.
        copy : bool, default False
            If True then guarantee that a copy is returned. A value of
            False does not guarantee that the underlying data is not
            copied.
        na_value : object, default lib.no_default
            Value to be used as the missing value sentinel.

        Returns
        -------
        arr : ndarray
        r   ri   r   N)rW   r>   rk   emptyr6  float	transposer   
no_defaultr,   rj   r+   r\   r   r   r   )rE   rj   r   na_value	empty_arrrn   r   r   s           rH   as_arrayzArrayManager.as_array  s    0 t{{q 59I&&(( 5xs~~5%DKK&HKSsyyK&HIE&$++59,FAs**U*.CF1a4L - 3>>)#+F4<  'Is   9Dc                    t        t        j                  j                  |D cg c]  }|j                   c}            } | ||d   |d   gd      }|S c c}w )zK
        Concatenate uniformly-indexed ArrayManagers horizontally.
        rL   r   Fr   )r   	itertoolschainfrom_iterabler>   )clsmgrsrF   rq   r>   rG  s         rH   concat_horizontalzArrayManager.concat_horizontal  sV     ioo3344P4CSZZ44PQRftAwQ05I 5Qs   Ac                   t        t        |d   j                              D cg c]=  }t        t        t        |            D cg c]  }||   j                  |    c}      ? }}} | ||d   |d   gd      }|S c c}w c c}}w )zI
        Concatenate uniformly-indexed ArrayManagers vertically.
        r   rL   Fr   )r7  rW   r>   concat_arrays)r  r  rF   jr   r>   rG  s          rH   concat_verticalzArrayManager.concat_vertical  s     3tAw~~./
/ eCI6FG6F47>>!,6FGH/ 	 
 ftAwQ05I	 H
s    BA?	B?B)r  z
Literal[2]r  r  r  r(  r
  r  r'  r   )r,  r   ra   r6   r  r  )r   r
  r  r'  )r   r
  r  r5   )r  zlist[ArrayLike])FN)r(  int | slice | np.ndarrayr   r5   r<  r   r  r  r  )r(  r
  rE  r  rF  r   r  r  rB   )r(  r
  rL  r4   r   r5   r  r  )r  r  )r   r   r  r9   )r   r  r  r  )re  r&   rb  r   rc  r8   r  r  )r   r   r  rh   r  z
np.ndarray)r  z
list[Self]rF   r@   r  r9   )r|   r  r  r  r}   rI   r  r)  r-  r/  r2  r4  rA  rH  rK  rQ  rX  r[  r`  rh  rx  r   r~  r  classmethodr  r  r_   rJ   rH   r  r  W  s     "&	%1% % 	%
 
%"6=4D= 8 8 ;%; ; 	; 
;| TY1151LP1	1(!!F	%;N@
5  !/7* * 	*
 -* 
**3Hn >>	. . 	.
 
.`    rJ   r  c                  @   e Zd ZU ddgZded<   ded<   edd       Z	 d	 	 	 	 	 	 	 ddZd dZe	d        Z
d!d"d
Zed#d       Zed$d       Zed%d       Zed        Zd Zd Zd Zed&d       Zed&d       Zd'dZd(d)dZd*dZd"dZdd+dZd#dZd,dZd-dZd.dZy	)/r'  r=   r>   r?   r@   c                     yr   r_   rR   s    rH   r}   zSingleArrayManager.ndim5  r  rJ   c                0   || _         || _        |rt        |      dk(  sJ t        |      dk(  sJ | j                   D cg c]  }t        |       c}| _         |d   }t	        |      }t        |d d      d   }|g| _        | j                          y y c c}w r  )r=   r>   rW   r'   r1   r0   r  )rE   r>   rF   rG   rY   rn   s         rH   rI   zSingleArrayManager.__init__9  s     
t9>!>v;!###59ZZ@Zr,r*Z@DJ)C%c*C&sD!4Q7C%DK""$  As   Bc                    | j                   \  }t        | j                        dk(  sJ | j                  d   }t        |      |k(  sJ |j                  dk(  st	        d|j                   d      y )NrL   r   z5Passed array should be 1-dimensional, got array with r   )r6  rW   r>   r}   r   )rE   r!  rn   s      rH   r  z$SingleArrayManager._verify_integrityL  st    JJ	4;;1$$$kk!n3x6!!!xx1}G88*02  rJ   c                    | S rB   r_   r`   s    rH   rb   z"SingleArrayManager._normalize_axisW  s    rJ   Nc                    |t        g t              g}t        j                  g | j                        } t        |       |g|      S )z9Return an empty ArrayManager with index/array of length 0ri   )r&   rh   rk   rl   rj   rM   )rE   rF   rl   s      rH   rN   zSingleArrayManager.make_empty[  s?    <"F+,DHHRtzz:tDz5'4((rJ   c                     | |g|g      S rB   r_   )r  rl   rV  s      rH   
from_arrayzSingleArrayManager.from_arrayb  s    E7UG$$rJ   c                    | j                   S rB   rQ   rR   s    rH   rF   zSingleArrayManager.axesg      zzrJ   c                     | j                   d   S )Nr   rQ   rR   s    rH   rV  zSingleArrayManager.indexk  s    zz!}rJ   c                .    | j                   j                  S rB   )rl   rj   rR   s    rH   rj   zSingleArrayManager.dtypeo  s    zzrJ   c                ,    t        | j                        S )z$The array that Series.values returns)r/   rl   rR   s    rH   r/   z"SingleArrayManager.external_valuess  s    tzz**rJ   c                    | j                   S )z%The array that Series._values returns)rl   rR   s    rH   internal_valuesz"SingleArrayManager.internal_valuesw  r  rJ   c                h    | j                   }t        |t        j                        rt	        |      }|S )z#The array that Series.array returns)rl   r   rk   r   r   rm   s     rH   r   zSingleArrayManager.array_values{  s(    jjc2::&%c*C
rJ   c                    t        | j                  t        j                        r"| j                  j                  j
                  dvS | j                  j                  S )Niub)r   rl   rk   r   rj   kind_can_hold_narR   s    rH   r  zSingleArrayManager._can_hold_na  sA    djj"**-::##((55 ::***rJ   c                     y)NTr_   rR   s    rH   r   z"SingleArrayManager.is_single_block  s    rJ   c                    t        d      )NzUse series._values[loc] insteadrC   )rE   r(  s     rH   r)  zSingleArrayManager.fast_xs  s    !"CDDrJ   c                    || j                   k\  rt        d      | j                  |   }| j                  j	                  |      } t        |       |g|gd      S )Nr   Fr   )r}   r   rl   rV  r+  rM   )rE   r,  ra   	new_arrayro  s        rH   r-  zSingleArrayManager.get_slice  sU    499BCCJJu%	JJ--e4	tDz9+	{UKKrJ   c                f    | j                   |   }| j                  |   } t        |       |g|g      S rB   )rl   rV  rM   )rE   r   r  ro  s       rH   get_rows_with_maskz%SingleArrayManager.get_rows_with_mask  s5    JJw'	JJw'	tDz9+	{33rJ   c                    t        |      r || j                  fi |}n t        | j                  |      di |} t        |       |g| j                        S )Nr_   )r   rl   r   rM   r=   )rE   r   r   r  s       rH   r   zSingleArrayManager.apply  sO    D>TZZ262I1

D1;F;ItDz9+tzz22rJ   c                    t        |t        j                        r1|j                  | j                  kD  rt	        d| j                         | j                  d||      S )z
        Set values with indexer.

        For SingleArrayManager, this backs s[indexer] = value

        See `setitem_inplace` for a version that works inplace and doesn't
        return a new Manager.
        zCannot set values with ndim > r   r   )r   rk   r   r}   r   r   r   s       rH   r   zSingleArrayManager.setitem  sP     grzz*w||dii/G=dii[IJJ$$Yu$MMrJ   c                    t        j                  | j                  d   t         j                        }d||<   | j                  d   |   g| _        | j
                  d   |   g| _        | S )zS
        Delete selected locations in-place (new array, same ArrayManager)
        r   ri   F)rk   rN  r6  rO  r>   r=   )rE   r   rP  s      rH   rQ  zSingleArrayManager.idelete  s]     ''$**Q-rxx8 {{1~g./jjmG,-
rJ   c                     || j                         r( t        |       | j                  | j                  d      S | j	                         S )NFr   )rl   rM   r>   r=   rN   )rE   r   s     rH   r   z#SingleArrayManager._get_data_subset  s8    TZZ 4:dkk4::NN??$$rJ   c                "    || j                   d<   y)z
        Set (replace) the values of the SingleArrayManager in place.

        Use at your own risk! This does not check if the passed values are
        valid for the current SingleArrayManager (length, dtype, etc).
        r   Nr1  )rE   r   s     rH   
set_valueszSingleArrayManager.set_values  s      ArJ   c                `    | j                   d   g}| j                  d   |g}t        ||d      S )z5
        Manager analogue of Series.to_frame
        r   Fr   )r>   rF   r  )rE   rW  r>   rF   s       rH   	to_2d_mgrzSingleArrayManager.to_2d_mgr  s5     ++a.!		!g&FD5AArJ   )r  z
Literal[1]r  r  r  rB   r  )r  r'  r	  r  r  r  r  )r,  r   ra   r6   r  r'  )r   znpt.NDArray[np.bool_]r  r'  )r   r   r  r'  )r   r   r  r'  )r   r5   r  r  )rW  r&   r  r  )r|   r  r  r  r  r  r}   rI   r  r  rb   rN   r  r  rF   rV  rj   r/   r  r   r  r   r)  r-  r  r   r   rQ  r   r  r  r_   rJ   rH   r'  r'  ,  s5   I
 .-  "&	%1% % 	%
 
%&	  ) % %        + + +  EL43N	% BrJ   r'  c                  6    e Zd ZdZdZddZedd       Zd	dZy)
r   ab  
    Proxy object for an all-NA array.

    Only stores the length of the array, and not the dtype. The dtype
    will only be known when actually concatenating (after determining the
    common dtype, for which this proxy is ignored).
    Using this object avoids that the internals/concat.py needs to determine
    the proper dtype and array type.
    rL   c                    || _         y rB   r   r   s     rH   rI   zNullArrayProxy.__init__  s	    rJ   c                    | j                   fS rB   r   rR   s    rH   r6  zNullArrayProxy.shape  s    yrJ   c                   t        |t              rd|j                         j                  g |      }t	        j
                  | j                  t        j                         }|j                  |d      S t        |      }t        |      }t	        j                  | j                  |      }|j                  |       t        |      S )a'  
        Helper function to create the actual all-NA array from the NullArrayProxy
        object.

        Parameters
        ----------
        arr : NullArrayProxy
        dtype : the dtype for the resulting array

        Returns
        -------
        np.ndarray or ExtensionArray
        ri   Trj  )r   r   r$  r%  rk   rN  r   r   r   r   r   r{  fillr!   )rE   rj   r{  r   r   rn   s         rH   to_arrayzNullArrayProxy.to_array  s     e^,..0??%?PEwwtvvRWW55G::g$:77 -U3E+E2J((466/CHHZ 1#66rJ   N)r   r
  r  r  )r  z
tuple[int])rj   r7   r  r5   )	r|   r  r  r  r}   rI   r  r6  r  r_   rJ   rH   r   r     s,     D  7rJ   r   c                   | D cg c]  }t        |t              r| }}|D ch c]  }|j                   }}t        |      dk(  }|r|d   j                  }n>t	        d |D              r	t        | }n#t        |D cg c]  }|j                   c}      }| D cg c]1  }t        |t              r|j                  |      nt        ||d      3 } }t        | d   t              rt        | d         }|j                  |       S t        j                  |       }t        |      dk(  rJ|D 	ch c]  }	|	j                  j                   }
}	t        |
      dk7  rd|
v r|j                  t               }|S c c}w c c}w c c}w c c}w c c}	w )a  
    Alternative for concat_compat but specialized for use in the ArrayManager.

    Differences: only deals with 1D arrays (no axis keyword), assumes
    ensure_wrapped_if_datetimelike and does not skip empty arrays to determine
    the dtype.
    In addition ensures that all NullArrayProxies get replaced with actual
    arrays.

    Parameters
    ----------
    to_concat : list of arrays

    Returns
    -------
    np.ndarray or ExtensionArray
    rL   r   c              3  H   K   | ]  }t        j                  |d         yw)r  N)r   is_np_dtype)rX   r   s     rH   rZ   z concat_arrays.<locals>.<genexpr>!  s     71S__Q&s    "Frz  b)r   r   rj   rW   r   r   r   r  r	   r   rM   _concat_same_typerk   concatenater  r   rh   )	to_concatr   to_concat_no_proxydtypessingle_dtypetarget_dtypern   r  r   r   kindss              rH   r  r    s   & &/TYjN6S!YT121!agg1F2v;!#L)!,22	77	7*F3'>P(Q>Ps>P(QR 	 C c>* 	\"#|%8	9 	   )A,/9Q< $$Y//^^I&F 6{a+=>+=C+=>u:?e|v.ME U2 )R" ?s!   E5E5E:>E?6F-F	)r  r   r  r5   )Zr  
__future__r   r  typingr   r   r   numpyrk   pandas._libsr   r   pandas.core.dtypes.astyper	   r
   pandas.core.dtypes.castr   r   r   r   pandas.core.dtypes.commonr   r   r   r   r   r   pandas.core.dtypes.dtypesr   pandas.core.dtypes.genericr   r   pandas.core.dtypes.missingr   r   r   pandas.core.algorithmscore
algorithmsr    pandas.core.array_algos.quantiler   pandas.core.array_algos.taker   pandas.core.arraysr   r   r   r    pandas.core.constructionr!   r"   r#   pandas.core.indexersr$   r%   pandas.core.indexes.apir&   r'   pandas.core.indexes.baser(   pandas.core.internals.baser)   r*   r+   r,   pandas.core.internals.blocksr-   r.   r/   r0   r1   r2   pandas.core.internals.managersr3   collections.abcr4   pandas._typingr5   r6   r7   r8   r9   r:   r<   r  r'  r   r  r_   rJ   rH   <module>r     s    #   
   5  ' & < 0  
 8   9( l{ ldR# RjiB)+< iBX,7 ,7^5rJ   