U
    MZf                     @  sF  d Z ddlmZ ddlmZ ddlmZmZ ddl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 dd
lmZmZ ddlmZ erddlmZ ddlmZmZ ddlmZmZm Z  ed'dddddddZ!ed(dddddddZ!ed)dddddddZ!d*dddddddZ!d+ddd!d"d#Z"d,dd$d%d&Z#dS )-z
timedelta support tools
    )annotations)	timedelta)TYPE_CHECKINGoverloadN)lib)NaTNaTType)	Timedeltaparse_timedelta_unit)is_list_like)ABCIndex	ABCSeries)sequence_to_td64ns)UnitChoices)	ArrayLikeDateTimeErrorChoices)IndexSeriesTimedeltaIndex.zstr | float | timedeltazUnitChoices | Noner   r	   )arguniterrorsreturnc                 C  s   d S N r   r   r   r   r   @/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/tools/timedeltas.pyto_timedelta.   s    r   r   c                 C  s   d S r   r   r   r   r   r   r   7   s    z(list | tuple | range | ArrayLike | Indexr   c                 C  s   d S r   r   r   r   r   r   r   @   s    raisezQstr | int | float | timedelta | list | tuple | range | ArrayLike | Index | Seriesz#Timedelta | TimedeltaIndex | Seriesc                 C  s  |dk	rt |}|dkr td|dkr0td| dkr<| S t| trjt| j||d}| j|| j| jdS t| t	rt| ||| jdS t| t
jr| jd	krt| } n>t| rt| d
ddkrt| ||dS t| d
ddkrtdt| tr|dk	rtdt| ||dS )a  
    Convert argument to timedelta.

    Timedeltas are absolute differences in times, expressed in difference
    units (e.g. days, hours, minutes, seconds). This method converts
    an argument from a recognized timedelta format / value into
    a Timedelta type.

    Parameters
    ----------
    arg : str, timedelta, list-like or Series
        The data to be converted to timedelta.

        .. versionchanged:: 2.0
            Strings with units 'M', 'Y' and 'y' do not represent
            unambiguous timedelta values and will raise an exception.

    unit : str, optional
        Denotes the unit of the arg for numeric `arg`. Defaults to ``"ns"``.

        Possible values:

        * 'W'
        * 'D' / 'days' / 'day'
        * 'hours' / 'hour' / 'hr' / 'h'
        * 'm' / 'minute' / 'min' / 'minutes' / 'T'
        * 'S' / 'seconds' / 'sec' / 'second'
        * 'ms' / 'milliseconds' / 'millisecond' / 'milli' / 'millis' / 'L'
        * 'us' / 'microseconds' / 'microsecond' / 'micro' / 'micros' / 'U'
        * 'ns' / 'nanoseconds' / 'nano' / 'nanos' / 'nanosecond' / 'N'

        .. versionchanged:: 1.1.0

           Must not be specified when `arg` context strings and
           ``errors="raise"``.

    errors : {'ignore', 'raise', 'coerce'}, default 'raise'
        - If 'raise', then invalid parsing will raise an exception.
        - If 'coerce', then invalid parsing will be set as NaT.
        - If 'ignore', then invalid parsing will return the input.

    Returns
    -------
    timedelta
        If parsing succeeded.
        Return type depends on input:

        - list-like: TimedeltaIndex of timedelta64 dtype
        - Series: Series of timedelta64 dtype
        - scalar: Timedelta

    See Also
    --------
    DataFrame.astype : Cast argument to a specified dtype.
    to_datetime : Convert argument to datetime.
    convert_dtypes : Convert dtypes.

    Notes
    -----
    If the precision is higher than nanoseconds, the precision of the duration is
    truncated to nanoseconds for string inputs.

    Examples
    --------
    Parsing a single string to a Timedelta:

    >>> pd.to_timedelta('1 days 06:05:01.00003')
    Timedelta('1 days 06:05:01.000030')
    >>> pd.to_timedelta('15.5us')
    Timedelta('0 days 00:00:00.000015500')

    Parsing a list or array of strings:

    >>> pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
    TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT],
                   dtype='timedelta64[ns]', freq=None)

    Converting numbers by specifying the `unit` keyword argument:

    >>> pd.to_timedelta(np.arange(5), unit='s')
    TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
                    '0 days 00:00:03', '0 days 00:00:04'],
                   dtype='timedelta64[ns]', freq=None)
    >>> pd.to_timedelta(np.arange(5), unit='d')
    TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
                   dtype='timedelta64[ns]', freq=None)
    N)ignorer   Zcoercez5errors must be one of 'ignore', 'raise', or 'coerce'.>   YMyzqUnits 'M', 'Y', and 'y' are no longer supported, as they do not represent unambiguous timedelta values durations.r   r   )indexname)r   r   r%   r   ndim   zBarg must be a string, timedelta, list, tuple, 1-d array, or Seriesz9unit must not be specified if the input is/contains a str)r
   
ValueError
isinstancer   _convert_listlikeZ_valuesZ_constructorr$   r%   r   npZndarrayr&   r   Zitem_from_zerodimr   getattr	TypeErrorstr _coerce_scalar_to_timedelta_type)r   r   r   valuesr   r   r   r   I   s4    e

nsr#   c                 C  sF   zt | |}W n2 tk
r@   |dkr( |dkr8|  Y S t}Y nX |S )z)Convert string 'r' to a timedelta object.r   r   )r	   r(   r   )rr   r   resultr   r   r   r/      s    
r/   )r   c                 C  s   t | ttfst| ds8t| ds*t| } tj| td} zt| ||ddd }W n& tk
rv   |dkrp|  Y S  Y nX ddl	m
} ||d	|d
}|S )z6Convert a list of objects to a timedelta index object.dtypeZ	__array__)r4   F)r   r   copyr   r   )r   r1   )r   r%   )r)   listtuplehasattrr+   arrayobjectr   r(   pandasr   )r   r   r   r%   Ztd64arrr   valuer   r   r   r*      s    
	r*   )..)..)..)Nr   )r1   r   )Nr   N)$__doc__
__future__r   datetimer   typingr   r   Znumpyr+   Zpandas._libsr   Zpandas._libs.tslibsr   r   Zpandas._libs.tslibs.timedeltasr	   r
   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.genericr   r   Zpandas.core.arrays.timedeltasr   r   Zpandas._typingr   r   r;   r   r   r   r   r/   r*   r   r   r   r   <module>   sJ                    