
    hI                    N   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 ddlZddlZddlmZ ddlmc mZ ddlmZmZ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$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZA ddlBmCc mDZE ddlFmGZG ddlHmIZI ddlJmKZK ddlLmCc mMZN ddlOmPZPmQZQmRZR ddlSmTZTmUZU ddlVmWZW ddlXmYZY erYddlZm[Z[ ddl\m]Z]  G d de]      Z^e^j                  Z_dd l`maZa dd!lbmcZcmdZd ed"   Zedd#lbmfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZq dd$lrmsZs n ete_      Z^d%d&iZud/d'Zv	 	 	 	 	 	 	 	 	 	 d0d(Zw	 d1	 	 	 	 	 d2d)Zx G d* d&eGeKeI      Zy	 	 	 d3	 	 	 	 	 d4d+Zzed5d,       Z{ed6d-       Z{d7d.Z{y)8z
SparseArray data structure
    )annotations)abcN)TYPE_CHECKINGAnyCallableLiteralcastoverload)lib)
BlockIndexIntIndexSparseIndex)NaT)function)PerformanceWarning)doc)find_stack_level)validate_bool_kwargvalidate_insert_loc)astype_array)"construct_1d_arraylike_from_scalarfind_common_typemaybe_box_datetimelike)is_bool_dtype
is_integeris_list_likeis_object_dtype	is_scalaris_string_dtypepandas_dtype)DatetimeTZDtypeSparseDtype)ABCIndex	ABCSeries)isnana_value_for_dtypenotna)	arraylike)OpsMixin)ExtensionArray)PandasObject)ensure_wrapped_if_datetimelikeextract_arraysanitize_array)check_array_indexerunpack_tuple_and_ellipses)check_below_min_count)printing)Sequence)Enumc                      e Zd ZdZy)ellipsisz...N)__name__
__module____qualname__Ellipsis     DD:\jyotish\venv\Lib\site-packages\pandas/core/arrays/sparse/array.pyr6   r6   [   s    r<   r6   )spmatrix)FillnaOptionsNumpySorter)integerblock)	ArrayLike	AstypeArgAxisAxisIntDtypeNpDtypePositionalIndexerScalarScalarIndexerSelfSequenceIndexernpt)SeriesklassSparseArrayc                    	 t        j                  | j                  | j                  j                        S # t
        $ r" t        j                  | j                        cY S w xY 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_valuerT   subtype
ValueError)arrs    r=   	_get_fillr[      sH    $*zz#..		0A0ABB *zz#..))*s   47 (A"!A"c           	        |j                  d      r|dd }| j                  j                  }|j                  j                  }||k7  rlt        ||g      }t	        || j
                        }t	        ||j
                        }| j                  |d      } |j                  |d      }|j                  }n|}d}| j                  j                  dk(  s|j                  j                  dk(  rt        j                  d	      5   || j                         |j                               }	 |t        |       t        |            }
ddd       | j                  j                  dk(  r| j                  }n|j                  }n| j                  j                  |j                        ret        j                  d	      5   || j                  |j                        }	 |t        |       t        |            }
ddd       | j                  }nX|d   d
k(  r	|| }} |dd }|dv rd|dk(  r_d| d}| j                  j                  t        j                         }|j                  j                  t        j                         }t"        }n d| d| }| j                  }|j                  }|dv rS|dk(  j%                         r@| j                  j&                  dv r(d| d}|j                  d      }|j                  d      }t)        t*        |      }t        j                  d	      5   ||| j                  | j
                  ||j                  |j
                        \  }	}}
ddd       |dk(  r,t-        |	d   
d   |      t-        ||	d   ||
d   |      fS |	j                  }t-        |	
|      S # 1 sw Y   pxY w# 1 sw Y   xY w# 1 sw Y   rxY w)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iu_float64float64divmodrS   )
startswithrT   rX   r   r"   rW   astypesp_indexngapsrU   errstateto_denser[   equals	sp_valuesviewuint8rj   anykindgetattrsplib_wrap_result)leftrightopnameltypertyperX   rT   result_dtyperesultfillindexopnameleft_sp_valuesright_sp_values	sparse_ops                   r=   _sparse_array_opr      sW   & tAbz JJEKKE~"E5>2GT__5GU%5%56{{5u{-U/ L}}a5>>#7#71#<[[X&)9:Fioy'78D ' ==!#MMENNE			enn	-[[X&8Fioy'78D ' 7c>%D8D''EVOtfF+F!^^00:N#oo22288<OLtfAeW-F!^^N#ooO ''!  "

4' tfH-F+229=N-44Y?OE6*	[[X&"+  #FE4 ' x
 vay%aMvay%aM
 	

 ||feTFF '& '&@ '&s%    AN/99N<:O	/N9<O	Oc                    | j                  d      r| dd } | dv rt        }t        j                  |      }t	        |      rt        |      }t        ||||      S )z.
    wrap op result to have correct dtype
    r]   r^   r_   )eqneltgtlege)sparse_indexrW   rT   )rt   rj   r   item_from_zerodimr   rQ   )r   datar   rW   rT   s        r=   r   r     sa     tAbz33&&z2JU*%
<Je r<   c                      e Zd ZU dZdZej                   eg       z  Zded<   ded<   ded<   	 	 	 	 	 dU	 	 	 	 	 	 	 dVdZ	e
	 	 	 	 	 	 	 	 dWd       Ze
dXd       Z	 dY	 	 	 	 	 dZdZd[dZe
d	d
dd\d       Ze
d        Zed]d       Zed^d       Zed_d       Zed        Zej,                  d[d       Zed`d       Zed^d       ZdadZedbd       ZdbdZedad       Zedcd       Zedad       Zddd Zd	d	d!d"	 	 	 	 	 	 	 	 	 de fd#Z 	 	 	 	 df	 	 	 	 	 	 	 dg fd$Z!dhdid%Z"d& Z# e$e%jL                        	 dj	 	 	 dkd'       Z&ddd(Z'd) Z(	 dl	 	 	 dmd*Z)dldnd+Z*e+dod,       Z,e+	 	 	 	 dpd-       Z,	 	 	 	 dqd.Z,d/ Z-d
d	d0drd1Z.dsd^d2Z/ddd3Z0	 	 dt	 	 	 	 	 	 	 dud4Z1ddd5Z2e
dvd6       Z3dwdxd7Z4dsddd8Z5d^d9Z6d: Z7d[d;Z8dyd<Z9d!d
d=	 	 	 	 	 dzd>Z:dsd?Z;d{d|d@Z<	 	 	 d}	 	 	 	 	 	 	 d~dAZ=d{ddBZ>d{ddCZ?d	d!dDddEZ@d	d!dDddFZAddGZBddHZCdlddIZDdlddJZEeFj                  eHj                  fZJddKZKdL ZLddMZMeMZNddNZOddOZPddPZQddQZRddRZSddSZTdddTZU xZVS )rQ   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
    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_arrayr   _sparse_index
np.ndarray_sparse_valuesr"   _dtypeNFc                :   |t        |t              r|j                  }t        |t        |             r6||j                  }||j                  }||j
                  }|j                  }t        |t              r	 t        j                  |      }t        |t              r||j                  }|j                  }t        |      rft        j                  dt        |       j                   dt         t#                      |d}n|j$                  }t'        ||d       }|j
                  }|t        |      }|t)        j*                  g |      }	 t-        |d       }|r|j7                         }|.||j
                  n|}|t(        j8                  }nt;        |      }t        |t        |             r1|/|j<                  }t)        j4                  |j                  |      }	n|t?        |d      }t        |t(        j@                        st        |j
                  tB              rgt        j                  d	|j
                   d
tD        t#                      t)        j4                  |d      }|tF        u rt)        jH                  dd      }t)        j4                  |      }tK        ||||      \  }	}}nGt)        j4                  ||      }	tM        |	      |jN                  k7  rtQ        dt        |	       d      || _        |	| _)        t        |	j
                  |      | _*        y # t        $ r t        |      }Y w xY w# t.        $ rK |Dt)        j
                  t0              }t)        j2                  t)        j4                  ||            }n Y :w xY w)NzConstructing z\ with scalar data is deprecated and will raise in a future version. Pass a sequence instead.
