o
    5c                     @  s0  d Z ddlmZ ddlmZ ddlZddlZddlmZm	Z	m
Z
mZmZmZmZmZ ddlZddlZddlmZ ddlm  mZ ddlmZmZ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.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZF ddlGmHZHmIZImJZJ ddlKmLZL ddlMmN  mOZP ddlQmRZR ddlSmTZT ddlUmVZV ddlWmXZX ddlYmZZZ ddl[mN  m\Z] ddl^m_Z_m`Z` ddlambZbmcZc ddldmeZe ddlfmgZg ddlhmN  miZi ddljmk  ml  mmZm erQddlnmoZo G dd deoZpepjqZqdd lrmsZs dd!lmtZtmuZu ed" Zvdd#lwmxZx neyeqZped$d%d&Zzd'd%iZ{dKd+d,Z|dLd3d4Z}	dMdNd7d8Z~G d9d% d%eTeZeVZ	:		dOdPd>d?ZedQdDdEZedRdHdEZdSdJdEZdS )Tz
SparseArray data structure
    )annotations)abcN)TYPE_CHECKINGAnyCallableLiteralSequenceTypeVarcastoverload)lib)
BlockIndexIntIndexSparseIndex)NaT)		ArrayLike	AstypeArgDtypeNpDtypePositionalIndexerScalarScalarIndexerSequenceIndexernpt)function)PerformanceWarning)find_stack_level)validate_bool_kwargvalidate_insert_loc)astype_nansafe)"construct_1d_arraylike_from_scalarfind_common_typemaybe_box_datetimelike)is_array_likeis_bool_dtypeis_datetime64_any_dtypeis_datetime64tz_dtypeis_dtype_equal
is_integeris_list_likeis_object_dtype	is_scalaris_string_dtypepandas_dtype)ABCIndex	ABCSeries)isnana_value_for_dtypenotna)	arraylike)quantile_with_mask)OpsMixin)ExtensionArray)SparseDtype)PandasObject)extract_arraysanitize_array)check_array_indexerunpack_tuple_and_ellipses)interpolate_2d)check_below_min_count)Enumc                   @  s   e Zd ZdZdS )ellipsisz...N)__name__
__module____qualname__Ellipsis rE   rE   U/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/arrays/sparse/array.pyr@   i   s    r@   )spmatrix)FillnaOptionsNumpySorter)integerblock)SeriesSparseArrayTSparseArray)boundklassarrreturn
np.ndarrayc                 C  s6   zt j| j| jjdW S  ty   t | j Y S w )a]  
    Create a 0-dim ndarray containing the fill value

    Parameters
    ----------
    arr : SparseArray

    Returns
    -------
    fill_value : ndarray
        0-dim ndarray with just the fill value.

    Notes
    -----
    coerce fill_value to arr dtype if possible
    int64 SparseArray can have NaN as fill_value if there is no missing
    dtype)npasarray
fill_valuerU   subtype
ValueError)rQ   rE   rE   rF   	_get_fill   s
   r[   leftrightopr   namestrc                 C  s  | dr|dd }| jj}|jj}t||s<t||g}t|| j}t||j}| j|dd} |j|dd}|j}n|}d}| jj	dksL|jj	dkrt
jdd	 ||  | }	|t| t|}
W d   n1 sow   Y  | jj	dkr~| j}n|j}n| j|jrt
jdd	 || j|j}	|t| t|}
W d   n1 sw   Y  | j}n|d d
kr|| } }|dd }|dv r|dkrd| d}| jt
j}|jt
j}t}nd| d| }| j}|j}|dv r|dk r| jjdv rd| d}|d}|d}tt|}t
jdd	 ||| j| j||j|j\}	}}
W d   n	1 s;w   Y  |dkr_t||	d ||
d |dt||	d ||
d |dfS |du rg|	j}t||	||
|dS )a4  
    Perform a binary operation between two arrays.

    Parameters
    ----------
    left : Union[SparseArray, ndarray]
    right : Union[SparseArray, ndarray]
    op : Callable
        The binary operation to perform
    name str
        Name of the callable.

    Returns
    -------
    SparseArray
    __   FcopyNr   ignoreallr   )andorxorboolsparse__uint8_)floordivmod)iu_float64float64divmodrT   )
startswithrU   rY   r'   r!   r7   rX   astypesp_indexngapsrV   errstateto_denser[   equals	sp_valuesviewuint8rn   anykindgetattrsplib_wrap_result)r\   r]   r^   r_   ltypertyperY   rU   result_dtyperesultfillindexopnameleft_sp_valuesright_sp_values	sparse_oprE   rE   rF   _sparse_array_op   s   










r   rU   Dtype | Nonec                 C  sL   |  dr| dd } | dv rt}t|}t|rt|}t||||dS )z.
    wrap op result to have correct dtype
    ra   rb   rc   )eqneltgtlege)sparse_indexrX   rU   )ry   rn   r   item_from_zerodimr$   rN   )r_   datar   rX   rU   rE   rE   rF   r   	  s   

r   c                   @  s  e Zd ZU dZdZejedgB Zded< ded< ded	< 	
	
	
		
	ddddZ	e
