B
    Z^                 @   s   d Z ddlmZ ddlZddlZdddZdd Zd	d
 Z	dd Z
dddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )z7
Compatibility tools for various data structure inputs
    )rangeNMc             C   sh   ddl m}m} t| j||fs(td| jjd k	r>| jj}nt	| j}|
|sdtd||d S )Nr   )PeriodIndexDatetimeIndexz0The index must be a DatetimeIndex or PeriodIndexzExpected frequency {}. Got {})pandasr   r   
isinstanceindex
ValueErrorfreqZfreqstrpdZ
infer_freq
startswithformat)xr
   r   r   Zinferred_freq r   5lib/python3.7/site-packages/statsmodels/tools/data.py_check_period_index	   s    

r   c             C   s   t | tjS )N)r   r   Z	DataFrame)objr   r   r   is_data_frame   s    r   c             C   s   ddl m} t| |S )Nr   )DesignMatrix)Zpatsyr   r   )r   r   r   r   r   is_design_matrix   s    r   c             C   s   t | tjo| jjd k	S )N)r   npndarraydtypenames)r   r   r   r   _is_structured_ndarray    s    r   c             C   s   t | tjrTt| r.|dkr$| jj}t| }n| }|dkrdd t|jd D }n,t	| rx| 
 } | j}| j}| j}ntdt |tst|}t||jd krtd|dk	rt|t|krtd|||fS )aC  
    Convert passed data structure to form required by estimation classes

    Parameters
    ----------
    data : ndarray-like
    colnames : sequence or None
        May be part of data structure
    rownames : sequence or None

    Returns
    -------
    (values, colnames, rownames) : (homogeneous ndarray, list)
    Nc             S   s   g | ]}d | qS )zY_%dr   ).0ir   r   r   
<listcomp><   s    z"interpret_data.<locals>.<listcomp>   z-cannot handle other input types at the momentz;length of colnames does not match number of columns in dataz8length of rownames does not match number of rows in data)r   r   r   r   r   r   struct_to_ndarrayr   shaper   Zdropnavaluescolumnsr   	Exceptionlistlenr	   )dataZcolnamesZrownamesr!   r   r   r   interpret_data$   s*    

r'   c             C   s   | j tt| jjftjdS )N)type)Zviewfloatr%   r   r   r   r   )Zarrr   r   r   r   U   s    r   c             C   s$   t | tjko"t |tjkp"|d kS )N)r(   r   r   )endogexogr   r   r   _is_using_ndarray_typeY   s    r,   c             C   s    t | tjot |tjp|d kS )N)r   r   r   )r*   r+   r   r   r   _is_using_ndarray^   s    r-   c             C   s    ddl m} t| |pt||S )Nr   )data_klasses)Zstatsmodels.compat.pandasr.   r   )r*   r+   Zklassesr   r   r   _is_using_pandasc   s    r/   c             C   s(   yt | } t |}dS    dS d S )NTF)r   Zasarray)r*   r+   r   r   r   _is_array_likeh   s    

r0   c             C   s   t | ot |p|d kS )N)r   )r*   r+   r   r   r   _is_using_patsyq   s    r1   c             C   s   t | tjjS )z,
    Returns true if data is a recarray
    )r   r   ZcoreZrecarray)r&   r   r   r   _is_recarrayw   s    r2   )r   )NN)__doc__Zstatsmodels.compat.pythonr   Znumpyr   r   r   r   r   r   r   r'   r   r,   r-   r/   r0   r1   r2   r   r   r   r   <module>   s   

1	