stacklevelrf   rS   )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   rW   rT   zNon array-like type z' must have the same length as the index)+
isinstancer"   rW   typerv   rT   r{   strconstruct_from_string	TypeErrorr    rX   r   warningswarnr7   FutureWarningr   lengthr   rU   arrayr.   rY   object
atleast_1drV   ra   nanr&   r   r-   ndarrayr!   UserWarningr   
datetime64_make_sparselennpointsAssertionErrorr   r   )
selfr   r   rW   r   rT   ra   r   fill_value_dtypesparse_valuess
             r=   __init__zSparseArray.__init__l  sL    *UK"@))JdDJ'##}}!!__
}

>>D eS!,#99%@ e[)!"--
MMET?MMT
 3 34 5O O+-	 #&--5dG4PDJJE 'E < 88Be,D		!$d3D 99;D-2]tzz'VV
/0@A
dDJ'L,@--L JJeM ! T:DdBJJ/djj/:MM4TZZL AA A $#3#5 ::d2BCD!S(%']]5$%?
zz$'6B %73M< JJt59M=!\%9%99$*4+>*? @8 8  *+!-"5"5zBM  ,$U+,N  	 }(}}RZZE%BC 	s%   ;L+ M +MMANNc                Z    t         j                  |       }||_        ||_        ||_        |S N)r   __new__r   r   r   )clsr   r   rT   news        r=   _simple_newzSparseArray._simple_new  s.     nnS!()

r<   c                   |j                   \  }}|dk7  rt        d| d      |j                         }|j                          |j                  }|j
                  }t        j                  d|j                        j                         }t        |j                  |      }t        ||      }| j                  |||      S )a  
        Create a SparseArray from a scipy.sparse matrix.

        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)
        rf   z''data' must have a single column, not ''r   rS   )shaperY   tocscsort_indicesr   indicesrU   r   rT   itemr"   r   r   )	r   r   r   ncolrZ   idxzerorT   r   s	            r=   from_spmatrixzSparseArray.from_spmatrix  s    4 zz19FtfANOO zz|iillxx+002CIIt,%sE511r<   c                   | j                   j                  dk(  r/|du rt        j                  | j                        S | j                  S |du r$t        j                  dt        t                      | j                  }|t| j                  j                  j                  dk(  r|t        u rt        j                  d      }	 t        j                  | j                  j                  t        |            }t        j$                  | j&                  ||      }| j                  || j                   j(                  <   |S # t         $ r	 t"        }Y Xw xY w)	Nr   TFaS  Starting with NumPy 2.0, the behavior of the 'copy' keyword has changed and passing 'copy=False' raises an error when returning a zero-copy NumPy array is not possible. pandas will follow this behavior starting with pandas 3.0.
