U
    Cvf#                     @  s   d Z ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ dZed Zed Zd	e Zd	e Zd
e Zdd ZG dd dZdd ZddddZdddddZdd ZdS )z3FrequencyInferer analog for cftime.datetime objects    )annotationsN)_MONTH_ABBREVIATIONS)CFTimeIndex)_contains_datetime_like_objects   i  <      c                 C  s   ddl m} t| |tjfr| jdkr0tdnt|| sDtdt	| j
}|dkrft| j} n |dkr|t| j} n
t| j} t| trt| }| S t| S )aI  
    Infer the most likely frequency given the input index.

    Parameters
    ----------
    index : CFTimeIndex, DataArray, DatetimeIndex, TimedeltaIndex, Series
        If not passed a CFTimeIndex, this simply calls `pandas.infer_freq`.
        If passed a Series or a DataArray will use the values of the series (NOT THE INDEX).

    Returns
    -------
    str or None
        None if no discernible frequency.

    Raises
    ------
    TypeError
        If the index is not datetime-like.
    ValueError
        If there are fewer than three values or the index is not 1D.
    r   )	DataArrayr   z'index' must be 1Dz*'index' must contain datetime-like objectszdatetime64[ns]ztimedelta64[ns])Zxarray.core.dataarrayr	   
isinstancepdZSeriesndim
ValueErrorr   npZasarraydtypeZDatetimeIndexvaluesZTimedeltaIndexr   _CFTimeFrequencyInfererget_freq
infer_freq)indexr	   r   Zinferer r   =/tmp/pip-unpacked-wheel-h316xyqg/xarray/coding/frequencies.pyr   ;   s     



r   c                   @  s`   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dd Z
e	dd Ze	dd ZdS )r   c                 C  sJ   || _ |j| _t|dk r"td| j jp0| j j| _d | _d | _	d | _
d S )N   z(Need at least 3 dates to infer frequency)r   Zasi8r   lenr   Zis_monotonic_decreasingZis_monotonic_increasingis_monotonic_deltas_year_deltas_month_deltas)selfr   r   r   r   __init__h   s    z _CFTimeFrequencyInferer.__init__c                 C  s   | j r| jjsdS | jd }t|tr.|  S t| jdks@dS t|trXt	d|t S t|t
rpt	d|t
 S t|trt	d|t S t|trt	d|t S t	d|t S dS )	zFind the appropriate frequency string to describe the inferred frequency of self.index

        Adapted from `pandas.tsseries.frequencies._FrequencyInferer.get_freq` for CFTimeIndexes.

        Returns
        -------
        str or None
        Nr   r   HTSLU)r   r   Z	is_uniquedeltas_is_multiple_ONE_DAY_infer_daily_ruler   	_ONE_HOUR_maybe_add_count_ONE_MINUTE_ONE_SECOND
_ONE_MILLI
_ONE_MICRO)r   deltar   r   r   r   w   s     	





z _CFTimeFrequencyInferer.get_freqc           
      C  s   |   }|r>| jd }t| jd j }| d| }t||S |  }|r| jd d }dddd}t|| jd jd   }| d| }t||S |  }|rt|| jd S t	| j
dkr| j
d t }	td	|	S d S )
Nr   -r         
   )r      r   r   D)_get_annual_ruleyear_deltasr   r   monthr)   _get_quartely_rulemonth_deltas_get_monthly_ruler   r$   r&   )
r   Zannual_ruleZnyearsr7   aliasZquartely_ruleZ	nquartersmod_dictZmonthly_ruledaysr   r   r   r'      s(    



z)_CFTimeFrequencyInferer._infer_daily_rulec                 C  sB   t | jdkrd S t t| jjdkr,d S dddt| jS )Nr   ZASAcsce)r   r6   r   uniquer   r7   getmonth_anchor_checkr   r   r   r   r5      s
    z(_CFTimeFrequencyInferer._get_annual_rulec                 C  s>   t | jdkrd S | jd d dkr(d S dddt| jS )Nr   r   r   ZQSQr?   r   r9   rC   rD   r   rE   r   r   r   r8      s
    z*_CFTimeFrequencyInferer._get_quartely_rulec                 C  s(   t | jdkrd S dddt| jS )Nr   ZMSMr?   rG   rE   r   r   r   r:      s    z)_CFTimeFrequencyInferer._get_monthly_rulec                 C  s   | j dkrt| j| _ | j S )z)Sorted unique timedeltas as microseconds.N)r   _unique_deltasr   rE   r   r   r   r$      s    
z_CFTimeFrequencyInferer.deltasc                 C  s   | j dkrt| jj| _ | j S )zSorted unique year deltas.N)r   rI   r   yearrE   r   r   r   r6      s    
z#_CFTimeFrequencyInferer.year_deltasc                 C  s*   | j dkr$t| jjd | jj | _ | j S )zSorted unique month deltas.Nr0   )r   rI   r   rJ   r7   rE   r   r   r   r9      s    
z$_CFTimeFrequencyInferer.month_deltasN)__name__
__module____qualname__r   r   r'   r5   r8   r:   propertyr$   r6   r9   r   r   r   r   r   g   s   		

r   c                 C  s   t t t | S )z#Sorted unique deltas of numpy array)r   sortrB   Zdiff)Zarrr   r   r   rI      s    rI   int)multc                 C  s   | | dkS )z Whether us is a multiple of multr   r   )usrQ   r   r   r   r%      s    r%   strfloatbasecountc                 C  s4   |dkr,|t |kstt |}| |  S | S dS )z<If count is greater than 1, add it to the base offset stringr   N)rP   AssertionErrorrU   r   r   r   r)      s
    r)   c                 C  s^   d}d}| D ]8}|r"||j dkM }|r<|j |jk}||M }q|s qFq|rNdS |rVdS dS dS )a  Return the monthly offset string.

    Return "cs" if all dates are the first days of the month,
    "ce" if all dates are the last day of the month,
    None otherwise.

    Replicated pandas._libs.tslibs.resolution.month_position_check
    but without business offset handling.
    Tr   rA   r@   N)dayZdaysinmonth)datesZcalendar_endZcalendar_startdatecalr   r   r   rD      s    

rD   )__doc__
__future__r   Znumpyr   Zpandasr   Zxarray.coding.cftime_offsetsr   Zxarray.coding.cftimeindexr   Zxarray.core.commonr   r-   r,   r+   r*   r(   r&   r   r   rI   r%   r)   rD   r   r   r   r   <module>   s$   ),y