dddZe
ddd Zddd"d#Zd$d% Ze
d
dd&dd'd(Ze
d)d* Zedd+d,Zedd-d.Zedd/d0Zed1d2 Zejd3d2 Zedd4d5Zedd6d7Zdd9d:Zedd;d<Zdd=d>Zedd?d@ZeddBdCZeddDdEZdFdG Z	
	
	
dddMdNZ dddQdRZ!dSdT Z"ddUdVZ#dWdX Z$e%j&e%j&fdd^d_Z'dddcddZ(ddidjZ)e*ddndoZ+e*ddqdoZ+ddtdoZ+dudv Z,dd
dwddydzZ-ddd{d|Z.dd}d~Z/		
ddddZ0dddZ1e
dddZ2ddddZ3dddZ4dddZ5dd Z6dddZ7dddZ8d`ddddZ9dddZ:dddZ;	`ddddZ<ddddZ=dddZ>d
d`ddddZ?d
d`ddddZ@dddZAdddZBddddZCddddZDeEjFeGjHfZId ddZJddĄ ZKdddƄZLeLZMdddȄZNdddʄZOddd̄ZPddd΄ZQdddЄZRddd҄ZSdddԄZTd
S (  rN   a	  
    An ExtensionArray for storing sparse data.

    Parameters
    ----------
    data : array-like or scalar
        A dense array of values to store in the SparseArray. This may contain
        `fill_value`.
    sparse_index : SparseIndex, optional
    index : Index

        .. deprecated:: 1.4.0
            Use a function like `np.full` to construct an array with the desired
            repeats of the scalar value instead.

    fill_value : scalar, optional
        Elements in data that are ``fill_value`` are not stored in the
        SparseArray. For memory savings, this should be the most common value
        in `data`. By default, `fill_value` depends on the dtype of `data`:

        =========== ==========
        data.dtype  na_value
        =========== ==========
        float       ``np.nan``
        int         ``0``
        bool        False
        datetime64  ``pd.NaT``
        timedelta64 ``pd.NaT``
        =========== ==========

        The fill value is potentially specified in three ways. In order of
        precedence, these are

        1. The `fill_value` argument
        2. ``dtype.fill_value`` if `fill_value` is None and `dtype` is
           a ``SparseDtype``
        3. ``data.dtype.fill_value`` if `fill_value` is None and `dtype`
           is not a ``SparseDtype`` and `data` is a ``SparseArray``.

    kind : str
        Can be 'integer' or 'block', default is 'integer'.
        The type of storage for sparse locations.

        * 'block': Stores a `block` and `block_length` for each
          contiguous *span* of sparse values. This is best when
          sparse data tends to be clumped together, with large
          regions of ``fill-value`` values between sparse values.
        * 'integer': uses an integer to store the location of
          each sparse value.

    dtype : np.dtype or SparseDtype, optional
        The dtype to use for the SparseArray. For numpy dtypes, this
        determines the dtype of ``self.sp_values``. For SparseDtype,
        this determines ``self.sp_values`` and ``self.fill_value``.
    copy : bool, default False
        Whether to explicitly copy the incoming `data` array.

    Attributes
    ----------
    None

    Methods
    -------
    None

    Examples
    --------
    >>> from pandas.arrays import SparseArray
    >>> arr = SparseArray([0, 0, 1, 2])
    >>> arr
    [0, 0, 1, 2]
    Fill: 0
    IntIndex
    Indices: array([2, 3], dtype=int32)
    sparse_array
get_valuesr   _sparse_indexrS   _sparse_valuesr7   _dtypeNrJ   Fr   SparseIndexKindrU   r   re   rn   rR   Nonec              	   C  s  |d u rt |tr|j}t |t| r+|d u r|j}|d u r!|j}|d u r(|j}|j}t |trEzt|}W n t	yD   t
|}Y nw t |trT|d u rQ|j}|j}|d uratjdtt d |d urmt|smtdt|r|d ur||d u r|tj}|d urt|}n
|d u rd}n|j}t||d d}|j}|d urt
|}|d u rtjg |d}t|szt|d d}W n ty   |d u rt}ttj||d}n Y nw |r| }|d u r|d u r|jn|}	|	d u rtj}nt|	}t |t| r|d u r|j }tj|j|d}
nb|d u rUt!|dd}t |tj"sIt#|jrDtjd	|j d
t$t d tj|dd}|t%u rDt&dd}t|}t'||||d\}
}}ntj||d}
t|
|j(krnt)dt|
 d|| _ |
| _*t|
j|| _+d S )NzThe index argument has been deprecated and will be removed in a future version. Use a function like np.full to construct an array with the desired repeats of the scalar value instead.