This conversion to NumPy requires a copy, but 'copy=False' was passed. Consider using 'np.asarray(..)' instead.r   Mr   rS   )rv   rw   rU   r   r{   r   r   r   r   rW   rT   r   r   r   result_typer   r   r   fullr   r   )r   rT   ra   rW   outs        r=   	__array__zSparseArray.__array__*  s    ==!#t|xx//~~%5=MM2 +-	 __
= ~~##((C/ $!#u!5Jt~~';';T*=MN ggdjj*E:%)^^DMM!!"
  s    3D: :EEc                    d}t        |      )Nz8SparseArray does not support item assignment via setitem)r   )r   keyvaluemsgs       r=   __setitem__zSparseArray.__setitem__U  s     Inr<   rT   ra   c                    | ||      S NrS   r;   )r   scalarsrT   ra   s       r=   _from_sequencezSparseArray._from_sequence\  s    7%((r<   c                *     | ||j                         S r   rS   )r   valuesoriginals      r=   _from_factorizedzSparseArray._from_factorized`  s    600r<   c                    | j                   S )zX
        The SparseIndex containing the location of non- ``fill_value`` points.
        )r   r   s    r=   rv   zSparseArray.sp_indexg  s    
 !!!r<   c                    | j                   S )a  
        An ndarray containing the non- ``fill_value`` values.

        Examples
        --------
        >>> from pandas.arrays import SparseArray
        >>> s = SparseArray([0, 0, 1, 0, 2], fill_value=0)
        >>> s.sp_values
        array([1, 2])
        )r   r   s    r=   r{   zSparseArray.sp_valuesn  s     """r<   c                    | j                   S r   )r   r   s    r=   rT   zSparseArray.dtype|  s    {{r<   c                .    | j                   j                  S )a  
        Elements in `data` that are `fill_value` are not stored.

        For memory savings, this should be the most common value in the array.

        Examples
        --------
        >>> ser = pd.Series([0, 0, 2, 2, 2], dtype="Sparse[int]")
        >>> ser.sparse.fill_value
        0
        >>> spa_dtype = pd.SparseDtype(dtype=np.int32, fill_value=2)
        >>> ser = pd.Series([0, 0, 2, 2, 2], dtype=spa_dtype)
        >>> ser.sparse.fill_value
        2
        )rT   rW   r   s    r=   rW   zSparseArray.fill_value  s    " zz$$$r<   c                N    t        | j                  j                  |      | _        y r   )r"   rT   rX   r   )r   r   s     r=   rW   zSparseArray.fill_value  s    !$**"4"4e<r<   c                :    t        | j                  t              ryy)zW
        The kind of sparse index for this array. One of {'integer', 'block'}.
        rA   rB   )r   rv   r   r   s    r=   r   zSparseArray.kind  s    
 dmmX.r<   c                :    | j                   }t        |      }||   S r   )r{   r'   )r   sp_valsmasks      r=   _valid_sp_valueszSparseArray._valid_sp_values  s    ..W~t}r<   c                .    | j                   j                  S r   )rv   r   r   s    r=   __len__zSparseArray.__len__  s    }}###r<   c                .    | j                   j                  S r   )r   _is_na_fill_valuer   s    r=   _null_fill_valuezSparseArray._null_fill_value  s    {{,,,r<   c                N    | j                   rt        |      S | j                  |k(  S r   )r   r%   rW   )r   rW   s     r=   _fill_value_matcheszSparseArray._fill_value_matches  s%      
##??j00r<   c                \    | j                   j                  | j                  j                  z   S r   )r{   nbytesrv   r   s    r=   r   zSparseArray.nbytes  s!    ~~$$t}}';';;;r<   c                \    | j                   j                  | j                   j                  z  S )z
        The percent of non- ``fill_value`` points, as decimal.

        Examples
        --------
        >>> from pandas.arrays import SparseArray
        >>> s = SparseArray([0, 0, 1, 1, 1], fill_value=0)
        >>> s.density
        0.6
        )rv   r   r   r   s    r=   densityzSparseArray.density  s#     }}$$t}}';';;;r<   c                .    | j                   j                  S )z
        The number of non- ``fill_value`` points.

        Examples
        --------
        >>> from pandas.arrays import SparseArray
        >>> s = SparseArray([0, 0, 1, 1, 1], fill_value=0)
        >>> s.npoints
        3
        )rv   r   r   s    r=   r   zSparseArray.npoints  s     }}$$$r<   c                   t        t        | j                        }| j                  r9t        |       j	                  t        | j                        | j                  |      S t        j                  t        |       dt        j                        }t        | j                        || j                  j                  <    t        |       |d|      S )NFrS   rW   rT   )r"   rj   r   r   r   r%   r{   rv   rU   r   r   bool_r   )r   rT   r   s      r=   r%   zSparseArray.isna  s     D$"7"78  :))$t~~*>uUUwws4y%rxx8&*4>>&:T]]""#tDz$5>>r<   T)limit
limit_areara   c               *    t         |   ||||      S )N)methodr   r   ra   )super_pad_or_backfill)r   r  r   r   ra   	__class__s        r=   r  zSparseArray._pad_or_backfill  s%     w':D ( 
 	
r<   c                f   ||||t        d      |t        | 	  ||      S t        j                  t        | j                        || j                        }| j                  r"t        | j                  j                  |      }n| j                  }| 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

        copy: bool, default True
            Ignored for SparseArray.

        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.
        z(Must specify one of 'method' or 'value'.)r  r   rW   )rY   r  fillnarU   wherer%   r{   r   r"   rT   rX   r   r   )r   r   r  r   ra   
new_values	new_dtyper  s          r=   r  zSparseArray.fillna  s    T Nu}5#4GHH7>u>== $t~~"6t~~NJ$$'

(:(:uM	 JJ	
D,>,>	JJr<   c           	     6   t        |       r|dk(  r| j                         S t        |      r| j                  j                  }t        j                  || j                  j                        }|| j                  j                  k7  r&| j                  t        || j                              }n| }| j                  |gt        t        |      t        |             z  |j                        }|dkD  r	|}|d |  }n|t        |      d  }|}|j                  ||g      S )Nr   rS   )r   ra   r%   rT   na_valuerU   r   rX   ru   r"   rW   r   minabs_concat_same_type)r   periodsrW   rX   rZ   emptyabs           r=   shiftzSparseArray.shift+  s    4yGqL99;
,,J..TZZ-?-?@djj(((++k'4??CDCC##L3s7|SY77syy $ 
 Q;AIgXACLN#AA$$aV,,r<   c                :   t        |       dk(  s"| j                  j                  t        |       k(  ry| j                  j                  }t        |      r|d   dkD  ryt        j
                  t	        j                  |      df   }||dkD  j                            dz   S )zh
        Get the location of the first fill value.

        Returns
        -------
        int
        r   r^   rf   )r   rv   r   r   rU   r_diffargmax)r   r   r  s      r=   _first_fill_value_locz!SparseArray._first_fill_value_locF  s     t9>T]]22c$i?--''7|wqzA~
 uuRWWW%q()q((*+a//r<   c                    t        j                  |       }t        j                  | j                               }t        j                  |||      S )N)keepr   )rU   rV   r%   algos
duplicated)r   r  r   r   s       r=   r  zSparseArray.duplicated[  s;     D!zz$))+&T==r<   c                   t        j                  | j                        }t        | j                        t        |       k7  r\| j	                         }t        t        j                  | j                  d |             }t        j                  ||| j                        }t        |       j                  || j                        S r   )r  uniquer{   r   r  rU   insertrW   r   r   rT   )r   uniquesfill_loc
insert_locs       r=   r   zSparseArray.uniquec  s    ,,t~~.t~~#d)+113H U\\$..(*CDEJiiT__EGDz((

(CCr<   c                D    t        j                  |       | j                  fS r   )rU   rV   rW   r   s    r=   _values_for_factorizez!SparseArray._values_for_factorizeq  s    zz$00r<   c                    t        j                  t        j                  |       |      \  }}t	        || j
                        }||fS )N)use_na_sentinelrS   )r  	factorizerU   rV   rQ   rT   )r   r(  codesr"  
uniques_sps        r=   r)  zSparseArray.factorizeu  sA     JJto
w !

;
j  r<   c                   ddl m}m} t        j                  | j
                  |      \  }}}| j                  j                  }|dkD  r| j                  r|s|| j                  rt        |      n|| j                  k(  }|j                         r||xx   |z  cc<   n8t        j                  |d| j                        }t        j                  |d|      }t        |t              s	 ||      }	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   )IndexrO   )dropnaF)r   ra   )pandasr-  rO   r  value_counts_arrayliker{   rv   rw   r   r%   rW   r~   rU   r!  r   r#   )
r   r.  r-  rO   keyscountsrm   fcountsr   r   s
             r=   value_countszSparseArray.value_counts  s    	

  66t~~fUfa--%%Q; 5 5V!%!6!64:DDOO<SDxxzt' yyq$//:61g6$)$KEEfE66r<   c                     y r   r;   r   r   s     r=   __getitem__zSparseArray.__getitem__  s    r<   c                     y r   r;   r6  s     r=   r7  zSparseArray.__getitem__  s    
 	r<   c                   t        |t              rt        |      }|t        u rt	        d      t        |      r| j                  |      S t        |t              r| j                         |   }nft        |t              r~|j                  |j                  dk(  r |j                  dn|j                  }|dk  r|t        |       z  }|j                  t        |       n|j                  }|dk  r|t        |       z  }| j                  j                  }t        j                   ||k\  ||k  z        }| j"                  |   }||   j%                         }|dkD  r||z  }t        t'        t        |             |         }	t)        |	|| j*                        }
t-        |       j/                  ||
| j0                        S t        j2                  t        |       t        j4                        |   }| j7                  |      S t9        |      st;        d      t        |t<              r	t?        |      rtA        |jB                        r2| j7                  |j                  j                  |j"                           S |jB                  s%| j7                  |j                  j                        S t        |       }t        jD                  |dt        jF                        }d||j                  j                  <   | j7                  t        j2                  |      |         S t        jH                  |      }tK        | |      }tM        jN                  |      rZtQ        t        jR                  |      }| j7                  t        j2                  t        |      t        j4                        |         S tU        |d      r| j7                  |      S t	        d	| d
       t-        |       || j*                        S )NzCannot slice with Ellipsisrf   r   rS   zuonly integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesTFr   zCannot slice with 'r   r   )+r   tupler0   r:   rY   r   _get_val_atry   slicestepstartr   stoprv   r   rU   flatnonzeror{   ra   rangemake_sparse_indexr   r   r   rT   arangeint32taker   
IndexErrorrQ   r   r%   rW   r   r   rV   r/   comis_bool_indexerr	   r   hasattr)r   r   
data_slicer?  endr   	keep_indsr   rv   new_lennew_sp_indexnr   s                r=   r7  zSparseArray.__getitem__  s    c5!+C0Ch !=>>c?##C((U# -JU#xx388q= YY.CII19SY&E#&88#3c$i73t9$C--//NNGu,<3+OP	..3"9-224 19%H eCI.s340(DIINDz--g|TZZPP))CIRXX>sCyy))c" L  #{+
 !%CNN+#yy)=)=cmm)LMM>>#yy)=)=>>D	A771d"((;D16D--.99RYYq\$%788**S/C%dC0C""3'2::s+yy3s8288!DS!IJJi(yy~% #6se1!=>>tDz*49955r<   c                    t        |t        |             }| j                  j                  |      }|dk(  r| j                  S | j
                  |   }t        || j
                  j                        }|S )Nr  )r   r   rv   lookuprW   r{   r   rT   )r   locsp_locvals       r=   r<  zSparseArray._get_val_at  s`    !#s4y1%%c*R<??"..(C(dnn.B.BCCJr<   )
allow_fillrW   c                  t        |      rt        d| d      t        j                  |t        j                        }d }|j
                  dk(  r$t        j                  g d      }| j                  }n'|r| j                  ||      }n| j                  |      S  t        |       || j                  | j                  |      S )Nz*'indices' must be an array, not a scalar 'z'.rS   r   r   r  )rW   r   rT   )r   rY   rU   rV   rE  sizer   rT   _take_with_fill_take_without_fillr   rW   r   )r   r   rV  rW   rT   r   s         r=   rF  zSparseArray.take#  s    WI'RTUVV**WBHH5<<1XXb1FJJE))'j)IF**733tDztTYYe
 	
