o
    ŀg4                     @  s  d dl mZ d dlmZ d dlZd dl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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 m!Z!m"Z#m$Z$ d dl%m&Z&m'Z'm(Z( d d	l)m*Z* d d
l+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZC d dlDmEZE d dlFmGZH d dlImJ  mKZL erd dlMmNZN d dlOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZW d dlFmXZXmYZY d dlZm[Z[ e
dedZ\ddiZ]dDdEdd Z^G d!d deHj_e#j`ZadFd$d%Zb		&dGdHd-d.ZcedId/d0ZdedJd3d0ZddKd5d0Zd	dDdLd7d8ZdMdNd<d=Ze								dOdPd?d@ZfdQdBdCZgdS )R    )annotations)	timedeltaN)TYPE_CHECKINGAnyCallableLiteralTypeVarcastoverload)algoslib)NDArrayBacked)
BaseOffsetNaTNaTType	Timedeltaadd_overflowsafeastype_overflowsafedt64arr_to_periodarrget_unit_from_dtypeiNaTparsingperiod	to_offset)	FreqGroupPeriodDtypeBasefreq_to_period_freqstr)isleapyear_arr)Tickdelta_to_tick)DIFFERENT_FREQIncompatibleFrequencyPeriodget_period_field_arrperiod_asfreq_arr)cache_readonlydoc)find_stack_level)ensure_objectpandas_dtype)DatetimeTZDtypePeriodDtype)ABCIndexABCPeriodIndex	ABCSeriesABCTimedeltaArray)isna)datetimelike)Sequence)AnyArrayLikeDtypeFillnaOptionsNpDtypeNumpySorterNumpyValueArrayLikeSelfnpt)DatetimeArrayTimedeltaArray)ExtensionArrayBaseOffsetT)boundklassPeriodArraynamestr	docstring
str | Nonec                   s     fdd} |_ ||_t|S )Nc                   s   | j j}t | j|}|S N)dtype_dtype_coder#   asi8)selfbaseresultrB    M/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/arrays/period.pyfm   s   z_field_accessor.<locals>.f)__name____doc__property)rB   rD   rP   rN   rM   rO   _field_accessorl   s   rT   c                      s  e Zd ZU dZdZdZeeZ	e
fZdd ZdZedd	d
Zg Zded< dgZded< g dZded< g dZded< ee e Zded< g dZded< ded< 	dddd Zedd$d%Zeddd&dd'd(Zeddd&dd)d*Zeddd+d,Zed-d. Zedd1d2Zdd6d7Zdd:d;Z dd>d?Z!e"dd@dAZ#eddCdDZ$eddEdFZ%	dddJdKZ&ddLdMZ'e(dNdOZ)e(dPdQZ*e(dRdSZ+e(dTdUZ,e(dVdWZ-e(dXdYZ.e(dZd[Z/e/Z0e(d\d]Z1e1Z2e2Z3e(d^d_ Z4Z5e(d`daZ6e(dbZ7e(dcddZ8e8Z9eddfdgZ:dddkdlZ;ddmdnZ<e=di e>dododpdddrdsZ?dddudvZ@dwddxdd|d}ZAdd fddZB		ddddZCddd~ddddZD	~ddÇ fddZEdddZFdddZG fddZHdddZIdd ZJ  ZKS )rA   a  
    Pandas ExtensionArray for storing Period data.

    Users should use :func:`~pandas.array` to create new instances.

    Parameters
    ----------
    values : Union[PeriodArray, Series[period], ndarray[int], PeriodIndex]
        The data to store. These should be arrays that can be directly
        converted to ordinals without inference or copy (PeriodArray,
        ndarray[int64]), or a box around such an array (Series[period],
        PeriodIndex).
    dtype : PeriodDtype, optional
        A PeriodDtype instance from which to extract a `freq`. If both
        `freq` and `dtype` are specified, then the frequencies must match.
    freq : str or DateOffset
        The `freq` to use for the array. Mostly applicable when `values`
        is an ndarray of integers, when `freq` is required. When `values`
        is a PeriodArray (or box around), it's checked that ``values.freq``
        matches `freq`.
    copy : bool, default False
        Whether to copy the ordinals before storing.

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

    Methods
    -------
    None

    See Also
    --------
    Period: Represents a period of time.
    PeriodIndex : Immutable Index for period data.
    period_range: Create a fixed-frequency PeriodArray.
    array: Construct a pandas array.

    Notes
    -----
    There are two components to a PeriodArray

    - ordinals : integer ndarray
    - freq : pd.tseries.offsets.Offset

    The values are physically stored as a 1-D ndarray of integers. These are
    called "ordinals" and represent some kind of offset from a base.

    The `freq` indicates the span covered by each element of the array.
    All elements in the PeriodArray have the same `freq`.

    Examples
    --------
    >>> pd.arrays.PeriodArray(pd.PeriodIndex(['2023-01-01',
    ...                                       '2023-01-02'], freq='D'))
    <PeriodArray>
    ['2023-01-01', '2023-01-02']
    Length: 2, dtype: period[D]
    i  periodarrayc                 C  s
   t | tS rF   )
isinstancer+   )xrN   rN   rO   <lambda>   s    zPeriodArray.<lambda>)r   returntype[Period]c                 C  s   t S rF   )r"   rJ   rN   rN   rO   _scalar_type      zPeriodArray._scalar_typez	list[str]
_other_opsis_leap_year	_bool_ops)
start_timeend_timefreq_object_ops)yearmonthdayhourminutesecond
weekofyearweekdayweek	dayofweekday_of_week	dayofyearday_of_yearquarterqyeardays_in_monthdaysinmonth
_field_ops_datetimelike_ops)strftimeto_timestampasfreq_datetimelike_methodsr+   _dtypeNFrG   Dtype | NonecopyboolNonec                 C  s  |d urt jdtt d t||}t|}|d ur+t|}t|ts+td| dt|t	r?|j
}t|t| s>tdnt|trG|j
}t|t| rd|d ur]||jkr]t||j|j|j}}|sntj|dd}ntj|d|d}|d u r~td	tt|}t| || d S )
Nz}The 'freq' keyword in the PeriodArray constructor is deprecated and will be removed in a future version. Pass 'dtype' instead)
stacklevelzInvalid dtype z for PeriodArrayzIncorrect dtypeint64rG   rG   r~   z-dtype is not specified and cannot be inferred)warningswarnFutureWarningr'   validate_dtype_freqr+   r)   rV   
ValueErrorr.   _valuestype	TypeErrorr-   rG   raise_on_incompatiblerc   _ndarraynpasarrayarrayr	   r   __init__)rJ   valuesrG   rc   r~   rN   rN   rO   r      s<   




zPeriodArray.__init__r   npt.NDArray[np.int64]r9   c                 C  s.   d}t |tjr|jdksJ || ||dS )Nz Should be numpy array of type i8i8r   )rV   r   ndarrayrG   )clsr   rG   assertion_msgrN   rN   rO   _simple_new  s   zPeriodArray._simple_newr   c                C  s   |d urt |}|rt|tr|j}nd }t|| r(t|j| |r&| }|S tj|t	d}|p5t
|}t
||}t|}| ||dS )Nr   )r)   rV   r+   rc   r   rG   r~   r   r   object	libperiodextract_freqextract_ordinals)r   scalarsrG   r~   rc   periodsordinalsrN   rN   rO   _from_sequence  s   
zPeriodArray._from_sequencec                C  s   | j |||dS )Nr   )r   )r   stringsrG   r~   rN   rN   rO   _from_sequence_of_strings4  s   z%PeriodArray._from_sequence_of_stringsc                 C  s<   t |trt|j|j}t|||\}}t|}| ||dS )a  
        Construct a PeriodArray from a datetime64 array

        Parameters
        ----------
        data : ndarray[datetime64[ns], datetime64[ns, tz]]
        freq : str or Tick
        tz : tzinfo, optional

        Returns
        -------
        PeriodArray[freq]
        r   )rV   r   r   nrB   r   r+   )r   datarc   tzrG   rN   rN   rO   _from_datetime64:  s
   
zPeriodArray._from_datetime64c                 C  sN   t |}|d urt|}|d us|d ur#t||||\}}||fS td)Nz/Not enough parameters to construct Period range)dtlvalidate_periodsr"   _maybe_convert_freq_get_ordinal_ranger   )r   startendr   rc   subarrrN   rN   rO   _generate_rangeO  s   

zPeriodArray._generate_rangefieldsdictc                C  s,   t dd|i|\}}t|}| j||dS )Nrc   r   rN   )_range_from_fieldsr+   r   )r   r   rc   r   rG   rN   rN   rO   _from_fields]  s   zPeriodArray._from_fieldsvaluePeriod | NaTTypenp.int64c                 C  sF   |t u r
t|jS t|| jr| | t|jS td| d)Nz!'value' should be a Period. Got 'z
' instead.)	r   r   r   _valuerV   r\   _check_compatible_withordinalr   rJ   r   rN   rN   rO   _unbox_scalari  s   
zPeriodArray._unbox_scalarrC   r"   c                 C  s   t || jdS )N)rc   )r"   rc   r   rN   rN   rO   _scalar_from_stringv  s   zPeriodArray._scalar_from_stringotherPeriod | NaTType | PeriodArrayc                 C  s   |t u rd S | |j d S rF   )r   _require_matching_freqrc   rJ   r   rN   rN   rO   r   |  s   z"PeriodArray._check_compatible_withc                 C  s   | j S rF   )r|   r[   rN   rN   rO   rG     s   zPeriodArray.dtyper   c                 C  s   | j jS )zC
        Return the frequency object for this PeriodArray.
        rG   rc   r[   rN   rN   rO   rc     s   zPeriodArray.freqc                 C  s   t | jj| jjS rF   )r   rc   r   rB   r[   rN   rN   rO   freqstr  s   zPeriodArray.freqstrNpDtype | Nonebool | None
np.ndarrayc                 C  s0   |dkr| j S |tkr| j S tjt| tdS )Nr   r   )rI   r   _isnanr   r   listr   )rJ   rG   r~   rN   rN   rO   	__array__  s
   zPeriodArray.__array__c                 C  s   ddl }ddlm} |dur@|j|r|j| j|  |dS t||r8| j	|j
kr7td| j	 d|j
 dntd| d	|| j	}|j| j|  d
d}|j||S )z6
        Convert myself into a pyarrow Array.
        r   N)ArrowPeriodType)maskr   zENot supported to convert PeriodArray to array with different 'freq' (z vs )z)Not supported to convert PeriodArray to 'z' typer   )pyarrow(pandas.core.arrays.arrow.extension_typesr   types
is_integerr   r   r0   rV   r   rc   r   r=   from_storage)rJ   r   r   r   period_typestorage_arrayrN   rN   rO   __arrow_array__  s*   


zPeriodArray.__arrow_array__re   z
        The year of the period.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y")
        >>> idx.year
        Index([2023, 2024, 2025], dtype='int64')
        rf   z
        The month as January=1, December=12.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
        >>> idx.month
        Index([1, 2, 3], dtype='int64')
        rg   z
        The days of the period.

        Examples
        --------
        >>> idx = pd.PeriodIndex(['2020-01-31', '2020-02-28'], freq='D')
        >>> idx.day
        Index([31, 28], dtype='int64')
        rh   z
        The hour of the period.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01-01 10:00", "2023-01-01 11:00"], freq='h')
        >>> idx.hour
        Index([10, 11], dtype='int64')
        ri   a  
        The minute of the period.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01-01 10:30:00",
        ...                       "2023-01-01 11:50:00"], freq='min')
        >>> idx.minute
        Index([30, 50], dtype='int64')
        rj   a	  
        The second of the period.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01-01 10:00:30",
        ...                       "2023-01-01 10:00:31"], freq='s')
        >>> idx.second
        Index([30, 31], dtype='int64')
        rm   a   
        The week ordinal of the year.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
        >>> idx.week  # It can be written `weekofyear`
        Index([5, 9, 13], dtype='int64')
        ro   z
        The day of the week with Monday=0, Sunday=6.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01-01", "2023-01-02", "2023-01-03"], freq="D")
        >>> idx.weekday
        Index([6, 0, 1], dtype='int64')
        rq   a  
        The ordinal day of the year.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01-10", "2023-02-01", "2023-03-01"], freq="D")
        >>> idx.dayofyear
        Index([10, 32, 60], dtype='int64')

        >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y")
        >>> idx
        PeriodIndex(['2023', '2024', '2025'], dtype='period[Y-DEC]')
        >>> idx.dayofyear
        Index([365, 366, 365], dtype='int64')
        rr   z
        The quarter of the date.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
        >>> idx.quarter
        Index([1, 1, 1], dtype='int64')
        rs   rt   a  
        The number of days in the month.

        Examples
        --------
        For Series:

        >>> period = pd.period_range('2020-1-1 00:00', '2020-3-1 00:00', freq='M')
        >>> s = pd.Series(period)
        >>> s
        0   2020-01
        1   2020-02
        2   2020-03
        dtype: period[M]
        >>> s.dt.days_in_month
        0    31
        1    29
        2    31
        dtype: int64

        For PeriodIndex:

        >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
        >>> idx.days_in_month   # It can be also entered as `daysinmonth`
        Index([31, 28, 31], dtype='int64')
        npt.NDArray[np.bool_]c                 C  s   t t| jS )z
        Logical indicating if the date belongs to a leap year.

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y")
        >>> idx.is_leap_year
        array([False,  True, False])
        )r   r   r   re   r[   rN   rN   rO   r_   b  s   zPeriodArray.is_leap_yearr   howr;   c                 C  s:  ddl m} t|}|dk}|r<|dks| jdkr,tddtdd }| jdd	| S tdd}| | j jdd	| S |d
u rP| j }t	|d}|j
}|}nt|}|j}| j||d	}	t|	j|}
||
}| jjdkrt| j}t|dkr|d }|| jjkr| j|_|S |dkr| jj|_|S |dS )a  
        Cast to DatetimeArray/Index.

        Parameters
        ----------
        freq : str or DateOffset, optional
            Target frequency. The default is 'D' for week or longer,
            's' otherwise.
        how : {'s', 'e', 'start', 'end'}
            Whether to use the start or end of the time period being converted.

        Returns
        -------
        DatetimeArray/Index

        Examples
        --------
        >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
        >>> idx.to_timestamp()
        DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'],
        dtype='datetime64[ns]', freq='MS')
        r   )r;   EB   Dnsr   )r   Ninfer)pandas.core.arraysr;   r   validate_end_aliasrc   r   ry   r|   _get_to_timestamp_baser   _freqstrr"   r   _period_dtype_coderz   periodarr_to_dt64arrrI   r   rB   libalgosunique_deltaslenrG   _n_freqrK   
_with_freq)rJ   rc   r   r;   r   adjust	freq_coderG   rK   new_parrnew_datadtadiffsdiffrN   rN   rO   ry   o  s<   







zPeriodArray.to_timestampc                 C  s   t j|| jdS )N)r   rc   )r"   _from_ordinalrc   )rJ   rW   rN   rN   rO   	_box_func  s   zPeriodArray._box_funcPeriodIndex)r   
other_namer   c           
      C  s   t |}t|trt|drt|j}t|}| j	j
}|j}| j}|dk}|r2|| jj d }n|}t||||}| jrCt|| j< t|}	t| ||	dS )a  
        Convert the {klass} to the specified frequency `freq`.

        Equivalent to applying :meth:`pandas.Period.asfreq` with the given arguments
        to each :class:`~pandas.Period` in this {klass}.

        Parameters
        ----------
        freq : str
            A frequency.
        how : str {{'E', 'S'}}, default 'E'
            Whether the elements should be aligned to the end
            or start within pa period.

            * 'E', 'END', or 'FINISH' for end,
            * 'S', 'START', or 'BEGIN' for start.

            January 31st ('END') vs. January 1st ('START') for example.

        Returns
        -------
        {klass}
            The transformed {klass} with the new frequency.

        See Also
        --------
        {other}.asfreq: Convert each Period in a {other_name} to the given frequency.
        Period.asfreq : Convert a :class:`~pandas.Period` object to the given frequency.

        Examples
        --------
        >>> pidx = pd.period_range('2010-01-01', '2015-01-01', freq='Y')
        >>> pidx
        PeriodIndex(['2010', '2011', '2012', '2013', '2014', '2015'],
        dtype='period[Y-DEC]')

        >>> pidx.asfreq('M')
        PeriodIndex(['2010-12', '2011-12', '2012-12', '2013-12', '2014-12',
        '2015-12'], dtype='period[M]')

        >>> pidx.asfreq('M', how='S')
        PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01',
        '2015-01'], dtype='period[M]')
        r   r   r   r   )r   r   rV   r   hasattrr+   r   r"   r   r|   rH   r   rI   rG   r   r$   _hasnar   r   r   )
rJ   rc   r   base1base2rI   r   r   r   rG   rN   rN   rO   rz     s    
.


zPeriodArray.asfreqboxedc                 C  s   |rt S djS )Nz'{}')rC   format)rJ   r   rN   rN   rO   
_formatter  s   zPeriodArray._formatterr   )na_repdate_formatr   str | floatnpt.NDArray[np.object_]c                K  s   t | j| jj||S )z3
        actually format my specific types
        )r   period_array_strftimerI   rG   rH   )rJ   r   r   kwargsrN   rN   rO   _format_native_types  s   z PeriodArray._format_native_typesTc                   s   t |}|| jkr|s| S |  S t|tr| |jS t|ds't|t	r<t
|dd }t|}|  ||S t j||dS )NMr   r~   )r)   r|   r~   rV   r+   rz   rc   r   is_np_dtyper*   getattrr   dtype_to_unitry   tz_localizeas_unitsuperastype)rJ   rG   r~   r   unit	__class__rN   rO   r	    s   


zPeriodArray.astypeleft$NumpyValueArrayLike | ExtensionArraysideLiteral['left', 'right']sorterNumpySorter | Nonenpt.NDArray[np.intp] | np.intpc                 C  s,   |  |d}| jd}|j|||dS )NM8[ns])r  r  )_validate_setitem_valueviewr   searchsorted)rJ   r   r  r  npvaluem8arrrN   rN   rO   r  $  s   zPeriodArray.searchsorted)limit
limit_arear~   methodr5   r  
int | Noner  #Literal['inside', 'outside'] | Nonec                C  s6   |  d}|j||||d}|rtd| | jS | S )Nr  )r  r  r  r~   r9   )r  _pad_or_backfillr	   rG   )rJ   r  r  r  r~   r   rL   rN   rN   rO   r  1  s   

zPeriodArray._pad_or_backfillc                   sD   |d ur|  d}|j||||d}| | jS t j||||dS )Nr  )r   r  r  r~   )r  fillnarG   r  )rJ   r   r  r  r~   r   rL   r  rN   rO   r   D  s
   
zPeriodArray.fillnanp.ndarray | intopCallable[[Any, Any], Any]c                 C  sL   |t jt jfv s
J |t ju r| }t| jtj|dd}t| || jdS )z
        Add or subtract array of integers.

        Parameters
        ----------
        other : np.ndarray[int64] or int
        op : {operator.add, operator.sub}

        Returns
        -------
        result : PeriodArray
        r   r   )	operatoraddsubr   rI   r   r   r   rG   )rJ   r   r"  
res_valuesrN   rN   rO   _addsub_int_array_or_scalarT  s
   
z'PeriodArray._addsub_int_array_or_scalarc                 C  s,   t |trJ | j|dd | |jtjS )NT)rK   )rV   r   r   r(  r   r$  r%  r   rN   rN   rO   _add_offseti  s   zPeriodArray._add_offsetc                   sD   t | jtst| |t|rt |S tt	|j
}| |S )z
        Parameters
        ----------
        other : timedelta, Tick, np.timedelta64

        Returns
        -------
        PeriodArray
        )rV   rc   r   r   r0   r  _add_timedeltalike_scalarr   r   r   asm8_add_timedelta_arraylike)rJ   r   tdr  rN   rO   r*  p  s   


z%PeriodArray._add_timedeltalike_scalar,TimedeltaArray | npt.NDArray[np.timedelta64]c              
   C  s   | j  std| j  t d| j j d}ztt||ddd}W n ty6 } ztd|d}~ww t	| j
t|d}t| || j d	S )
z
        Parameters
        ----------
        other : TimedeltaArray or ndarray[timedelta64]

        Returns
        -------
        PeriodArray
        z2Cannot add or subtract timedelta64[ns] dtype from m8[]FrG   r~   round_okznCannot add/subtract timedelta-like from PeriodArray that is not an integer multiple of the PeriodArray's freq.Nr   r   )rG   _is_tick_liker   r   
_td64_unitr   r   r   r!   r   rI   r  r   )rJ   r   rG   deltaerrr'  rN   rN   rO   r,    s&   


z$PeriodArray._add_timedelta_arraylikec              
   C  s   | j  sJ t d| j j d}t|ttjtfr$tt	|j
}nt|}z
t||ddd}W n tyE } zt| ||d}~ww |d}t|S )a<  
        Arithmetic operations with timedelta-like scalars or array `other`
        are only valid if `other` is an integer multiple of `self.freq`.
        If the operation is valid, find that integer multiple.  Otherwise,
        raise because the operation is invalid.

        Parameters
        ----------
        other : timedelta, np.timedelta64, Tick,
                ndarray[timedelta64], TimedeltaArray, TimedeltaIndex

        Returns
        -------
        multiple : int or ndarray[int64]

        Raises
        ------
        IncompatibleFrequency
        r/  r0  Fr1  Nr   )rG   r3  r   r4  rV   r   timedelta64r   r   r   r+  r   r   r   r  r   item_from_zerodim)rJ   r   rG   r-  r5  r6  rN   rN   rO    _check_timedeltalike_freq_compat  s   


z,PeriodArray._check_timedeltalike_freq_compat)rY   rZ   )NNF)rG   r}   r~   r   rY   r   )r   r   rG   r+   rY   r9   )rG   r}   r~   r   rY   r9   rF   )rY   r9   )r   r   rY   r9   )r   r   rY   r   )r   rC   rY   r"   )r   r   rY   r   )rY   r+   )rY   r   )rY   rC   )NN)rG   r   r~   r   rY   r   )rY   r   )Nr   )r   rC   rY   r;   )rY   r   rN   )Nr   )r   rC   rY   r9   )F)r   r   )r   r   rY   r   )T)r~   r   )r  N)r   r  r  r  r  r  rY   r  )
r  r5   r  r  r  r  r~   r   rY   r9   )NNNT)r  r  r~   r   rY   r9   )r   r!  r"  r#  rY   r9   )r   r   )r   r.  rY   r9   )LrQ   
__module____qualname__rR   __array_priority___typr   r   r   _internal_fill_valuer"   _recognized_scalars_is_recognized_dtype_infer_matchesrS   r\   r^   __annotations__r`   rd   rv   rw   r{   r   classmethodr   r   r   r   r   r   r   r   r   r%   rG   rc   r   r   r   rT   re   rf   rg   rh   ri   rj   rk   rm   ro   rn   rl   rp   rq   rr   rs   rt   ru   r_   ry   r   r&   _shared_doc_kwargsrz   r   r   r	  r  r  r   r(  r)  r*  r,  r9  __classcell__rN   rN   r  rO   rA   y   s   
 =
*







DH


%rY   r!   c                 C  s   t |tjtfs|du rd}n t |trt|j|j}nt |tt	t
fr(|j}ntt|j}t| jj| jj}tjt| j||d}t|S )a>  
    Helper function to render a consistent error message when raising
    IncompatibleFrequency.

    Parameters
    ----------
    left : PeriodArray
    right : None, DateOffset, Period, ndarray, or timedelta-like

    Returns
    -------
    IncompatibleFrequency
        Exception to be raised by the caller.
    N)r   own_freq
other_freq)rV   r   r   r/   r   r   r   rB   r-   rA   r"   r   r   r   rc   r    r   r   rQ   r!   )r  rightrG  rF  msgrN   rN   rO   r     s   
r   Fr   ,Sequence[Period | str | None] | AnyArrayLikerc   str | Tick | BaseOffset | Noner~   r   c           	      C  s  t | dd}t|drt| |S t|tr-t| }|dur+||jkr&|S ||S |S t| t	j
tttfs;t| } t	| }|rGt|}nd}|jjdkrYt|dkrYtd|jjdv rs|jt	jdd	}t||}t||d
S t|} |du rt| }t|}tj| |d
S )a  
    Construct a new PeriodArray from a sequence of Period scalars.

    Parameters
    ----------
    data : Sequence of Period objects
        A sequence of Period objects. These are required to all have
        the same ``freq.`` Missing values can be indicated by ``None``
        or ``pandas.NaT``.
    freq : str, Tick, or Offset
        The frequency of every element of the array. This can be specified
        to avoid inferring the `freq` from `data`.
    copy : bool, default False
        Whether to ensure a copy of the data is made.

    Returns
    -------
    PeriodArray

    See Also
    --------
    PeriodArray
    pandas.PeriodIndex

    Examples
    --------
    >>> period_array([pd.Period('2017', freq='Y'),
    ...               pd.Period('2018', freq='Y')])
    <PeriodArray>
    ['2017', '2018']
    Length: 2, dtype: period[Y-DEC]

    >>> period_array([pd.Period('2017', freq='Y'),
    ...               pd.Period('2018', freq='Y'),
    ...               pd.NaT])
    <PeriodArray>
    ['2017', '2018', 'NaT']
    Length: 3, dtype: period[Y-DEC]

    Integers that look like years are handled

    >>> period_array([2000, 2001, 2002], freq='D')
    <PeriodArray>
    ['2000-01-01', '2001-01-01', '2002-01-01']
    Length: 3, dtype: period[D]

    Datetime-like strings may also be passed

    >>> period_array(['2000-Q1', '2000-Q2', '2000-Q3', '2000-Q4'], freq='Q')
    <PeriodArray>
    ['2000Q1', '2000Q2', '2000Q3', '2000Q4']
    Length: 4, dtype: period[Q-DEC]
    rG   Nr  rP   r   z9PeriodIndex does not allow floating point in constructioniuFr  r   )r  r   r  rA   r   rV   r+   rc   rz   r   r   r   tupler.   r   rG   kindr   r   r	  r   r   from_ordinalsr(   r   r   )	r   rc   r~   