stacklevelz$must only pass scalars with an indexrj   rT   r   T)extract_numpyzCreating SparseArray from z^ data loses timezone information. Cast to object before sparse to retain timezone information.zdatetime64[ns]r   ns)r   rX   rU   zNon array-like type z' must have the same length as the index),
isinstancer7   rX   typer{   rU   r   r`   construct_from_string	TypeErrorr-   rY   warningswarnFutureWarningr   r+   	ExceptionrV   nanlenlengthr    arrayr#   r:   rZ   object
atleast_1drW   re   r1   r   r9   ndarrayr&   UserWarningr   
datetime64make_sparsenpointsAssertionErrorr   r   )selfr   r   r   rX   r   rU   re   r   fill_value_dtypesparse_valuesrE   rE   rF   __init__s  s   

	
	


zSparseArray.__init__clstype[SparseArrayT]r   rM   c                 C  s    t | }||_||_||_|S N)r   __new__r   r   r   )r   r   r   rU   newrE   rE   rF   _simple_new  s
   
zSparseArray._simple_newr   rG   c           	      C  sv   |j \}}|dkrtd| d| }|  |j}|j}tjd|jd	 }t
|j|}t||}| |||S )a  
        Create a SparseArray from a scipy.sparse matrix.

        .. versionadded:: 0.25.0

        Parameters
        ----------
        data : scipy.sparse.sp_matrix
            This should be a SciPy sparse matrix where the size
            of the second dimension is 1. In other words, a
            sparse matrix with a single column.

        Returns
        -------
        SparseArray

        Examples
        --------
        >>> import scipy.sparse
        >>> mat = scipy.sparse.coo_matrix((4, 1))
        >>> pd.arrays.SparseArray.from_spmatrix(mat)
        [0.0, 0.0, 0.0, 0.0]
        Fill: 0.0
        IntIndex
        Indices: array([], dtype=int32)
        rj   z''data' must have a single column, not ''r   rT   )shaperZ   tocscsort_indicesr   indicesrV   r   rU   itemr7   r   r   )	r   r   r   ncolrQ   idxzerorU   r   rE   rE   rF   from_spmatrix  s   

zSparseArray.from_spmatrixNpDtype | Nonec                 C  s   | j }| jjdkr| jS |d u r7t| jjr|tu rtd}zt	| jjt
|}W n ty6   t}Y nw tj| j||d}| j|| jj< |S )Nr   r   rT   )rX   r{   r|   r   r%   rU   r   rV   r   result_typer   r   r   fullr   r   )r   rU   rX   outrE   rE   rF   	__array__J  s   
zSparseArray.__array__c                 C  s   d}t |)Nz8SparseArray does not support item assignment via setitem)r   )r   keyvaluemsgrE   rE   rF   __setitem__c  s   zSparseArray.__setitem__)rU   re   c                C  s   | ||dS NrT   rE   )r   scalarsrU   re   rE   rE   rF   _from_sequencej  s   zSparseArray._from_sequencec                 C  s   | ||j dS r   rT   )r   valuesoriginalrE   rE   rF   _from_factorizedn  s   zSparseArray._from_factorizedc                 C     | j S )zX
        The SparseIndex containing the location of non- ``fill_value`` points.
        )r   r   rE   rE   rF   r{   u  s   zSparseArray.sp_indexc                 C  r   )z
        An ndarray containing the non- ``fill_value`` values.

        Examples
        --------
        >>> s = SparseArray([0, 0, 1, 0, 2], fill_value=0)
        >>> s.sp_values
        array([1, 2])
        )r   r   rE   rE   rF   r   |  s   zSparseArray.sp_valuesc                 C  r   r   )r   r   rE   rE   rF   rU     s   zSparseArray.dtypec                 C     | j jS )z
        Elements in `data` that are `fill_value` are not stored.

        For memory savings, this should be the most common value in the array.
        )rU   rX   r   rE   rE   rF   rX     s   zSparseArray.fill_valuec                 C  s   t | jj|| _d S r   )r7   rU   rY   r   )r   r   rE   rE   rF   rX     s   c                 C  s   t | jtrdS dS )zW
        The kind of sparse index for this array. One of {'integer', 'block'}.
        rJ   rK   )r   r{   r   r   rE   rE   rF   r     s   zSparseArray.kindc                 C  s   | j }t|}|| S r   )r   r2   )r   sp_valsmaskrE   rE   rF   _valid_sp_values  s   zSparseArray._valid_sp_valuesintc                 C  r   r   )r{   r   r   rE   rE   rF   __len__  s   zSparseArray.__len__c                 C  r   r   )r   _is_na_fill_valuer   rE   rE   rF   _null_fill_value  s   zSparseArray._null_fill_valuec                 C  s   | j rt|S | j|kS r   )r   r0   rX   )r   rX   rE   rE   rF   _fill_value_matches  s   
zSparseArray._fill_value_matchesc                 C  s   | j j| jj S r   )r   nbytesr{   r   rE   rE   rF   r        zSparseArray.nbytesfloatc                 C  s   | j j| j j S )z
        The percent of non- ``fill_value`` points, as decimal.

        Examples
        --------
        >>> s = SparseArray([0, 0, 1, 1, 1], fill_value=0)
        >>> s.density
        0.6
        )r{   r   r   r   rE   rE   rF   density  s   zSparseArray.densityc                 C  r   )z
        The number of non- ``fill_value`` points.

        Examples
        --------
        >>> s = SparseArray([0, 0, 1, 1, 1], fill_value=0)
        >>> s.npoints
        3
        )r{   r   r   rE   rE   rF   r     s   zSparseArray.npointsc                 C  sf   t t| j}| jrt| t| j| j|S tj	t
| dtjd}t| j|| jj< t| |d|dS )NFrT   rX   rU   )r7   rn   r   r   r   r0   r   r{   rV   r   r   bool8r   )r   rU   r   rE   rE   rF   r0     s   zSparseArray.isnar   methodFillnaOptions | Nonelimit
int | Nonec                 C  s   |du r|du s|dur|durt d|dur8d}tj|tt d t| }t|||d t| || j	dS t
t| j|| j}| jrOt| jj|d}n| j}| || j|S )a  
        Fill missing values with `value`.

        Parameters
        ----------
        value : scalar, optional
        method : str, optional

            .. warning::

               Using 'method' will result in high memory use,
               as all `fill_value` methods will be converted to
               an in-memory ndarray

        limit : int, optional

        Returns
        -------
        SparseArray

        Notes
        -----
        When `value` is specified, the result's ``fill_value`` depends on
        ``self.fill_value``. The goal is to maintain low-memory use.

        If ``self.fill_value`` is NA, the result dtype will be
        ``SparseDtype(self.dtype, fill_value=value)``. This will preserve
        amount of memory used before and after filling.

        When ``self.fill_value`` is not NA, the result dtype will be
        ``self.dtype``. Again, this preserves the amount of memory used.
        Nz(Must specify one of 'method' or 'value'.z0fillna with 'method' requires high memory usage.r   )r   r   rX   )rZ   r   r   r   r   rV   rW   r=   r   rX   wherer0   r   r   r7   rU   rY   r   r   )r   r   r   r   r   
new_values	new_dtyperE   rE   rF   fillna  s*   &
zSparseArray.fillnarj   periodsc                 C  s   t | r|dkr|  S t|r| jj}t|| jj}|| jjkr,| t	|| j
}n| }| j|gtt|t |  |jd}|dkrN|}|d |  }n
|t|d  }|}|||gS )Nr   rT   )r   re   r0   rU   na_valuerV   r   rY   rz   r7   rX   r   minabs_concat_same_type)r   r   rX   rY   rQ   emptyabrE   rE   rF   shift!  s"   zSparseArray.shiftc                 C  sh   t | dks| jjt | krdS | jj}t |r|d dkr dS tjt|df }||dk  d S )zh
        Get the location of the first fill value.

        Returns
        -------
        int
        r   rb   rj   )r   r{   r   r   rV   r_diffargmax)r   r   r  rE   rE   rF   _first_fill_value_loc=  s   z!SparseArray._first_fill_value_locc                 C  sb   t | j}t| jt| kr'|  }tt | jd | }t||| j}t| j	|| j
dS r   )algosuniquer   r   r  rV   insertrX   r   r   rU   )r   uniquesfill_loc
insert_locrE   rE   rF   r	  R  s   zSparseArray.uniquec                 C  s   t | | jfS r   )rV   rW   rX   r   rE   rE   rF   _values_for_factorize`  r   z!SparseArray._values_for_factorizena_sentinelint | lib.NoDefaultuse_na_sentinelbool | lib.NoDefaulttuple[np.ndarray, SparseArray]c                 C  sX   t jt| ||d\}}|tju rd}|tju s|r!|||dk< t|| jd}||fS )N)r  r  r  rT   )r  	factorizerV   rW   r   
no_defaultrN   rU   )r   r  r  codesr  
uniques_sprE   rE   rF   r  d  s   