r<   c                   || j                   j                  }|j                         dk  rt        d      |j	                         t        |       k\  rt        d      t        |       dk(  rq|dk(  j                         rSt        j                  | j                  t        |            }t        j                  ||      }|j                  |       |S t        d      | j                  j                  |      }|dk(  }|dk(  | z  }| j                  j                   dk(  rR|j                         rBt        j"                  |j$                  | j&                  | j                   j(                        }|S | j                  j                   dk(  rft        j                  | j                   j(                  t        |            }t        j"                  |j$                  ||      }| j&                  ||<   |S | j                  j+                  |      }||   dk  }	||   dk  }
|j                   }|	j-                         rIt        j                  |t        | j&                              }|j/                  |      }| j&                  ||<   |
j-                         r5t        j                  |t        |            }|j/                  |      }|||<   |S )Nr  zKInvalid value in 'indices'. Must be between -1 and the length of the array.!out of bounds value in 'indices'.r   rS   .cannot do a non-empty take from an empty axes.r   )rT   r  r  rY   maxr   rG  rd   rU   r   r{   r   
empty_liker   rv   lookup_arrayr   r   r   rW   rX   rF  r~   ru   )r   r   rW   rT   taken
sp_indexernew_fill_indicesold_fill_indicesr   m0m1r   s               r=   rY  zSparseArray._take_with_fill5  sX   ,,J;;=2/ 
 ;;=CI%@AAt9>2""$t~~tJ7GHgU;