data_dtypeoutarrdatarG   arrr   rN   rN   rO   period_array  s6   :





rT  c                 C     d S rF   rN   r   rN   rN   rO   r   W  r]   r   timedelta | str | Noner   c                 C  rU  rF   rN   r   rN   rN   rO   r   \  r]   1BaseOffsetT | BaseOffset | timedelta | str | Nonec                 C  s^   |dur
t |dd}| dur-t| } t| tstd|du r$| j}|S || jkr-td|S )at  
    If both a dtype and a freq are available, ensure they match.  If only
    dtype is available, extract the implied freq.

    Parameters
    ----------
    dtype : dtype
    freq : DateOffset or None

    Returns
    -------
    freq : DateOffset

    Raises
    ------
    ValueError : non-period dtype
    IncompatibleFrequency : mismatch between dtype and freq
    NT	is_periodzdtype must be PeriodDtypez&specified freq and dtype are different)r   r)   rV   r+   r   rc   r!   r   rN   rN   rO   r   a  s   

(tuple[npt.NDArray[np.int64], BaseOffset]c                 C  s   t | jtjr| jjdkrtd| j |du r4t | tr&| j| j} }nt | tr3| j| j	j} }n
t | ttfr>| j} t
| j}t|}|j}t| d|||d|fS )a  
    Convert an datetime-like array to values Period ordinals.

    Parameters
    ----------
    data : Union[Series[datetime64[ns]], DatetimeIndex, ndarray[datetime64ns]]
    freq : Optional[Union[str, Tick]]
        Must match the `freq` on the `data` if `data` is a DatetimeIndex
        or Series.
    tz : Optional[tzinfo]

    Returns
    -------
    ordinals : ndarray[int64]
    freq : Tick
        The frequency extracted from the Series or DatetimeIndex if that's
        used.

    r  zWrong dtype: Nr   )reso)rV   rG   r   rN  r   r,   r   rc   r.   dtr   r"   r   r   c_dt64arr_to_periodarrr  )r   rc   r   r[  rK   rN   rN   rO   r     s   



