3
Ud?)                 @   s  d Z ddlmZmZmZmZmZ ddlZddlj	j
Z
ddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddljjZ erddl!m"Z" e
j#Z#ee$d	d
dZ%dd Z&dd Z'dd Z(dee)ee) e)ee$ef dddZ*dd Z+dS )zM
Table Schema builders

https://specs.frictionlessdata.io/json-table-schema/
    )TYPE_CHECKINGAnyDictOptionalcastN)DtypeObjFrameOrSeriesJSONSerializable)	is_bool_dtypeis_categorical_dtypeis_datetime64_dtypeis_datetime64tz_dtypeis_integer_dtypeis_numeric_dtypeis_period_dtypeis_string_dtypeis_timedelta64_dtype)CategoricalDtype)	DataFrame)
MultiIndex)xreturnc             C   sl   t | rdS t| rdS t| r$dS t| s<t| s<t| r@dS t| rLdS t| rXdS t| rddS dS dS )	a  
    Convert a NumPy / pandas type to its corresponding json_table.

    Parameters
    ----------
    x : np.dtype or ExtensionDtype

    Returns
    -------
    str
        the Table Schema data types

    Notes
    -----
    This table shows the relationship between NumPy / pandas dtypes,
    and Table Schema dtypes.

    ==============  =================
    Pandas type     Table Schema type
    ==============  =================
    int64           integer
    float64         number
    bool            boolean
    datetime64[ns]  datetime
    timedelta64[ns] duration
    object          str
    categorical     any
    =============== =================
    integerbooleannumberdatetimedurationanystringN)	r   r
   r   r   r   r   r   r   r   )r    r   P/var/www/html/virt/lib64/python3.6/site-packages/pandas/io/json/_table_schema.pyas_json_table_type"   s    r!   c             C   s   t j| jj rf| jj}t|dkr:| jjdkr:tjd n(t|dkrbtdd |D rbtjd | S | j	 } | jj
dkrdd t| jjD }|| j_n| jjpd| j_| S )	z?Sets index names to 'index' for regular, or 'level_x' for Multi   indexz,Index name of 'index' is not round-trippablec             s   s   | ]}|j d V  qdS )level_N)
startswith).0r   r   r   r    	<genexpr>X   s    z$set_default_names.<locals>.<genexpr>z;Index names beginning with 'level_' are not round-trippablec             S   s&   g | ]\}}|d k	r|nd| qS )Nr$   r   )r&   inamer   r   r    
<listcomp>_   s   z%set_default_names.<locals>.<listcomp>)comZall_not_noner#   nameslenr)   warningswarnr   copynlevels	enumerate)dataZnmsr,   r   r   r    set_default_namesR   s    

r4   c             C   s   | j }| jd krd}n| j}|t|d}t|rX|j}|j}dt|i|d< ||d< n*t|rn|jj	|d< nt
|r|jj|d< |S )Nvalues)r)   typeenumconstraintsorderedfreqtz)dtyper)   r!   r   
categoriesr9   listr   r:   Zfreqstrr   r;   zone)Zarrr<   r)   fieldZcatsr9   r   r   r    !convert_pandas_type_to_json_fieldh   s     

rA   c             C   s   | d }|dkrdS |dkr dS |dkr,dS |dkr8d	S |d
krDdS |dkrl| j drfd| d  dS dS n4|dkrd| krd| krt| d d | d dS dS td| dS )a  
    Converts a JSON field descriptor into its corresponding NumPy / pandas type

    Parameters
    ----------
    field
        A JSON field descriptor

    Returns
    -------
    dtype

    Raises
    ------
    ValueError
        If the type of the provided field is unknown or currently unsupported

    Examples
    --------
    >>> convert_json_field_to_pandas_type({'name': 'an_int',
                                           'type': 'integer'})
    'int64'
    >>> convert_json_field_to_pandas_type({'name': 'a_categorical',
                                           'type': 'any',
                                           'constraints': {'enum': [
                                                          'a', 'b', 'c']},
                                           'ordered': True})
    'CategoricalDtype(categories=['a', 'b', 'c'], ordered=True)'
    >>> convert_json_field_to_pandas_type({'name': 'a_datetime',
                                           'type': 'datetime'})
    'datetime64[ns]'
    >>> convert_json_field_to_pandas_type({'name': 'a_datetime_with_tz',
                                           'type': 'datetime',
                                           'tz': 'US/Central'})
    'datetime64[ns, US/Central]'
    r6   r   objectr   Zint64r   Zfloat64r   boolr   timedelta64r   r;   zdatetime64[ns, ]zdatetime64[ns]r   r8   r9   r7   )r=   r9   z#Unsupported or invalid field type: N)getr   
ValueError)r@   typr   r   r    !convert_json_field_to_pandas_type   s*    %
rI   T)r3   r#   primary_keyversionr   c             C   s,  |dkrt | } i }g }|r| jjdkrrtd| j| _xJt| jj| jjD ]"\}}t|}||d< |j| qJW n|jt| j | j	dkrx2| j
 D ]\}	}
