
    i3              	         d Z ddlmZ ddlmZm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Z ddlmc 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& ddl
m'Z'm(Z( ddl)m*Z*m+Z+  e%dddddddgejX                  ed       e%g de       ed       G d de#                           Z- ed      	 	 	 	 	 	 d"dd 	 	 	 	 	 d#d!       Z.y)$zimplement the TimedeltaIndex    )annotations)TYPE_CHECKINGcast)indexlib)
Resolution	Timedelta	to_offset)abbrev_to_npy_unit)
set_module)	is_scalarpandas_dtype)
ArrowDtype)	ABCSeries)TimedeltaArrayN)Indexmaybe_extract_name)DatetimeTimedeltaMixin)inherit_names)NaTType)DayTick)DtypeObjTimeUnit__neg____pos____abs__total_secondsroundfloorceilT)wrap)
componentsto_pytimedeltasumstdmedianpandasc                      e Zd ZU dZdZeZedd       Zde	d<   e
j                  Zedd       Zdej                  dddf	 ddZdd	Zd
 ZddZddZedd       Zy)TimedeltaIndexa 	  
    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.
    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, default None
        Whether to copy input data, only relevant for array, Series, and Index
        inputs (for other input, e.g. a list, a new array is created anyway).
        Defaults to True for array input and False for Index/Series.
        Set to False to avoid copying array input at your own risk (if you
        know the input data won't be modified elsewhere).
        Set to True to force copying Series/Index input up front.
    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
    :ref:`this link<timeseries.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[us]', 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')
    timedeltaindexc                "    t         j                  S N)libindexTimedeltaEngineselfs    [/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/core/indexes/timedeltas.py_engine_typezTimedeltaIndex._engine_type   s    '''    r   _datac                .    | j                   j                  S r-   )r5   _resolution_objr0   s    r2   r7   zTimedeltaIndex._resolution_obj   s    zz)))r4   Nc                   t        |||       }| j                  |||      \  }}t        |      r| j                  |       |t	        |      }t        |t              rH|t        j                  u r6|||j                  k(  r%|r|j                         }| j                  ||      S t        |t              rG|t        j                  u r5|3|||j                  k(  r"|r|j                         S |j                         S t        j                  ||d ||      }d }|s"t        |t        t         f      r|j"                  }| j                  |||      S )Nname)frequnitdtypecopy)r:   refs)r   _maybe_copy_array_inputr   _raise_scalar_data_errorr   
isinstancer   r   
no_defaultr=   r>   _simple_newr*   _view_from_sequence_not_strictr   r   _references)clsdatar;   r=   r>   r:   tdarrr?   s           r2   __new__zTimedeltaIndex.__new__   s7    "$c2 00tUC
dT?((. 'E t^,&%4::"5yy{??4d?33 t^,&%4::"5yy{"zz|# 88t$e$
 
4)U);<##Du4d;;r4   c                l    t        |t              r|j                  dk(  S t        j                  |d      S )zF
        Can we compare values of the given dtype to our own?
        m)rB   r   kindr   is_np_dtype)r1   r=   s     r2   _is_comparable_dtypez#TimedeltaIndex._is_comparable_dtype   s.     eZ(::$$uc**r4   c                    | j                  |       	 | j                  j                  |d      }t        j                  | |      S # t        $ r}t	        |      |d}~ww xY w)z
        Get integer location for requested label

        Returns
        -------
        loc : int, slice, or ndarray[int]
        F)unboxN)_check_indexing_errorr5   _validate_scalar	TypeErrorKeyErrorr   get_loc)r1   keyerrs      r2   rW   zTimedeltaIndex.get_loc   sa     	""3'	)**--c-?C }}T3''  	)3-S(	)s   A 	A AA c                    t        |      }t        |t               r#t        j                  |j                        }||fS t        j                  d      }||fS )Ns)r	   rB   r   get_reso_from_freqstrr<   )r1   labelparsedresos       r2   _parse_with_resozTimedeltaIndex._parse_with_reso   sR    5!fi(33FKK@D t| 33C8Dt|r4   c                    |j                  |j                        }|t        |j                        z   t        d| j                        j                  | j                        z
  }||fS )N   r<   )r   resolution_stringr
   r	   r<   as_unit)r1   r_   r^   lboundrbounds        r2   _parsed_string_to_boundsz'TimedeltaIndex._parsed_string_to_bounds  s_    f6670012		*22499=> 	
 v~r4   c                     y)Ntimedelta64 r0   s    r2   inferred_typezTimedeltaIndex.inferred_type  s    r4   )returnztype[libindex.TimedeltaEngine])rm   zResolution | None)r>   zbool | None)r=   r   rm   bool)r]   strrm   z&tuple[Timedelta | NaTType, Resolution])r_   r   r^   r	   )rm   ro   )__name__
__module____qualname____doc___typr   	_data_clspropertyr3   __annotations__r   _get_string_slicer7   r   rC   rK   rP   rW   r`   rh   rl   rk   r4   r2   r*   r*   2   s    4EN DI( (  // * * ^^ 0<
 0<h+(*  r4   r*   rc   c                  |t        j                  || |      rd}t        |      }t        j                  | |||      dk7  rt	        d      |@| |t        |       } t        |      }t        t
        |       } t        t
        |      }t        | j                        t        |j                        kD  rt        d| j                        }n|t        d|j                        }ne| 2t        |       } t        t
        |       } t        d| j                        }n1t        |      }t        t
        |      }t        d|j                        }|Ft        d|      }t        |      }|j                  |kD  r t        d|j                  j                        }t        j                  | |||||      }t        j                  ||      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 : Hashable, 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 : {'s', 'ms', 'us', 'ns', None}, default None
        Specify the desired resolution of the result.
        If not specified, this is inferred from the 'start', 'end', and 'freq'
        using the same inference as :class:`Timedelta` taking the highest
        resolution of the three that are provided.

        .. versionadded:: 2.0.0

    Returns
    -------
    TimedeltaIndex
        Fixed frequency, with day as the default.

    See Also
    --------
    date_range : Return a fixed frequency DatetimeIndex.
    period_range : Return a fixed frequency PeriodIndex.

    Notes
    -----
    Of the four parameters ``start``, ``end``, ``periods``, and ``freq``,
    a maximum of three can be specified at once. Of the three parameters
    ``start``, ``end``, and ``periods``, at least two must be specified.
    If ``freq`` is omitted, the resulting ``DatetimeIndex`` will have
    ``periods`` linearly spaced elements between ``start`` and ``end``
    (closed on both sides).

    To learn more about the frequency strings, please see
    :ref:`this link<timeseries.offset_aliases>`.

    Examples
    --------
    >>> pd.timedelta_range(start="1 day", periods=4)
    TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'],
                   dtype='timedelta64[us]', 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[us]', 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[us]', 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[us]', 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')
    D   zVOf the four parameters: start, end, periods, and freq, exactly three must be specifiedr   z
