U
    Kvf9                     @   s|   d 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
    N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   :/tmp/pip-unpacked-wheel-2v6byqio/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 r   )r   npndarraydtypenamesr   r   r   r   _is_structured_ndarray   s    r   c                 C   s   t | tjr8t| }|dkrrdd t|jd D }n:t| r\|  } | j}| j	}| j
}ntdjt| jdt |tst|}t||jd krtd|dk	rt|t|krtd|||fS )	aA  
    Convert passed data structure to form required by estimation classes

    Parameters
    ----------
    data : array_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>6   s     z"interpret_data.<locals>.<listcomp>   zCannot handle input type {typ})typz;length of colnames does not match number of columns in dataz8length of rownames does not match number of rows in data)r   r   r   asarrayrangeshaper   Zdropnavaluescolumnsr   	TypeErrorr   type__name__listlenr   )dataZcolnamesZrownamesr$   r   r   r   interpret_data#   s&    

r,   c                 C   s   | j tt| jjfftjdS )N)r'   )viewfloatr*   r   r   r   r   )Zarrr   r   r   struct_to_ndarrayP   s    r/   c                 C   s$   t | tjko"t |tjkp"|d kS r   )r'   r   r   endogexogr   r   r   _is_using_ndarray_typeT   s    r3   c                 C   s    t | tjot |tjp|d kS r   )r   r   r   r0   r   r   r   _is_using_ndarrayY   s    r4   c                 C   s    ddl m} t| |pt||S )Nr   )data_klasses)Zstatsmodels.compat.pandasr5   r   )r1   r2   Zklassesr   r   r   _is_using_pandas^   s    r6   c                 C   s.   zt | } t |}W dS    Y dS X d S )NTF)r   r!   r0   r   r   r   _is_array_likec   s    

r7   c                 C   s   t | ot |p|d kS r   )r   r0   r   r   r   _is_using_patsyl   s    r8   c                 C   s   t | tjjS )z,
    Returns true if data is a recarray
    )r   r   coreZrecarray)r+   r   r   r   _is_recarrayr   s    r:   )r   )NN)__doc__Znumpyr   r   r
   r   r   r   r   r,   r/   r3   r4   r6   r7   r8   r:   r   r   r   r   <module>   s   

-	