r   r   multintc                 C  sZ  t | ||dkrtd|d urt|dd}|j}| d ur#t| |} |d ur,t||}t| t}t|t}|rD|rD| j|jkrDtd| tu sL|tu rPtd|d u rg|rZ| j}n
|r`|j}ntd|j}|d ur|| }| d u rt	j
|j| | |jd |t	jd	}||fS t	j
| j| j| |t	jd	}||fS t	j
| j|jd |t	jd	}||fS )
N   zOOf the three parameters: start, end, and periods, exactly two must be specifiedTrX  z!start and end must have same freqzstart and end must not be NaTz#Could not infer freq from start/endr   r   )comcount_not_noner   r   r   r"   rV   rc   r   r   aranger   r   )r   r   r   rc   r^  is_start_per
is_end_perr   rN   rN   rO   r     sJ   




r   tuple[np.ndarray, BaseOffset]c                 C  sZ  |d u rd}|d u rd}|d u rd}|d u rd}g }|d urr|d u r-t ddd}tjj}	nt |dd}t|}	|	tjjkrBtd|j}
t| |\} }t	| |D ]\}}t
|||
\}}t||dddddd|		}|| qQn1t |dd}t|}	t| |||||}t	| D ]\}}}}}}|t||||||dd|		 qtj|tjd|fS )Nr   r   QTrX  zbase must equal FR_QTRr   )r   r   FR_QTRr   r   freq_to_dtype_codeAssertionErrorr   _make_field_arrayszipr   quarter_to_myearperiod_ordinalappendr   r   r   )re   rf   rr   rg   rh   ri   rj   rc   r   rK   r   yqcalendar_yearcalendar_monthvalarraysmthdhmnsrN   rN   rO   r     s@   



"r   list[np.ndarray]c                    s^   d  | D ]!}t |ttjtfr% d urt| krtd d u r%t| q fdd| D S )NzMismatched Period array lengthsc                   s4   g | ]}t |tjttfrt|nt| qS rN   )rV   r   r   r   r.   r   repeat).0rW   lengthrN   rO   
<listcomp>  s    
z&_make_field_arrays.<locals>.<listcomp>)rV   r   r   r   r.   r   r   )r   rW   rN   r~  rO   rk    s   
rk  rF   )rB   rC   rD   rE   )rY   r!   )NF)r   rJ  rc   rK  r~   r   rY   rA   )rc   r>   rY   r>   )rc   rV  rY   r   )rc   rW  rY   r>   )rY   rZ  )r   )r^  r_  )NNNNNNNN)rY   rf  )rY   r{  )h
__future__r   datetimer   r$  typingr   r   r   r   r   r	   r
   r   numpyr   pandas._libsr   r   r   pandas._libs.arraysr   pandas._libs.tslibsr   r   r   r   r   r   r   r]  r   r   r   r   r   r   pandas._libs.tslibs.dtypesr   r   r   pandas._libs.tslibs.fieldsr   pandas._libs.tslibs.offsetsr   r   pandas._libs.tslibs.periodr    r!   r"   r#   r$   pandas.util._decoratorsr%   r&   pandas.util._exceptionsr'   pandas.core.dtypes.commonr(   r)   pandas.core.dtypes.dtypesr*   r+   pandas.core.dtypes.genericr,   r-   r.   r/   pandas.core.dtypes.missingr0   r   r1   r   pandas.core.commoncorecommonra  collections.abcr2   pandas._typingr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   pandas.core.arrays.baser=   r>   rD  rT   DatelikeOpsPeriodMixinrA   r   rT  r   r   r   rk  rN   rN   rN   rO   <module>   sv    $	8(      
]&c
&(21