HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/io/json/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/io/json/__pycache__/_table_schema.cpython-36.pyc
3

���h?)�@sdZddlmZmZmZmZmZddlZddlj	j
Z
ddlmZm
Z
mZddlmZmZmZmZmZmZmZmZmZddlmZddlmZddljjZ er�ddl!m"Z"e
j#Z#ee$d	�d
d�Z%dd
�Z&dd�Z'dd�Z(de
e)ee)e)ee$efd�dd�Z*dd�Z+dS)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�returncCslt|�rdSt|�rdSt|�r$dSt|�s<t|�s<t|�r@dSt|�rLdSt|�rXdSt|�rddSdSdS)	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)	rr
rrr
rrrr)r�r�>/tmp/pip-build-5_djhm0z/pandas/pandas/io/json/_table_schema.py�as_json_table_type"sr!cCs�tj|jj�rf|jj}t|�dkr:|jjdkr:tjd�n(t|�dkrbtdd�|D��rbtjd�|S|j	�}|jj
dkr�dd�t|jj�D�}||j_n|jjp�d|j_|S)	z?Sets index names to 'index' for regular, or 'level_x' for Multi��indexz,Index name of 'index' is not round-trippablecss|]}|jd�VqdS)�level_N)�
startswith)�.0rrrr �	<genexpr>Xsz$set_default_names.<locals>.<genexpr>z;Index names beginning with 'level_' are not round-trippablecSs&g|]\}}|dk	r|nd|���qS)Nr$r)r&�i�namerrr �
<listcomp>_sz%set_default_names.<locals>.<listcomp>)�comZall_not_noner#�names�lenr)�warnings�warnr�copy�nlevels�	enumerate)�dataZnmsr,rrr �set_default_namesRs

r4cCs�|j}|jdkrd}n|j}|t|�d�}t|�rX|j}|j}dt|�i|d<||d<n*t|�rn|jj	|d<nt
|�r�|jj|d<|S)N�values)r)�type�enum�constraints�ordered�freq�tz)
�dtyper)r!r�
categoriesr9�listrr:Zfreqstrr
r;�zone)Zarrr<r)�fieldZcatsr9rrr �!convert_pandas_type_to_json_fieldhs 

rAcCs�|d}|dkrdS|dkr dS|dkr,dS|dkr8d	S|d
krDdS|dkrl|jd
�rfd|d
�d�SdSn4|dkr�d|kr�d|kr�t|dd|dd�SdStd|����dS)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]'
    r6r�objectrZint64rZfloat64r�boolr�timedelta64rr;zdatetime64[ns, �]zdatetime64[ns]rr8r9r7)r=r9z#Unsupported or invalid field type: N)�getr�
ValueError)r@�typrrr �!convert_json_field_to_pandas_type�s*%
rIT)r3r#�primary_key�versionrcCs,|dkrt|�}i}g}|r�|jjdkrrtd|j�|_xJt|jj|jj�D]"\}}t|�}||d<|j|�qJWn|jt|j��|j	dkr�x2|j
�D]\}	}
|jt|
��q�Wn|jt|��||d<|o�|jjo�|dk�r|jjdkr�|jjg|d<n|jj|d<n|dk	�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"rr)�fieldsN�
primaryKeyz0.20.0Zpandas_version)
r4r#r1r�zipZlevelsr,rA�append�ndim�itemsZ	is_uniquer))r3r#rJrK�schemarL�levelr)Z	new_field�column�srrr �build_table_schema�s45

rVcCs�t||d�}dd�|ddD�}t|d|d�|}dd	�|ddD�}td
d�|j�D��rjtd��d
|j�kr~td��|j|�}d|dkr�|j|dd�}t|jj	�dkr�|jj
dkr�d|j_
ndd�|jj	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_floatcSsg|]}|d�qS)r)r)r&r@rrr r*>sz&parse_table_schema.<locals>.<listcomp>rRrLr3)�columnscSsi|]}t|�|d�qS)r))rI)r&r@rrr �
<dictcomp>Asz&parse_table_schema.<locals>.<dictcomp>css|]}t|�jd�VqdS)zdatetime64[ns, N)�strr%)r&rrrr r'Gsz%parse_table_schema.<locals>.<genexpr>z-table="orient" can not yet read timezone datarDz<table="orient" can not yet read ISO-formatted Timedelta datarMr"r#NcSsg|]}|jd�rdn|�qS)r$N)r%)r&rrrr r*Ys)�loadsrrr5�NotImplementedErrorZastypeZ	set_indexr-r#r,r))�jsonrW�tableZ	col_orderZdfZdtypesrrr �parse_table_schemas$$

r_)TNT),�__doc__Ztypingrrrrrr.Zpandas._libs.jsonZ_libsr]Zpandas._typingrrr	Zpandas.core.dtypes.commonr
rrr
rrrrrZpandas.core.dtypes.dtypesrZpandasrZpandas.core.common�core�commonr+Zpandas.core.indexes.multirr[rZr!r4rArIrCrVr_rrrr �<module>s(,0BT