ó
šxŠ\c           @   så   d  Z  d d l Z d d l j j 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 j Z d „  Z d „  Z d d „ Z d	 „  Z e d e d
 „ Z d „  Z d S(   sL   
Table Schema builders

http://specs.frictionlessdata.io/json-table-schema/
iÿÿÿÿN(	   t   is_bool_dtypet   is_categorical_dtypet   is_datetime64_dtypet   is_datetime64tz_dtypet   is_integer_dtypet   is_numeric_dtypet   is_period_dtypet   is_string_dtypet   is_timedelta64_dtype(   t	   DataFrame(   t   CategoricalDtypec         C   s   t  |  ƒ r d St |  ƒ r  d St |  ƒ r0 d St |  ƒ sT t |  ƒ sT t |  ƒ rX d St |  ƒ rh d St |  ƒ rx d St |  ƒ rˆ d Sd Sd S(	   s”  
    Convert a NumPy / pandas type to its corresponding json_table.

    Parameters
    ----------
    x : array or dtype

    Returns
    -------
    t : 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
    =============== =================
    t   integert   booleant   numbert   datetimet   durationt   anyt   stringN(	   R   R    R   R   R   R   R   R   R   (   t   x(    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt   as_json_table_type   s     c         C   s   t  j |  j j Œ  r‘ |  j j } t | ƒ d k rU |  j j d k rU t j d ƒ n8 t | ƒ d k r t d „  | Dƒ ƒ r t j d ƒ n  |  S|  j	 ƒ  }  |  j j
 d k rg  t |  j j ƒ D]- \ } } | d k	 rà | n d j | ƒ ^ qÂ } | |  j _ n |  j j pd |  j _ |  S(   s?   Sets index names to 'index' for regular, or 'level_x' for Multii   t   indexs,   Index name of 'index' is not round-trippablec         s   s   |  ] } | j  d  ƒ Vq d S(   t   level_N(   t
   startswith(   t   .0R   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pys	   <genexpr>M   s    s;   Index names beginning with 'level_' are not round-trippables   level_{}N(   t   comt   _all_not_noneR   t   namest   lent   namet   warningst   warnR   t   copyt   nlevelst	   enumeratet   Nonet   format(   t   datat   nmst   iR   R   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt   set_default_namesG   s    $(Cc         C   s  | p |  j  } |  j d  k r' d } n	 |  j } i | d 6t | ƒ d 6} t |  ƒ r¶ t |  d ƒ rz |  j } |  j } n |  j j } |  j j } i t	 | ƒ d 6| d <| | d <n` t
 |  ƒ rÒ |  j | d <nD t |  ƒ rt |  d	 ƒ r|  j j j | d
 <q|  j j | d
 <n  | S(   Nt   valuesR   t   typet
   categoriest   enumt   constraintst   orderedt   freqt   dtt   tz(   t   dtypeR   R"   R   R   t   hasattrR*   R-   t   catt   listR   t   freqstrR   R/   R0   t   zone(   t   arrR1   R   t   fieldt   catsR-   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt!   convert_pandas_type_to_json_field\   s*    		
	c         C   só   |  d } | d k r d S| d k r* d S| d k r: d S| d k rJ d	 S| d
 k rZ d S| d k r |  j  d ƒ r‰ d j d |  d ƒ Sd SnJ | d k rÚ d |  k rÓ d |  k rÓ t d |  d d d |  d ƒ Sd Sn  t d j | ƒ ƒ ‚ d S(   sá  
    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',
                                           'contraints': {'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]'
    R)   R   t   objectR   t   int64R   t   float64R   t   boolR   t   timedelta64R   R0   s   datetime64[ns, {tz}]s   datetime64[ns]R   R,   R-   R*   R+   s%   Unsupported or invalid field type: {}N(   t   getR#   R
   t
   ValueError(   R8   t   typ(    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt!   convert_json_field_to_pandas_typex   s*    %
c   	      C   sm  | t  k r t |  ƒ }  n  i  } g  } | r… |  j j d k rl x@ |  j j D] } | j t | ƒ ƒ qL Wq… | j t |  j ƒ ƒ n  |  j d k rÇ xC |  j ƒ  D] \ } } | j t | ƒ ƒ q¡ Wn | j t |  ƒ ƒ | | d <| r=|  j j	 r=| d k r=|  j j d k r*|  j j g | d <qV|  j j | d <n | d k	 rV| | d <n  | rid | d <n  | S(   sÆ  
    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 `_as_json_table_type` 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']}
    i   t   fieldst
   primaryKeys   0.20.0t   pandas_versionN(   t   TrueR'   R   R    t   levelst   appendR:   t   ndimt	   iteritemst	   is_uniqueR"   R   R   (	   R$   R   t   primary_keyt   versiont   schemaRD   t   levelt   columnt   s(    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt   build_table_schema·   s.    .
c         C   sh  t  |  d | ƒ} g  | d d D] } | d ^ q! } t | d d | ƒ| } d „  | d d Dƒ } t d „  | j ƒ  Dƒ ƒ r” t d	 ƒ ‚ n  d
 | j ƒ  k rµ t d ƒ ‚ n  | j | ƒ } d | d k rd| j | d d ƒ } t | j j	 ƒ d k r'| j j
 d k rad | j _
 qaqdg  | j j	 D]! } | j d ƒ rOd n | ^ q4| j _	 n  | S(   s  
    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
    t   precise_floatRO   RD   R   R$   t   columnsc         S   s#   i  |  ] } t  | ƒ | d  “ q S(   R   (   RC   (   R   R8   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pys
   <dictcomp>.  s   	c         s   s$   |  ] } t  | ƒ j d  ƒ Vq d S(   s   datetime64[ns, N(   t   strR   (   R   R   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pys	   <genexpr>2  s    s-   table="orient" can not yet read timezone dataR?   s<   table="orient" can not yet read ISO-formatted Timedelta dataRE   i   R   R   N(   t   loadsR	   R   R(   t   NotImplementedErrort   astypet	   set_indexR   R   R   R   R"   R   (   t   jsonRT   t   tableR8   t	   col_ordert   dft   dtypesR   (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt   parse_table_schema  s$    $%:(    t   __doc__R   t   pandas._libs.jsont   _libsR[   t   pandas.core.dtypes.commonR    R   R   R   R   R   R   R   R   t   pandasR	   t   pandas.api.typesR
   t   pandas.core.commont   coret   commonR   RW   R   R'   R"   R:   RC   RG   RS   R`   (    (    (    s:   lib/python2.7/site-packages/pandas/io/json/table_schema.pyt   <module>   s   @		1		?O