:& !QRR
 ]]//8
"b=&",1A0AA==  A%*:*>*>*@GG  T__DJJDVDVE@ 9 ]]""a'^^DJJ$6$6Z8HIFGGJ,,6RE&*ooE"#0 - NN''
3E ,-1B,-1B++Kvvx nn[$t:OP[1*.//&'vvx nn[$z:JK[1*4&'r<   c                \   |dk  }t        |       }|j                         |k\  s|j                         | k  r|dk(  rt        d      t        d      |j	                         r|j                         }||xx   |z  cc<   | j                  j                  |      }|dk7  }| j                  ||      }t        j                  |      j                  t        j                  d      }t        t        |      || j                        }t        |       j!                  ||| j"                        S )	Nr   r]  r\  r  Fr`   r:  rS   )r   r^  r  rG  r~   ra   rv   r`  r{   rU   rA  ru   rE  rC  r   r   r   rT   )	r   r   to_shiftrP  rb  
value_masknew_sp_valuesvalue_indicesrO  s	            r=   rZ  zSparseArray._take_without_fillw  s    Q;IKKMQGKKMQB$6Av !QRR@AA<<>llnGH"]]//8
2%
z*'=>z299"((9O(W}499UDz%%m\%TTr<   c                    d}t        j                  |t        t                      t	        j
                  |      }t	        j
                  | | j                  j                        j                  |||      S )Nz(searchsorted requires high memory usage.r   rS   )	r   r   r   r   rU   rV   rT   rX   searchsorted)r   vsidesorterr   s        r=   rm  zSparseArray.searchsorted  sV     9c-:J:LMJJqMzz$djj&8&89FFq$PVWWr<   c                    | j                   j                         }| j                  || j                  | j                        S r   )r{   ra   r   rv   rT   )r   r   s     r=   ra   zSparseArray.copy  s1    $$&tzzBBr<   c                   |d   j                   }g }d}|r|d   j                  }nd}|dk(  rg }|D ]p  }|j                  j                  j	                         }||z  }||j                  j
                  z  }|j                  |j                         |j                  |       r t        j                  |      }	t        j                  |      }
t        ||
      }ng }g }|D ]  }|j                  j                         }|j                  |j                         |j                  |j                  j	                         |z          |j                  |j                         ||j                  j
                  z  } t        j                  |      }	t        j                  |      }t        j                  |      }t        |||      } | |	||      S )Nr   rA   r   rW   )rW   r   rv   r   ra   r   appendr{   rU   concatenater   to_block_indexblocsblengthsr   )r   	to_concatrW   r   r   sp_kindr   rZ   int_idxr   indices_arrrv   rx  rw  	block_idx	blocs_arrblengths_arrs                    r=   r  zSparseArray._concat_same_type  s   q\,,