zSparseArray.factorizeTdropnarL   c           	      C  s   ddl m}m} tj| j|d\}}| jj}|dkrG| jr|sG| jr&t	|n|| j
k}| r8||  |7  < nt|d| j
}t|d|}t|tsQ||}n|}|||dS )a  
        Returns a Series containing counts of unique values.

        Parameters
        ----------
        dropna : bool, default True
            Don't include counts of NaN, even if NaN is in sp_values.

        Returns
        -------
        counts : Series
        r   )IndexrL   )r  r   )pandasr  rL   r  value_counts_arrayliker   r{   r|   r   r0   rX   r   rV   r
  r   r.   )	r   r  r  rL   keyscountsfcountsr   r   rE   rE   rF   value_countsx  s   

zSparseArray.value_countsqsnpt.NDArray[np.float64]interpolationr`   c                 C  s\   | j s	| jjdkr| j}tj|jtd}n|  }| 	 }t
|jdd}t|||||}|S )Nr   rT   Fcompat)r   r{   r|   r   rV   zerosr   rn   to_numpyr0   r1   rU   r4   )r   r   r"  npvaluesr   rX   
res_valuesrE   rE   rF   	_quantile  s   
zSparseArray._quantiler   r   r   c                 C     d S r   rE   r   r   rE   rE   rF   __getitem__     zSparseArray.__getitem__,SequenceIndexer | tuple[int | ellipsis, ...]c                 C  r*  r   rE   r+  rE   rE   rF   r,    s   .PositionalIndexer | tuple[int | ellipsis, ...]SparseArrayT | Anyc                 C  sl  t |trt|}|tu rtdt|r| |S t |tr'|  | }nt |tr|j	d u s6|j	dkr|j
d u r=dn|j
}|dk rJ|t| 7 }|jd u rSt| n|j}|dk r`|t| 7 }| jj}t||k||k @ }| j| }||  }|dkr||8 }ttt| | }	t|	|| j}
t| ||
| jS tjt| tjd| }| |S t|stdt |trt|rt |j!r| |jj|j S |j!s| |jjS t| }tj"|dtj#d}d||jj< | t|| S t$|}t%| |}t&'|rt(tj)|}| tjt|tjd| S t*|dr%| |S td	| d
t| || jdS )NzCannot slice with Ellipsisrj   r   rT   zuonly integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesTFr   zCannot slice with 'r   r   )+r   tupler<   rD   rZ   r(   _get_val_atr~   slicestepstartr   stopr{   r   rV   flatnonzeror   re   rangemake_sparse_indexr   r   r   rU   arangeint32taker)   
IndexErrorrN   r$   r0   rX   r   r   rW   r;   comis_bool_indexerr
   r   hasattr)r   r   
data_slicer6  endr   	keep_indsr   r{   new_lennew_sp_indexnr   rE   rE   rF   r,    sd   











c                 C  sD   t |t| }| j|}|dkr| jS | j| }t|| jj}|S )Nr  )r   r   r{   lookuprX   r   r"   rU   )r   locsp_locvalrE   rE   rF   r3  )  s   
zSparseArray._get_val_at)
allow_fillrX   rL  c                C  s   t |rtd| dtj|tjd}d }|jdkr&tjg dd}| j}n|r0| j||d}n| 	|S t
| || j| j|dS )Nz*'indices' must be an array, not a scalar 'z'.rT   r   r   r   )rX   r   rU   )r+   rZ   rV   rW   r<  sizer   rU   _take_with_fill_take_without_fillr   rX   r   )r   r   rL  rX   rU   r   rE   rE   rF   r=  4  s   