|jt|
 qW n|jt|  ||d< |o| jjo|dkr| jjdkr| jjg|d< n| jj|d< n|dk	r||d< |r(d|d	< |S )
a
  
    Create a Table schema from ``data``.

    Parameters
    ----------
    data : Series, DataFrame
    index : bool, default True
        Whether to include ``data.index`` in the schema.
    primary_key : bool or None, default True
        Column names to designate as the primary key.
        The default `None` will set `'primaryKey'` to the index
        level or levels if the index is unique.
    version : bool, default True
        Whether to include a field `pandas_version` with the version
        of pandas that generated the schema.

    Returns
    -------
    schema : dict

    Notes
    -----
    See `Table Schema
    <https://pandas.pydata.org/docs/user_guide/io.html#table-schema>`__ for
    conversion types.
    Timedeltas as converted to ISO8601 duration format with
    9 decimal places after the seconds field for nanosecond precision.

    Categoricals are converted to the `any` dtype, and use the `enum` field
    constraint to list the allowed values. The `ordered` attribute is included
    in an `ordered` field.

    Examples
    --------
    >>> df = pd.DataFrame(
    ...     {'A': [1, 2, 3],
    ...      'B': ['a', 'b', 'c'],
    ...      'C': pd.date_range('2016-01-01', freq='d', periods=3),
    ...     }, index=pd.Index(range(3), name='idx'))
    >>> build_table_schema(df)
    {'fields': [{'name': 'idx', 'type': 'integer'},
    {'name': 'A', 'type': 'integer'},
    {'name': 'B', 'type': 'string'},
    {'name': 'C', 'type': 'datetime'}],
    'pandas_version': '0.20.0',
    'primaryKey': ['idx']}
    Tr"   r   r)   fieldsN
primaryKeyz0.20.0Zpandas_version)r4   r#   r1   r   zipZlevelsr,   rA   appendndimitemsZ	is_uniquer)   )r3   r#   rJ   rK   schemarL   levelr)   Z	new_fieldcolumnsr   r   r    build_table_schema   s4    5

rV   c             C   s   t | |d}dd |d d D }t|d |d| }dd	 |d d D }td
d |j D rjtdd|j kr~td|j|}d|d kr|j|d d }t|jj	dkr|jj
dkrd|j_
ndd |jj	D |j_	|S )a  
    Builds a DataFrame from a given schema

    Parameters
    ----------
    json :
        A JSON table schema
    precise_float : boolean
        Flag controlling precision when decoding string to double values, as
        dictated by ``read_json``

    Returns
    -------
    df : DataFrame

    Raises
    ------
    NotImplementedError
        If the JSON table schema contains either timezone or timedelta data

    Notes
    -----
        Because :func:`DataFrame.to_json` uses the string 'index' to denote a
        name-less :class:`Index`, this function sets the name of the returned
        :class:`DataFrame` to ``None`` when said string is encountered with a
        normal :class:`Index`. For a :class:`MultiIndex`, the same limitation
        applies to any strings beginning with 'level_'. Therefore, an
        :class:`Index` name of 'index'  and :class:`MultiIndex` names starting
        with 'level_' are not supported.

    See Also
    --------
    build_table_schema : Inverse function.
    pandas.read_json
    )precise_floatc             S   s   g | ]}|d  qS )r)   r   )r&   r@   r   r   r    r*   >  s    z&parse_table_schema.<locals>.<listcomp>rR   rL   r3   )columnsc             S   s   i | ]}t ||d  qS )r)   )rI   )r&   r@   r   r   r    
<dictcomp>A  s   z&parse_table_schema.<locals>.<dictcomp>c             s   s   | ]}t |jd V  qdS )zdatetime64[ns, N)strr%   )r&   r   r   r   r    r'   G  s    z%parse_table_schema.<locals>.<genexpr>z-table="orient" can not yet read timezone datarD   z<table="orient" can not yet read ISO-formatted Timedelta datarM   r"   r#   Nc             S   s   g | ]}|j d rdn|qS )r$   N)r%   )r&   r   r   r   r    r*   Y  s    )loadsr   r   r5   NotImplementedErrorZastypeZ	set_indexr-   r#   r,   r)   )jsonrW   tableZ	col_orderdfZdtypesr   r   r    parse_table_schema  s$    $

r`   )TNT),__doc__typingr   r   r   r   r   r.   Zpandas._libs.jsonZ_libsr]   Zpandas._typingr   r   r	   Zpandas.core.dtypes.commonr
   r   r   r   r   r   r   r   r   Zpandas.core.dtypes.dtypesr   Zpandasr   Zpandas.core.commoncorecommonr+   Zpandas.core.indexes.multir   r[   rZ   r!   r4   rA   rI   rC   rV   r`   r   r   r   r    <module>   s(   ,0B  T