l''GG iG ,,..3356!#,,---cmm,w' ! >>&)D..1K  4H HE LL779	cmm,Y__113f<=	 2 23#,,--- ! >>&)Du-I>>(3L!&)\BH4h:FFr<   c                   || j                   k(  r|s| S | j                         S t        |      }t        |t              s.t        j                  |       }t        |      }t        ||d      S | j                  j                  |      }t        |j                        }t        t
        j                  |      }t        | j                        }t        |||      }t        j                  |      }| j                  || 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)
        Fr   r`   )r   ra   r    r   r"   rU   rV   r,   r   rT   update_dtype_subtype_with_strr	   r{   r   rv   )r   rT   ra   future_dtyper   rX   r{   s          r=   ru   zSparseArray.astype  s    r DKKyy{"#E*,4ZZ%F3F;FlGG

''.u667rxx)/? t<	JJy)		4==%@@r<   c                D   t        t        j                  t        f      | j                  |t              rrj                        n       fd}| j                  D cg c]
  } ||       }} t        |       || j                        S c c}w )af  
        Map categories using an input mapping or function.

        Parameters
        ----------
        mapper : dict, Series, callable
            The correspondence from old values to new.
        na_action : {None, 'ignore'}, default None
            If 'ignore', propagate NA values, without passing them to the
            mapping correspondence.

        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                l    rj                  | d       n |       }|u s|k(  rd}t        |      |S )Nz-fill value in the sparse values not supported)getrY   )sp_val
new_sp_valr   fill_valis_mapmappers      r=   funczSparseArray.map.<locals>.funcS  s?    5;FD1JX%x)?E o%r<   rs  )
r   r   Mappingr$   rW   r'   r  r{   r   rv   )r   r  	na_actionr  xr{   r  r  s    `    @@r=   mapzSparseArray.map$  s    P FS[[)$<=??h9?vzz(H5VHEUH	 '+nn5nT!Wn	5tDz)$--HUU 6s   .Bc                X    t        j                  | | j                  j                        S )zr
        Convert SparseArray to a NumPy array.

        Returns
        -------
        arr : NumPy array
        rS   )rU   rV   r{   rT   r   s    r=   ry   zSparseArray.to_dense_  s     zz$dnn&:&:;;r<   c                    t        j                  || |      }t        |j                  | j                        }t        |       j                  ||      }|S )Nr  rS   )rU   r  r"   rT   rW   r   r   )r   r   r   naive_implementationrT   r   s         r=   _wherezSparseArray._wherei  sM      "xxdE:0664??Sd**+?u*Mr<   c                
   t        |t              rX|\  }\  }}t        j                  g       }|j	                  |       || _        || _        t        |j                  |      | _	        y| j                  j                  |       y)z*Necessary for making this object picklableN)r   r;  rU   r   __setstate__r   r   r"   rT   r   __dict__update)r   statend_staterW   rv   r   s         r=   r  zSparseArray.__setstate__t  sl    eU#/4,H,z8HHRLM&&x0"/D!)D%m&9&9:FDKMM  'r<   c                    | j                   dk(  r| j                  j                  fS | j                  j                  | j                  dk7     fS )Nr   )rW   rv   r   r{   r   s    r=   nonzerozSparseArray.nonzero  sB    ??aMM))++MM))$..A*=>@@r<   )skipnakeepdimsc                   t        | |d       }|t        d| d| j                         |r| }n| j                         } t        ||      di |}|r t	        |       |g| j                        S |S )Nzcannot perform z with type rS   r;   )r   r   rT   r.  r   )r   r   r  r  kwargsr  rZ   r   s           r=   _reducezSparseArray._reduce  s}     tT*>odV;tzzlKLLC++-C#d#-f-4:vhdjj99Mr<   c                    t        j                  ||       | j                  }t        |      t        |       k7  r t	        j
                  | j                        sy|j                         S )z
        Tests whether all elements evaluate True

        Returns
        -------
        all : bool

        See Also
        --------
        numpy.all
        F)nvvalidate_allr{   r   rU   rd   rW   r   axisargsr  r   s        r=   rd   zSparseArray.all  sI     	f%v;#d)#BFF4??,Czz|r<   c                    t        j                  ||       | j                  }t        |      t        |       k7  r t	        j
                  | j                        ry|j                         j                         S )z
        Tests whether at least one of elements evaluate True

        Returns
        -------
        any : bool

        See Also
        --------
        numpy.any
        T)r  validate_anyr{   r   rU   r~   rW   r   r  s        r=   r~   zSparseArray.any  sR     	f%v;#d)#t(?zz|  ""r<   c                R   t        j                  ||       | j                  }|j                         }| j                  j
                  dkD  xr | j                   }|r#|s!t        | j                  j                  d      S | j                  r:t        |j                  d|      r!t        | j                  j                  d      S |S | j                  j
                  }	t        |j                  d||	z
        r!t        | j                  j                  d      S || j                  |	z  z   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   FcompatN)r  validate_sumr   sumrv   rw   r   r&   rT   rX   r1   r   rW   )
r   r  	min_countr  r  r  
valid_valssp_sumhas_nansparses
             r=   r  zSparseArray.sum  s    4 	f%**
!$$q(F1F1F-F&%djj&8&8GG  $Z%5%5tYG)$***<*<UKKMmm))G$Z%5%5tY=PQ)$***<*<UKKDOOg555r<   c                H   t        j                  ||       ||| j                  k\  rt        d| d      | j                  s't        | j                               j                         S t        | j                  j                         | j                  | j                        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
        zaxis(=z) out of boundsrs  )r  validate_cumsumndimrY   r   rQ   ry   cumsumr{   rv   rW   )r   r  r  r  s       r=   r  zSparseArray.cumsum  s    $ 	4(		 1vdV?;<<$$t}}/6688NN!!#
 	
r<   c                    t        j                  ||       | j                  }|j                         }t	        |      }| j
                  r||z  S | j                  j                  }|| j                  |z  z   ||z   z  S )zb
        Mean of non-NA/null values

        Returns
        -------
        mean : float
        )	r  validate_meanr   r  r   r   rv   rw   rW   )r   r  r  r  r  r  ctr  s           r=   meanzSparseArray.mean  ss     	v&**
!_  B;mm))GT__w662<HHr<   )r  r  c               h    t        j                  || j                         | j                  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
        r^  r  r  validate_minmax_axisr  _min_maxr   r  r  s      r=   r^  zSparseArray.max)  +     	dii0}}U6}22r<   c               h    t        j                  || j                         | j                  d|      S )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  s      r=   r  zSparseArray.min;  r  r<   c                   | j                   }| j                   xr | j                  j                  dkD  }t	        |      dkD  rw t        ||             }|r$|dk(  rt        nt        } ||| j                        S |r|S | j                  j                  dk(  r|S t        | j                  j                  d      S |r| j                  S t        | j                  j                  d      S )z
        Min/max of non-NA/null values

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

        Returns
        -------
        scalar
        r   r^  Fr  )r   r   rv   rw   r   r   r^  r  rW   r&   rT   rX   )r   r   r  r  has_nonnull_fill_vals
sp_min_maxr  s          r=   r  zSparseArray._min_maxM  s     **
$($9$9 9 Udmm>Q>QTU>Uz?Q2T24J %"emsJ88!!$$)!!)$***<*<UKK"??"%djj&8&8GGr<   c                   | j                   }| j                  j                  }t        j                  t        |            }|dk(  rt        j                  nt        j                  }t        j                  |j                  d         }||    }||    }| ||         }	||	   }
t        | j                        r|
S |dk(  r| |
   | j                  k  r|
S |dk(  r| |
   | j                  kD  r|
S | j                         }|dk(  r|
S |S )Nr  r   argminr  )r   r   r   rU   rV   r%   r  r  rD  r   rW   r  )r   r   r   r   r   r  r   non_nansnon_nan_idx
_candidate	candidate_locs               r=   _argmin_argmaxzSparseArray._argmin_argmaxp  s    $$""**zz$v,' H,ryy"))iiQ(4%=4%j h0
*%	 8Y$// A8Y$// A))+2:Kr<   c                d    t        |d       |s| j                  rt        | j                  d      S )Nr  r  r   _hasnaNotImplementedErrorr  r   r  s     r=   r  zSparseArray.argmax  ,    FH-$++%%""8,,r<   c                d    t        |d       |s| j                  rt        | j                  d      S )Nr  r  r  r  s     r=   r  zSparseArray.argmin  r  r<   c                    |j                  dd      }||z   D ](  }t        | j                  t        fz         r"t        c S  t        j                   ||g|i |}|t        ur|S d|v rt        j                   ||g|i |}|S |dk(  r%t        j                   ||g|i |}|t        ur|S t        |      dk(  r t        ||       j                  fi |}	 t        ||       j                  fi |}
|j                  dkD  r t         fdt        |	|
      D              }|S |dk(  r|	S  j!                  |	 j"                  t%        |	j&                  |
            S t        d |D              } t        ||      |i |}|rt        |      dk(  r|d   }|S |j                  dkD  rt         fd|D              S |d	k(  ry  t)               |      S )