zSparseArray.takec                 C  s  |d u r| j j}| dk rtd| t| krtdt| dkrE|dk rAt	| j
t|}tj||d}|| |S td| j|}|dk}|dk| @ }| jjdkrn| rntj|j| j| j jd}|S | jjdkrt	| j jt|}tj|j||d}|S | j
|}|| dk }	|| dk }
|j }|	 rt	|t| j}||}| j||< |
 rt	|t|}||}|||< |S )Nr  zKInvalid value in 'indices'. Must be between -1 and the length of the array.!out of bounds value in 'indices'.r   rT   .cannot do a non-empty take from an empty axes.r   )rU   r   r   rZ   maxr   r>  rh   rV   r   r   r   
empty_liker   r{   lookup_arrayr   r   r   rX   rY   r=  r   rz   )r   r   rX   rU   taken
sp_indexernew_fill_indicesold_fill_indicesr   m0m1r   rE   rE   rF   rN  H  sP   



zSparseArray._take_with_fillc           	      C  s   |dk }t | }| |ks| | k r!|dkrtdtd| r1| }||  |7  < | j|}|dk}| j||  }t	
|jt	jdd}tt ||| jd}t| j||| jdS )	Nr   rQ  rP  r  Frd   r1  rT   )r   rR  r   r>  r   re   r{   rT  r   rV   r8  rz   r<  r:  r   r   r   rU   )	r   r   to_shiftrG  rV  
value_masknew_sp_valuesvalue_indicesrF  rE   rE   rF   rO    s   zSparseArray._take_without_fillr\   vArrayLike | objectsideLiteral['left', 'right']sorterrI   npt.NDArray[np.intp] | np.intpc                 C  sN   d}t j|tt d t|st|}t|}tj| | jjd	|||S )Nz(searchsorted requires high memory usage.r   rT   )
r   r   r   r   r+   rV   rW   rU   rY   searchsorted)r   r_  ra  rc  r   rE   rE   rF   re    s   

zSparseArray.searchsortedc                 C  s   | j  }| || j| jS r   )r   re   r   r{   rU   )r   r   rE   rE   rF   re     s   
zSparseArray.copy	to_concatSequence[SparseArrayT]c                 C  s  |d j }g }d}|r|d j}nd}|dkrIg }|D ]}|jj }||7 }||jj7 }||j || qt	|}	t	|}
t
||
}n?g }g }|D ]#}|j }||j ||j |  ||j ||jj7 }qOt	|}	t	|}t	|}t|||}| |	||dS )Nr   rJ   r   rX   )rX   r   r{   r   re   r   appendr   rV   concatenater   to_block_indexblocsblengthsr   )r   rf  rX   r   r   sp_kindr   rQ   int_idxr   indices_arrr{   rm  rl  	block_idx	blocs_arrblengths_arrrE   rE   rF   r     s<   






zSparseArray._concat_same_typeAstypeArg | Nonec                 C  sv   t || jr|s
| S |  S t|}t|ts tjdtt	 d | j
|}t|j}t| j||d}| || j|S )aE  
        Change the dtype of a SparseArray.

        The output will always be a SparseArray. To convert to a dense
        ndarray with a certain dtype, use :meth:`numpy.asarray`.

        Parameters
        ----------
        dtype : np.dtype or ExtensionDtype
            For SparseDtype, this changes the dtype of
            ``self.sp_values`` and the ``self.fill_value``.

            For other dtypes, this only changes the dtype of
            ``self.sp_values``.

        copy : bool, default True
            Whether to ensure a copy is made, even if not necessary.

        Returns
        -------
        SparseArray

        Examples
        --------
        >>> arr = pd.arrays.SparseArray([0, 0, 1, 2])
        >>> arr
        [0, 0, 1, 2]
        Fill: 0
        IntIndex
        Indices: array([2, 3], dtype=int32)

        >>> arr.astype(SparseDtype(np.dtype('int32')))
        [0, 0, 1, 2]
        Fill: 0
        IntIndex
        Indices: array([2, 3], dtype=int32)

        Using a NumPy dtype with a different kind (e.g. float) will coerce
        just ``self.sp_values``.

        >>> arr.astype(SparseDtype(np.dtype('float64')))
        ... # doctest: +NORMALIZE_WHITESPACE
        [nan, nan, 1.0, 2.0]
        Fill: nan
        IntIndex
        Indices: array([2, 3], dtype=int32)

        Using a SparseDtype, you can also change the fill value as well.

        >>> arr.astype(SparseDtype("float64", fill_value=0.0))
        ... # doctest: +NORMALIZE_WHITESPACE
        [0.0, 0.0, 1.0, 2.0]
        Fill: 0.0
        IntIndex
        Indices: array([2, 3], dtype=int32)
        zThe behavior of .astype from SparseDtype to a non-sparse dtype is deprecated. In a future version, this will return a non-sparse array with the requested dtype. To retain the old behavior, use `obj.astype(SparseDtype(dtype))`r   rd   )r'   r   re   r-   r   r7   r   r   r   r   rU   update_dtype_subtype_with_strr   r   r   r{   )r   rU   re   future_dtyperY   r   rE   rE   rF   rz     s"   9
	