Tick | Day)closedr<   r9   )comany_noner
   count_not_none
ValueErrorr	   r   r   r<   _cresobasefreqstrr   _generate_ranger*   rD   )	startendperiodsr;   r:   r|   r<   cresorJ   s	            r2   timedelta_ranger     s   ~ |WeS9T?D
%gt498
 	

 | e$EC.CE*Ey#&C!%**-0B3880LLJ

3J1e$EE*E
EJJ/DC.Cy#&C
CHH-D d+D&t,E{{U"J		(9(9:**sGT&tE %%e$%77r4   )NNNNNN)r   z
int | Noner<   zTimeUnit | Nonerm   r*   )/rs   
__future__r   typingr   r   pandas._libsr   r.   r   pandas._libs.tslibsr   r	   r
   pandas._libs.tslibs.dtypesr   pandas.util._decoratorsr   pandas.core.dtypes.commonr   r   pandas.core.dtypes.dtypesr   pandas.core.dtypes.genericr   pandas.core.arrays.timedeltasr   pandas.core.commoncorecommonr}   pandas.core.indexes.baser   r    pandas.core.indexes.datetimeliker   pandas.core.indexes.extensionr   r   r   r   pandas._typingr   r   
_field_opsr*   r   rk   r4   r2   <module>r      s3   " "
 
 : . 1 0 8     D 7$ 	 
	"	"	 	  	 HI+ I 	2IX H
		I8 !I8 I8 I8 I8 I8r4   