a
    DBìjÐ*  ã                   @  s&  d Z ddlmZ ddlmZ ddlZddlmZ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mZ dd
lmZ ddlmZ ddlm  mZ ddlmZmZ ddl m!Z! ddl"m#Z# erÒddl$m%Z% e#g d¢ej& edd�e#g d¢eƒG dd„ de!ƒƒƒZ'dddœddddœdd„Z(dS )z implement the TimedeltaIndex é    )Úannotations)ÚTYPE_CHECKINGN)ÚindexÚlib)Ú
ResolutionÚ	TimedeltaÚ	to_offset)Údisallow_ambiguous_unit)Úfind_stack_level)Ú	is_scalarÚpandas_dtype)Ú	ABCSeries)ÚTimedeltaArray)ÚIndexÚmaybe_extract_name)ÚDatetimeTimedeltaMixin)Úinherit_names)ÚDtypeObj)Ú__neg__Ú__pos__Ú__abs__Útotal_secondsÚroundÚfloorÚceilT)Úwrap)Ú
componentsZto_pytimedeltaÚsumZstdZmedianc                   @  s¶   e Zd ZU dZdZeZeddœdd„ƒZde	d< e
jZed	dœd
d„ƒZdejejejdddfddœdd„Zdddœdd„Zdd„ Zddœdd„Zddœdd„Zeddœd d!„ƒZdS )"ÚTimedeltaIndexal  
    Immutable Index of timedelta64 data.

    Represented internally as int64, and scalars returned Timedelta objects.

    Parameters
    ----------
    data : array-like (1-dimensional), optional
        Optional timedelta-like data to construct index with.
    unit : {'D', 'h', 'm', 's', 'ms', 'us', 'ns'}, optional
        The unit of ``data``.

        .. deprecated:: 2.2.0
         Use ``pd.to_timedelta`` instead.

    freq : str or pandas offset object, optional
        One of pandas date offset strings or corresponding objects. The string
        ``'infer'`` can be passed in order to set the frequency of the index as
        the inferred frequency upon creation.
    dtype : numpy.dtype or str, default None
        Valid ``numpy`` dtypes are ``timedelta64[ns]``, ``timedelta64[us]``,
        ``timedelta64[ms]``, and ``timedelta64[s]``.
    copy : bool
        Make a copy of input array.
    name : object
        Name to be stored in the index.

    Attributes
    ----------
    days
    seconds
    microseconds
    nanoseconds
    components
    inferred_freq

    Methods
    -------
    to_pytimedelta
    to_series
    round
    floor
    ceil
    to_frame
    mean

    See Also
    --------
    Index : The base pandas Index type.
    Timedelta : Represents a duration between two dates or times.
    DatetimeIndex : Index of datetime64 data.
    PeriodIndex : Index of Period data.
    timedelta_range : Create a fixed-frequency TimedeltaIndex.

    Notes
    -----
    To learn more about the frequency strings, please see `this link
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__.

    Examples
    --------
    >>> pd.TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'])
    TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
                   dtype='timedelta64[ns]', freq=None)

    We can also let pandas infer the frequency when possible.

    >>> pd.TimedeltaIndex(np.arange(5) * 24 * 3600 * 1e9, freq='infer')
    TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
                   dtype='timedelta64[ns]', freq='D')
    Ztimedeltaindexztype[libindex.TimedeltaEngine])Úreturnc                 C  s   t jS ©N)ÚlibindexZTimedeltaEngine©Úself© r$   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/indexes/timedeltas.pyÚ_engine_typeƒ   s    zTimedeltaIndex._engine_typer   Ú_datazResolution | Nonec                 C  s   | j jS r    )r'   Ú_resolution_objr"   r$   r$   r%   r(   Ž   s    zTimedeltaIndex._resolution_objNFÚbool)Úcopyc           
      C  sX  |t jur&tjd| j› d�ttƒ d� |t jurNtjd| j› d�ttƒ d� nd }t||| ƒ}t|ƒrp|  	|¡ t
|ƒ |d urˆt|ƒ}t|tƒrÈ|t ju rÈ|d u s®||jkrÈ|rº| ¡ }| j||d�S t|tƒ�r|t ju �r|d u �r|d u sþ||jk�r|�r| ¡ S | ¡ S tj|||||d�}d }	|�sHt|ttfƒ�rH|j}	| j|||	d�S )	NzThe 'closed' keyword in zD construction is deprecated and will be removed in a future version.)Ú
stacklevelzThe 'unit' keyword in za construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead.©Úname)ÚfreqÚunitÚdtyper*   )r-   Úrefs)r   Ú
no_defaultÚwarningsÚwarnÚ__name__ÚFutureWarningr
   r   r   Z_raise_scalar_data_errorr	   r   Ú
isinstancer   r0   r*   Ú_simple_newr   Z_viewZ_from_sequence_not_strictr   r   Z_references)
ÚclsÚdatar/   r.   Úclosedr0   r*   r-   Útdarrr1   r$   r$   r%   Ú__new__•   sd    

ü
û
ÿþýýÿþýüü
ÿzTimedeltaIndex.__new__r   )r0   r   c                 C  s   t  |d¡S )zF
        Can we compare values of the given dtype to our own?
        Úm)r   Zis_np_dtype)r#   r0   r$   r$   r%   Ú_is_comparable_dtypeÞ   s    z#TimedeltaIndex._is_comparable_dtypec              
   C  sZ   |   |¡ z| jj|dd�}W n. tyL } zt|ƒ|‚W Y d}~n
d}~0 0 t | |¡S )z…
        Get integer location for requested label

        Returns
        -------
        loc : int, slice, or ndarray[int]
        F)ZunboxN)Z_check_indexing_errorr'   Z_validate_scalarÚ	TypeErrorÚKeyErrorr   Úget_loc)r#   ÚkeyÚerrr$   r$   r%   rB   ç   s    
 zTimedeltaIndex.get_locÚstr)Úlabelc                 C  s   t |ƒ}|d fS r    )r   )r#   rF   Úparsedr$   r$   r%   Ú_parse_with_resoø   s    zTimedeltaIndex._parse_with_resor   )rG   c                 C  s,   |  |j¡}|t|jƒ tddƒ }||fS )Né   Úns)r   Zresolution_stringr   r   )r#   ZresorG   ZlboundZrboundr$   r$   r%   Ú_parsed_string_to_boundsý   s    z'TimedeltaIndex._parsed_string_to_boundsc                 C  s   dS )NZtimedelta64r$   r"   r$   r$   r%   Úinferred_type  s    zTimedeltaIndex.inferred_type)r5   Ú
__module__Ú__qualname__Ú__doc__Z_typr   Z	_data_clsÚpropertyr&   Ú__annotations__r   Z_get_string_slicer(   r   r2   r=   r?   rB   rH   rK   rL   r$   r$   r$   r%   r   &   s.   
HøI	r   )r/   z
int | Nonez
str | None)Úperiodsr/   r   c                C  sF   |du rt  || |¡rd}t|ƒ}tj| |||||d�}tj||d�S )a$  
    Return a fixed frequency TimedeltaIndex with day as the default.

    Parameters
    ----------
    start : str or timedelta-like, default None
        Left bound for generating timedeltas.
    end : str or timedelta-like, default None
        Right bound for generating timedeltas.
    periods : int, default None
        Number of periods to generate.
    freq : str, Timedelta, datetime.timedelta, or DateOffset, default 'D'
        Frequency strings can have multiples, e.g. '5h'.
    name : str, default None
        Name of the resulting TimedeltaIndex.
    closed : str, default None
        Make the interval closed with respect to the given frequency to
        the 'left', 'right', or both sides (None).
    unit : str, default None
        Specify the desired resolution of the result.

        .. versionadded:: 2.0.0

    Returns
    -------
    TimedeltaIndex

    Notes
    -----
    Of the four parameters ``start``, ``end``, ``periods``, and ``freq``,
    exactly three must be specified. If ``freq`` is omitted, the resulting
    ``TimedeltaIndex`` will have ``periods`` linearly spaced elements between
    ``start`` and ``end`` (closed on both sides).

    To learn more about the frequency strings, please see `this link
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__.

    Examples
    --------
    >>> pd.timedelta_range(start='1 day', periods=4)
    TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'],
                   dtype='timedelta64[ns]', freq='D')

    The ``closed`` parameter specifies which endpoint is included.  The default
    behavior is to include both endpoints.

    >>> pd.timedelta_range(start='1 day', periods=4, closed='right')
    TimedeltaIndex(['2 days', '3 days', '4 days'],
                   dtype='timedelta64[ns]', freq='D')

    The ``freq`` parameter specifies the frequency of the TimedeltaIndex.
    Only fixed frequencies can be passed, non-fixed frequencies such as
    'M' (month end) will raise.

    >>> pd.timedelta_range(start='1 day', end='2 days', freq='6h')
    TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00',
                    '1 days 18:00:00', '2 days 00:00:00'],
                   dtype='timedelta64[ns]', freq='6h')

    Specify ``start``, ``end``, and ``periods``; the frequency is generated
    automatically (linearly spaced).

    >>> pd.timedelta_range(start='1 day', end='5 days', periods=4)
    TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00',
                    '5 days 00:00:00'],
                   dtype='timedelta64[ns]', freq=None)

    **Specify a unit**

    >>> pd.timedelta_range("1 Day", periods=3, freq="100000D", unit="s")
    TimedeltaIndex(['1 days', '100001 days', '200001 days'],
                   dtype='timedelta64[s]', freq='100000D')
    NÚD)r;   r/   r,   )ÚcomZany_noner   r   Z_generate_ranger   r8   )ÚstartÚendrR   r.   r-   r;   r/   r<   r$   r$   r%   Útimedelta_range
  s    SÿrW   )NNNNNN))rO   Ú
__future__r   Útypingr   r3   Zpandas._libsr   r!   r   Zpandas._libs.tslibsr   r   r   Zpandas._libs.tslibs.timedeltasr	   Zpandas.util._exceptionsr
   Zpandas.core.dtypes.commonr   r   Zpandas.core.dtypes.genericr   Zpandas.core.arrays.timedeltasr   Zpandas.core.commonÚcoreÚcommonrT   Zpandas.core.indexes.baser   r   Z pandas.core.indexes.datetimeliker   Zpandas.core.indexes.extensionr   Zpandas._typingr   Z
_field_opsr   rW   r$   r$   r$   r%   Ú<module>   sL   ÿüø
 V      úø