zSparseArray.astypec                   sv   t  tr	   t  tjr" | j| j} fdd| jD }n | j} fdd| jD }t| || j	|dS )a  
        Map categories using an input mapping or function.

        Parameters
        ----------
        mapper : dict, Series, callable
            The correspondence from old values to new.

        Returns
        -------
        SparseArray
            The output array will have the same density as the input.
            The output fill value will be the result of applying the
            mapping to ``self.fill_value``

        Examples
        --------
        >>> arr = pd.arrays.SparseArray([0, 1, 2])
        >>> arr.map(lambda x: x + 10)
        [10, 11, 12]
        Fill: 10
        IntIndex
        Indices: array([1, 2], dtype=int32)

        >>> arr.map({0: 10, 1: 11, 2: 12})
        [10, 11, 12]
        Fill: 10
        IntIndex
        Indices: array([1, 2], dtype=int32)

        >>> arr.map(pd.Series([10, 11, 12], index=[0, 1, 2]))
        [10, 11, 12]
        Fill: 10
        IntIndex
        Indices: array([1, 2], dtype=int32)
        c                   s   g | ]}  |d qS r   )get.0xmapperrE   rF   
<listcomp>o  s    z#SparseArray.map.<locals>.<listcomp>c                   s   g | ]} |qS rE   rE   ry  r|  rE   rF   r~  r  s    rh  )
r   r/   to_dictr   Mappingrx  rX   r   r   r{   )r   r}  rX   r   rE   r|  rF   mapB  s   
(
zSparseArray.mapc                 C  s   t j| | jjdS )zr
        Convert SparseArray to a NumPy array.

        Returns
        -------
        arr : NumPy array
        rT   )rV   rW   r   rU   r   rE   rE   rF   r~   v  s   zSparseArray.to_densec                 C  s4   t || |}t|j| jd}t| j||d}|S )Nr   rT   )rV   r   r7   rU   rX   r   r   )r   r   r   naive_implementationrU   r   rE   rE   rF   _where  s   zSparseArray._wherec                 C  sX   t |tr$|\}\}}tg }|| || _|| _t|j|| _	dS | j
| dS )z*Necessary for making this object picklableN)r   r2  rV   r   __setstate__r   r   r7   rU   r   __dict__update)r   statend_staterX   r{   r   rE   rE   rF   r    s   


zSparseArray.__setstate__tuple[npt.NDArray[np.int32]]c                 C  s(   | j dkr
| jjfS | jj| jdk fS )Nr   )rX   r{   r   r   r   rE   rE   rF   nonzero  s   

zSparseArray.nonzeroskipnar_   r  c                K  sP   t | |d }|d u rtd| d| j |r| }n|  }t ||di |S )Nzcannot perform z with type rE   )r   r   rU   r  )r   r_   r  kwargsr   rQ   rE   rE   rF   _reduce  s   zSparseArray._reducec                 O  s:   t || | j}t|t| krt| jsdS | S )z
        Tests whether all elements evaluate True

        Returns
        -------
        all : bool

        See Also
        --------
        numpy.all
        F)nvvalidate_allr   r   rV   rh   rX   r   axisargsr  r   rE   rE   rF   rh     s
   zSparseArray.allr   c                 O  s>   t || | j}t|t| krt| jrdS |  S )z
        Tests whether at least one of elements evaluate True

        Returns
        -------
        any : bool

        See Also
        --------
        numpy.any
        T)r  validate_anyr   r   rV   r   rX   r   r  rE   rE   rF   r     s
   zSparseArray.anyr  	min_countr   c           
      O  s   t || | j}| }| jjdko| j }|r#|s#t| jj	ddS | jr7t
|jd|r5t| jj	ddS |S | jj}	t
|jd||	 rLt| jj	ddS || j|	  S )a  
        Sum of non-NA/null values

        Parameters
        ----------
        axis : int, default 0
            Not Used. NumPy compatibility.
        min_count : int, default 0
            The required number of valid values to perform the summation. If fewer
            than ``min_count`` valid values are present, the result will be the missing
            value indicator for subarray type.
        *args, **kwargs
            Not Used. NumPy compatibility.

        Returns
        -------
        scalar
        r   Fr#  N)r  validate_sumr   sumr{   r|   r   r1   rU   rY   r>   r   rX   )
r   r  r  r  r  r  
valid_valssp_sumhas_nansparserE   rE   rF   r    s   zSparseArray.sumc                 O  s\   t || |dur|| jkrtd| d| js"t|   S t| j | j	| j
dS )a   
        Cumulative sum of non-NA/null values.

        When performing the cumulative summation, any non-NA/null values will
        be skipped. The resulting SparseArray will preserve the locations of
        NaN values, but the fill value will be `np.nan` regardless.

        Parameters
        ----------
        axis : int or None
            Axis over which to perform the cumulative summation. If None,
            perform cumulative summation over flattened array.

        Returns
        -------
        cumsum : SparseArray
        Nzaxis(=z) out of boundsrh  )r  validate_cumsumndimrZ   r   rN   r~   cumsumr   r{   rX   )r   r  r  r  rE   rE   rF   r    s   zSparseArray.cumsumc                 O  sN   t || | j}| }t|}| jr|| S | jj}|| j|  ||  S )zb
        Mean of non-NA/null values

        Returns
        -------
        mean : float
        )	r  validate_meanr   r  r   r   r{   r|   rX   )r   r  r  r  r  r  ctr  rE   rE   rF   mean!  s   zSparseArray.mean)r  r  c                C     t || j | jd|dS )a4  
        Max of array values, ignoring NA values if specified.

        Parameters
        ----------
        axis : int, default 0
            Not Used. NumPy compatibility.
        skipna : bool, default True
            Whether to ignore NA values.

        Returns
        -------
        scalar
        rR  r  r  validate_minmax_axisr  _min_maxr   r  r  rE   rE   rF   rR  4     zSparseArray.maxc                C  r  )a4  
        Min of array values, ignoring NA values if specified.

        Parameters
        ----------
        axis : int, default 0
            Not Used. NumPy compatibility.
        skipna : bool, default True
            Whether to ignore NA values.

        Returns
        -------
        scalar
        r   r  r  r  rE   rE   rF   r   F  r  zSparseArray.minLiteral['min', 'max']c                 C  s   | j }| j o| jjdk}t|dkr=t|| }|r)|dkr!tnt}||| jS |r-|S | jjdkr5|S t	| j