Nr   r;   reducerf   c           	   3     K   | ]8  \  }}j                  |j                  t        |j                  |             : y wr   )r   rv   r"   rT   ).0sp_valuefvr   s      r=   	<genexpr>z.SparseArray.__array_ufunc__.<locals>.<genexpr>  sD       )C" $$ $--X^^R1P )Cs   >Ac              3  F   K   | ]  }t        j                  |        y wr   )rU   rV   )r  r  s     r=   r  z.SparseArray.__array_ufunc__.<locals>.<genexpr>  s     9&Q2::a=&s   !r   c              3  @   K   | ]  } t              |        y wr   )r   )r  r  r   s     r=   r  z.SparseArray.__array_ufunc__.<locals>.<genexpr>  s     71dAs   at)r  r   _HANDLED_TYPESrQ   NotImplementedr(   !maybe_dispatch_ufunc_to_dunder_opdispatch_ufunc_with_outdispatch_reduction_ufuncr   r   r{   rW   noutr;  zipr   rv   r"   rT   r   )r   ufuncr  inputsr  r   r  r   resr{   rW   arrays
new_inputss   `            r=   __array_ufunc__zSparseArray.__array_ufunc__  s   jj##Aa!4!4~!EF%% 
 <<%
"(
,2
 'MF?33eV&,06C JX77eV&,06F ^+v;!.v.t~~HHI//J6JJzzA~  ),Iz(B	  8#  ##4==+iooz*R  9&99
''>v>3x1}!fJ::>7777t^4:f%%r<   c           	     ^   |j                   }t        |t              rt        | |||      S t	        |      rt        j                  d      5   |t        |       t        j                  |            } || j                  |      }d d d        |dk(  r:\  }}\  }}	t        ||| j                  |      t        ||| j                  |	      fS t        || j                        S t        j                  |      }t        j                  d      5  t        |       t        |      k7  r#t        dt        |        dt        |             t        |t              s%t        |dd       }
t        || j                  |
      }t        | |||      cd d d        S # 1 sw Y   xY w# 1 sw Y   y xY w)Nrb   rc   rs   zlength mismatch: z vs. rT   r   )r7   r   rQ   r   r   rU   rx   r[   rV   r{   r   rv   r   r   r   rW   )r   otherr   op_namer   r   r   r   lfillrfillrT   s              r=   _arith_methodzSparseArray._arith_method  so   ++e[)#D%W==u*)D/2::e+<=DNNE2 + ("$e#u $uE %F 
  EE JJu%E*t9E
*(+CI;eCJ<H  "%5#E7D9E'$//QVWE'eRA +*! +*  +*s   9FA=F#F #F,c                   t        |      s*t        |t        |             st        j                  |      }t        |t        j
                        rt        || j                        }t        |t              rct        |       t        |      k7  r#t        dt        |        dt        |             |j                  j                  d      }t        | |||      S  || j                  |      }t        j                  t        |       |t        j                        } || j                  |      || j                   j"                  <    t        |       ||t        j                        S )Nr  z operands have mismatched length z and rm   rS   r   )r   r   r   rU   rV   r   rQ   rW   r   rY   r7   stripr   r   r   r{   rv   r   )r   r  r   r  rW   r   s         r=   _cmp_methodzSparseArray._cmp_method  s   
5$t*(EJJu%EeRZZ($//BEe[)4yCJ& 6s4yks5zlS  kk'',G#D%W== DOOU3JWWSY
"((CF,.t~~u,EF4==(()4:%hh r<   c                    |t        j                  | j                              j                         }t	        | j
                  j                  |      }t        | j                        s|| j                  k(  rB || j                        }t        |       j                  || j                  | j
                        S  t        |        || j                               |      S r   )rU   r   rW   r   r"   rT   rX   r%   r{   r   r   rv   ry   )r   r   rW   rT   r   s        r=   _unary_methodzSparseArray._unary_method"  s    12779
DJJ..
;  J$//$A'F:))&$--LLtDz"T]]_-U;;r<   c                @    | j                  t        j                        S r   )r  operatorposr   s    r=   __pos__zSparseArray.__pos__-      !!(,,//r<   c                @    | j                  t        j                        S r   )r  r  negr   s    r=   __neg__zSparseArray.__neg__0  r  r<   c                @    | j                  t        j                        S r   )r  r  invertr   s    r=   
__invert__zSparseArray.__invert__3  s    !!(//22r<   c                @    | j                  t        j                        S r   )r  r  r  r   s    r=   __abs__zSparseArray.__abs__6  r  r<   c                    t        j                  |       }t        j                  | j                        }t        j                  | j                        }| d| d| S )Nz
Fill: 
)r2   pprint_thingrW   rv   )r   pp_strpp_fillpp_indexs       r=   __repr__zSparseArray.__repr__<  sQ    &&t,''8((7'"XJ77r<   c                     y r   r;   )r   boxeds     r=   
_formatterzSparseArray._formatterB  s     r<   )NNrA   NF)r   SparseIndexKindrT   Dtype | Nonera   rj   returnNone)r   r   r   r   rT   r"   r  rL   )r   r>   r  rL   )NN)rT   zNpDtype | Nonera   zbool | Noner  r   )r  r  )rT   r  ra   rj   )r  r   )r  r   )r  r"   )r  r  )r  int)r  rj   )r  float)r  rL   )
r  r?   r   
int | Noner   z#Literal['inside', 'outside'] | Nonera   rj   r  rL   )NNNT)r  zFillnaOptions | Noner   r  ra   rj   r  rL   )rf   N)r  r  r  rL   )first)r  zLiteral['first', 'last', False]r  znpt.NDArray[np.bool_])T)r(  rj   r  ztuple[np.ndarray, SparseArray])r.  rj   r  rO   )r   rK   r  r   )r   z,SequenceIndexer | tuple[int | ellipsis, ...]r  rL   )r   z.PositionalIndexer | tuple[int | ellipsis, ...]r  z
Self | Any)rV  rj   r  rL   r   )r   N)rn  zArrayLike | objectro  zLiteral['left', 'right']rp  zNumpySorter | Noner  znpt.NDArray[np.intp] | np.intp)ry  zSequence[Self]r  rL   )NT)rT   zAstypeArg | Nonera   rj   )r  ztuple[npt.NDArray[np.int32]])r   r   r  rj   r  rj   )r   )r  rF   r  rj   )r   r   T)r  rF   r  r  r  rj   r  rJ   )r  rF   r  rQ   )r  rE   )r  zAxisInt | Noner  rj   )r   zLiteral['min', 'max']r  rj   r  rJ   )r   zLiteral['argmin', 'argmax']r  r  )r  rj   r  r  )r  znp.ufuncr  r   )r  rQ   )r  r   )F)r  rj   )Wr7   r8   r9   __doc___subtypr+   _hidden_attrs	frozenset__annotations__r   classmethodr   r   r   r   r   r   propertyrv   r{   rT   rW   setterr   r   r   r   r   r   r   r   r%   r  r  r  r  r   r*   r  r   r&  r)  r4  r
   r7  r<  rF  rY  rZ  rm  ra   r  ru   r  ry   r  r  r  r  rd   r~   r  r  r  r^  r  r  r  r  r  rU   r   numbersNumberr  r  r  r  _logical_methodr  r  r  r  r  r  r  __classcell__)r  s   @r=   rQ   rQ     s   DL G ..2>M
  )"CC
 CC CC CC 
CCJ 
 
 "
 	

 

 
 )2 )2X AE)#)2=)	)V >BQV ) ) 1 1 " " # #   % %$ = =    
$ - -1 < < < < % %? !:>
 
 	

 8
 
 

  '+ ;K %;K 	;K
 ;K 
;Kz-60* 		"	"#6=>3>	> $>D1 !%!! 
(!&7V   9 
 \6;\6 
\6|	 38D 
$@DU4 *0%)		X	X '	X #		X
 
(	XC 5G 5GnMA^9Vv<(A ,0%$(;?(*#. 	*6*6 *6 	*6 
*6X
@I& -1 3$ -1 3$!HF4-- jj'..1N@&LBB: "O	<00308 r<   c                   t        | t        j                        sJ | j                  dkD  rt	        d      |t        | j                        }t        |      rt        |       }n[t        | j                        r| j                  t              } t        | j                        rt        j                  | |      }n| |k7  }t        |       }|t        |      k7  r|j                   j"                  }n0|j%                         d   j                  t        j&                        }t)        |||      }| |   }|-t+        |      }t-        ||      }t        j.                  |      }|||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)
    rf   zexpected dimension <= 1 datar   rS   )r   rU   r   r  r   r&   rT   r%   r'   r   ru   r   r   r   make_mask_object_ndarrayr   rv   r   r  rE  rC  r,   r   rV   )	rZ   r   rW   rT   r   r   r   r   sparsified_valuess	            r=   r   r   H  s4   * c2::&&&
xx!|677'		2
JSz 399%**V$C399% 11#zBD*$DXFT--'',,.#**2884fgt4ED	:;LM():%HJJ'89 eZ//r<   c                     y r   r;   r   r   r   s      r=   rC  rC        r<   c                     y r   r;   r'  s      r=   rC  rC    r(  r<   c                    |dk(  r't        j                  |      \  }}t        | ||      }|S |dk(  rt        | |      }|S t	        d      )NrB   rA   zmust be block or integer type)r   
get_blocksr   r   rY   )r   r   r   locslensr   s         r=   rC  rC    s]    w%%g.
d64.
 L	 
	) L 899r<   )rZ   rQ   r  r   )
r   rQ   r   rQ   r   r   r   r   r  rQ   r   )r   r   rT   r  r  rQ   )rB   NN)rZ   r   r   r  rT   znp.dtype | None)r   r  r   zLiteral['block']r  r   )r   r  r   zLiteral['integer']r  r   )r   r  r   r  r  r   )|r  
__future__r   collectionsr   r  r  typingr   r   r   r   r	   r
   r   numpyrU   pandas._libsr   pandas._libs.sparse_libssparser   r   r   r   pandas._libs.tslibsr   pandas.compat.numpyr   r  pandas.errorsr   pandas.util._decoratorsr   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    pandas.core.dtypes.dtypesr!   r"   pandas.core.dtypes.genericr#   r$   pandas.core.dtypes.missingr%   r&   r'   pandas.corer(   pandas.core.algorithmscore
algorithmsr  pandas.core.arrayliker)   pandas.core.arraysr*   pandas.core.baser+   pandas.core.commoncommonrH  pandas.core.constructionr,   r-   r.   pandas.core.indexersr/   r0   pandas.core.nanopsr1   pandas.io.formatsr2   collections.abcr3   enumr4   r6   r:   scipy.sparser>   pandas._typingr?   r@   r  rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r/  rO   r   _sparray_doc_kwargsr[   r   r   rQ   r   rC  r;   r<   r=   <module>rT     s   #        # # 
 $ . , ' 4
 3 
    " & & * - )     
 5 & (4    H%
 01O     H~H . *0iG
iG)iG/7iG?BiGiGZ FJ
6B.f(L. fV1 $!	;0	;0
;0 	;0| 
 
 
 
	r<   