jddS |rB| jS t	| j
jddS )z
        Min/max of non-NA/null values

        Parameters
        ----------
        kind : {"min", "max"}
        skipna : bool

        Returns
        -------
        scalar
        r   rR  Fr#  )r   r   r{   r|   r   r   rR  r   rX   r1   rU   rY   )r   r   r  r  has_nonnull_fill_vals
sp_min_maxfuncrE   rE   rF   r  X  s   zSparseArray._min_maxLiteral['argmin', 'argmax']c                 C  s   | j }| jj}tt|}|dkrtjntj}t|j	d }||  }||  }||| }	||	 }
t| j
r;|
S |dkrH| |
 | j
k rH|
S |dkrU| |
 | j
krU|
S |  }|dkr_|
S |S )Nr  r   argminr  )r   r   r   rV   rW   r0   r  r  r;  r   rX   r  )r   r   r   r   r   r  r   non_nansnon_nan_idx
_candidate	candidate_locrE   rE   rF   _argmin_argmax{  s&   


zSparseArray._argmin_argmaxc                 C  "   t |d |s| jrt| dS )Nr  r  r   _hasnaNotImplementedErrorr  r   r  rE   rE   rF   r       


zSparseArray.argmaxc                 C  r  )Nr  r  r  r  rE   rE   rF   r    r  zSparseArray.argminufuncnp.ufuncc                   s  | dd}|| D ]}t| jtf st  S q
tj ||g|R i |}|tur.|S d|v rBtj ||g|R i |}|S |dkrZtj	 ||g|R i |}|turZ|S t
|dkrt|| jfi |}	t|| jfi |}
|jdkrt fddt|	|
D }|S |dkr|	S  |	 jt|	j|
S tdd |D }t|||i |}|rt
|dkr|d }|S |jdkrt fd	d|D S |d
krd S t |S )Nr   rE   reducerj   c                 3  s,    | ]\}}  | jt|j|V  qd S r   )r   r{   r7   rU   )rz  sp_valuefvr   rE   rF   	<genexpr>  s    
z.SparseArray.__array_ufunc__.<locals>.<genexpr>c                 s  s    | ]}t |V  qd S r   )rV   rW   ry  rE   rE   rF   r    s    r   c                 3  s    | ]	}t  |V  qd S r   )r   ry  r   rE   rF   r    s    at)rx  r   _HANDLED_TYPESrN   NotImplementedops!maybe_dispatch_ufunc_to_dunder_opr3   dispatch_ufunc_with_outdispatch_reduction_ufuncr   r   r   rX   noutr2  zipr   r{   r7   rU   r   )r   r  r   inputsr  r   r{  r   resr   rX   arrays
new_inputsrE   r   rF   __array_ufunc__  sr   

zSparseArray.__array_ufunc__c                 C  sT  |j }t|trt| |||S t|r]tjdd |t| t|}|| j	|}W d    n1 s4w   Y  |dkrU|\}}|\}}	t
||| j|t
||| j|	fS t
||| j|S t|}tjdd8 t| t|krtdt|  dt| t|tst|dd }
t|| j|
d}t| |||W  d    S 1 sw   Y  d S )Nrf   rg   rx   zlength mismatch: z vs. rU   r   )rA   r   rN   r   r+   rV   r}   r[   rW   r   r   r{   r   r   r   rX   )r   otherr^   op_namer   r   r\   r]   lfillrfillrU   rE   rE   rF   _arith_method  s4   


$zSparseArray._arith_methodc                 C  s   t |st|t| st|}t|tjrt|| jd}t|trEt| t|kr8t	dt|  dt| |j
d}t| |||S tjdd# || j|}tjt| |tjd}|| j||| jj< W d    n1 sqw   Y  t| ||tjdS )	Nr   z operands have mismatched length z and rq   rf   rg   rT   r   )r+   r   r   rV   rW   r   rN   rX   r   rZ   rA   stripr   r}   r   bool_r   r{   r   )r   r  r^   r  rX   r   rE   rE   rF   _cmp_method  s*   

zSparseArray._cmp_methodc                 C  sn   |t | j }t| jj|}t| js|| jkr+|| j}t	| 
|| j| jS t	| ||  |dS r   )rV   r   rX   r   r7   rU   rY   r0   r   r   r   r{   r~   )r   r^   rX   rU   r   rE   rE   rF   _unary_method/  s   
zSparseArray._unary_methodc                 C     |  tjS r   )r  operatorposr   rE   rE   rF   __pos__:     zSparseArray.__pos__c                 C  r  r   )r  r  negr   rE   rE   rF   __neg__=  r  zSparseArray.__neg__c                 C  r  r   )r  r  invertr   rE   rE   rF   
__invert__@  r  zSparseArray.__invert__c                 C  r  r   )r  r  r   r   rE   rE   rF   __abs__C  r  zSparseArray.__abs__c                 C  s6   t | }t | j}t | j}| d| d| S )Nz
Fill: 
)printingpprint_thingrX   r{   )r   pp_strpp_fillpp_indexrE   rE   rF   __repr__I  s   
zSparseArray.__repr__c                 C  r*  r   rE   )r   boxedrE   rE   rF   
_formatterO  s   zSparseArray._formatter)NNNrJ   NF)r   r   rU   r   re   rn   rR   r   )
r   r   r   rS   r   r   rU   r7   rR   rM   )r   r   r   rG   rR   rM   r   )rU   r   rR   rS   )rU   r   re   rn   )rR   r   )rR   rS   )rR   r7   )rR   r   )rR   r   )rR   rn   )rR   r   )NNN)r   rM   r   r   r   r   rR   rM   )rj   N)r   rM   r   r   rR   rM   )r   rM   rR   rM   )r  r  r  r  rR   r  )T)r  rn   rR   rL   )r   r!  r"  r`   )r   r   rR   r   )r   rM   r   r.  rR   rM   )r   rM   r   r/  rR   r0  )r   rM   rL  rn   rR   rM   )r\   N)r_  r`  ra  rb  rc  rI   rR   rd  )r   r   rf  rg  rR   rM   )NT)rU   rt  re   rn   )rR   r   )rR   r  )r_   r`   r  rn   )r   )r   r   T)r  r   r  r   r  rn   rR   r   )r  r   rR   rN   )r  r   r  rn   )r   r  r  rn   rR   r   )r   r  rR   r   )r  rn   rR   r   )r  r  r   r`   )rR   rN   )rR   r`   )F)UrA   rB   rC   __doc___subtypr8   _hidden_attrs	frozenset__annotations__r   classmethodr   r   r   r   r   r   propertyr{   r   rU   rX   setterr   r   r   r   r   r   r   r   r0   r   r  r  r	  r  r   r  r  r  r)  r   r,  r3  r=  rN  rO  re  re   r   rz   r  r~   r  r  r  r  rh   r   r  r  r  rR  r   r  r  r  r  rV   r   numbersNumberr  r  r  r  _logical_methodr  r  r  r  r  r  r  rE   rE   rE   rF   rN      s   
 L -


	

B

(
d
A
9
U
4





'
 

#
F!rK   r   r   r   c           	      C  s   t | tjsJ | jdkrtd|du rt| j}t|r#t| }nt	| jr-| 
t} t| jr9t| |}n| |k}t| }|t|krL|jj}n
| d 
tj}t|||}| | }|durjt||d}|||fS )a@  
    Convert ndarray to sparse format

    Parameters
    ----------
    arr : ndarray
    kind : {'block', 'integer'}
    fill_value : NaN or another value
    dtype : np.dtype, optional
    copy : bool, default False

    Returns
    -------
    (sparse_values, index, fill_value) : (ndarray, SparseIndex, Scalar)
    rj   zexpected dimension <= 1 dataNr   rT   )r   rV   r   r  r   r1   rU   r0   r2   r,   rz   r   r*   r   make_mask_object_ndarrayr   r{   r   r  r<  r:  r   )	rQ   r   rX   rU   r   r   r   r   sparsified_valuesrE   rE   rF   r   U  s.   







r   r   r   Literal['block']r   c                 C  r*  r   rE   r   r   r   rE   rE   rF   r:    r-  r:  Literal['integer']r   c                 C  r*  r   rE   r  rE   rE   rF   r:    r-  r   c                 C  sD   |dkrt |\}}t| ||}|S |dkrt| |}|S td)NrK   rJ   zmust be block or integer type)r   
get_blocksr   r   rZ   )r   r   r   locslensr   rE   rE   rF   r:    s   
)rQ   rN   rR   rS   )
r\   rN   r]   rN   r^   r   r_   r`   rR   rN   r   )r_   r`   rU   r   rR   rN   )rK   NN)rQ   rS   r   r   rU   r   )r   r   r   r  rR   r   )r   r   r   r  rR   r   )r   r   r   r   rR   r   )r  
__future__r   collectionsr   r  r  typingr   r   r   r   r   r	   r
   r   r   numpyrV   pandas._libsr   pandas._libs.sparse_libssparser   r   r   r   pandas._libs.tslibsr   pandas._typingr   r   r   r   r   r   r   r   r   pandas.compat.numpyr   r  pandas.errorsr   pandas.util._exceptionsr   pandas.util._validatorsr   r   pandas.core.dtypes.astyper   pandas.core.dtypes.castr    r!   r"   pandas.core.dtypes.commonr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   pandas.core.dtypes.genericr.   r/   pandas.core.dtypes.missingr0   r1   r2   pandas.corer3   pandas.core.algorithmscore
algorithmsr   pandas.core.array_algos.quantiler4   pandas.core.arrayliker5   pandas.core.arraysr6   pandas.core.arrays.sparse.dtyper7   pandas.core.baser8   pandas.core.commoncommonr?  pandas.core.constructionr9   r:   pandas.core.indexersr;   r<   pandas.core.missingr=   pandas.core.nanopsr>   pandas.core.opsr  pandas.io.formats.printingioformatsr  enumr?   r@   rD   scipy.sparserG   rH   rI   r   r  rL   r   rM   _sparray_doc_kwargsr[   r   r   rN   r   r:  rE   rE   rE   rF   <module>   s    (
,4

m            C?