ó
šxŠ\c           @   sˆ   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z m	 Z	 d „  Z
 d d d d	 „ Z d d d d d
 d d „ Z d S(   iÿÿÿÿ(   t   defaultdictN(   t   convert_json_to_lines(   t	   DataFramet   compatc         C   s<   |  d d k r% |  d d k r% |  S|  d d !}  t  |  ƒ S(   sJ   
    Helper function that converts JSON lists to line delimited JSON.
    i    t   [iÿÿÿÿt   ]i   (   R   (   t   s(    (    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyt   _convert_to_line_delimits   s    !t    t   .i    c      	   C   s9  t  } t |  t ƒ r' |  g }  t } n  g  } x÷ |  D]ï } t j | ƒ } xÊ | j ƒ  D]¼ \ } }	 t | t j ƒ sƒ t	 | ƒ } n  | d k r˜ | }
 n | | | }
 t |	 t ƒ sã | d k rV | j
 | ƒ }	 |	 | |
 <qV qV qV | j
 | ƒ }	 | j t |	 |
 | | d ƒ ƒ qV W| j | ƒ q4 W| r5| d S| S(   s  
    A simplified json_normalize.

    Converts a nested dict into a flat dict ("record"), unlike json_normalize,
    it does not attempt to extract a subset of the data.

    Parameters
    ----------
    ds : dict or list of dicts
    prefix: the prefix, optional, default: ""
    sep : string, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar

        .. versionadded:: 0.20.0

    level: the number of levels in the jason string, optional, default: 0

    Returns
    -------
    d - dict or list of dicts, matching `ds`

    Examples
    --------

    IN[52]: nested_to_record(dict(flat1=1,dict1=dict(c=1,d=2),
                                  nested=dict(e=dict(c=1,d=2),d=2)))
    Out[52]:
    {'dict1.c': 1,
     'dict1.d': 2,
     'flat1': 1,
     'nested.d': 2,
     'nested.e.c': 1,
     'nested.e.d': 2}
    i    i   (   t   Falset
   isinstancet   dictt   Truet   copyt   deepcopyt   itemsR   t   string_typest   strt   popt   updatet   nested_to_recordt   append(   t   dst   prefixt   sept   levelt	   singletont   new_dst   dt   new_dt   kt   vt   newkey(    (    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyR      s0    $			$t   raisec      	      sL  d „  ‰  t  |  t ƒ r& |  r& t ƒ  St  |  t ƒ rA |  g }  n  | d k r‚ t d „  |  Dƒ ƒ rx t |  d | ƒ}  n  t |  ƒ St  | t ƒ s | g } n  ˆ d k r² g  ‰ n t  ˆ t ƒ sÍ ˆ g ‰ n  g  ˆ D]$ } t  | t ƒ rï | n | g ^ qÔ ‰ g  ‰ g  ‰ t t ƒ ‰ t  | t j	 ƒ s7t
 | ƒ } n  g  ˆ D] } | j | ƒ ^ q>‰ d ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d † ‰ ˆ |  | i  d d ƒt ˆ ƒ }	 ˆ d k	 rÌ|	 j d ‡ f d †  ƒ }	 n  xy t j ˆ ƒ D]h \ }
 } | d k	 r| |
 }
 n  |
 |	 k r(t d	 j d
 |
 ƒ ƒ ‚ n  t j | ƒ j ˆ ƒ |	 |
 <qÜW|	 S(   s1  
    Normalize semi-structured JSON data into a flat table.

    Parameters
    ----------
    data : dict or list of dicts
        Unserialized JSON objects
    record_path : string or list of strings, default None
        Path in each object to list of records. If not passed, data will be
        assumed to be an array of records
    meta : list of paths (string or list of strings), default None
        Fields to use as metadata for each record in resulting table
    meta_prefix : string, default None
    record_prefix : string, default None
        If True, prefix records with dotted (?) path, e.g. foo.bar.field if
        path to records is ['foo', 'bar']
    errors : {'raise', 'ignore'}, default 'raise'

        * 'ignore' : will ignore KeyError if keys listed in meta are not
          always present
        * 'raise' : will raise KeyError if keys listed in meta are not
          always present

        .. versionadded:: 0.20.0

    sep : string, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar

        .. versionadded:: 0.20.0

    Returns
    -------
    frame : DataFrame

    Examples
    --------

    >>> from pandas.io.json import json_normalize
    >>> data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
    ...         {'name': {'given': 'Mose', 'family': 'Regner'}},
    ...         {'id': 2, 'name': 'Faye Raker'}]
    >>> json_normalize(data)
        id        name name.family name.first name.given name.last
    0  1.0         NaN         NaN     Coleen        NaN      Volk
    1  NaN         NaN      Regner        NaN       Mose       NaN
    2  2.0  Faye Raker         NaN        NaN        NaN       NaN

    >>> data = [{'state': 'Florida',
    ...          'shortname': 'FL',
    ...          'info': {
    ...               'governor': 'Rick Scott'
    ...          },
    ...          'counties': [{'name': 'Dade', 'population': 12345},
    ...                      {'name': 'Broward', 'population': 40000},
    ...                      {'name': 'Palm Beach', 'population': 60000}]},
    ...         {'state': 'Ohio',
    ...          'shortname': 'OH',
    ...          'info': {
    ...               'governor': 'John Kasich'
    ...          },
    ...          'counties': [{'name': 'Summit', 'population': 1234},
    ...                       {'name': 'Cuyahoga', 'population': 1337}]}]
    >>> result = json_normalize(data, 'counties', ['state', 'shortname',
    ...                                           ['info', 'governor']])
    >>> result
             name  population info.governor    state shortname
    0        Dade       12345    Rick Scott  Florida        FL
    1     Broward       40000    Rick Scott  Florida        FL
    2  Palm Beach       60000    Rick Scott  Florida        FL
    3      Summit        1234   John Kasich     Ohio        OH
    4    Cuyahoga        1337   John Kasich     Ohio        OH

    >>> data = {'A': [1, 2]}
    >>> json_normalize(data, 'A', record_prefix='Prefix.')
        Prefix.0
    0          1
    1          2
    c         S   sA   |  } t  | t ƒ r3 x% | D] } | | } q Wn
 | | } | S(   N(   R   t   list(   t   jst   spect   resultt   field(    (    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyt   _pull_field·   s    
c         s   s:   |  ]0 } g  t  j | ƒ D] } t | t ƒ ^ q Vq d  S(   N(   R   t
   itervaluesR   R   (   t   .0t   yt   x(    (    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pys	   <genexpr>É   s   R   i    c   
         s°  t  |  t ƒ r |  g }  n  t | ƒ d k r· x||  D]| } xM t ˆ ˆ ƒ D]< \ } } | d t | ƒ k rJ ˆ  | | d ƒ | | <qJ qJ Wˆ | | d | d | d | d ƒq4 Wnõ xò |  D]ê } ˆ  | | d ƒ } ˆ j t | ƒ ƒ x® t ˆ ˆ ƒ D] \ } } | d t | ƒ k r)| | } n] y ˆ  | | | ƒ } WnC t k
 r…}	 ˆ d k rjt j } q†t d j d |	 ƒ ƒ ‚ n Xˆ | j | ƒ qú Wˆ j	 | ƒ q¾ Wd  S(   Ni   iÿÿÿÿi    R   t   ignoresC   Try running with errors='ignore' as key {err} is not always presentt   err(
   R   R   t   lent   zipR   t   KeyErrort   npt   nant   formatt   extend(
   t   datat   patht	   seen_metaR   t   objt   valt   keyt   recst   meta_valt   e(   R(   t   _recursive_extractt   errorst   lengthst   metat	   meta_keyst	   meta_valst   records(    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyR?   ç   s0    R   t   columnsc            s   d j  d ˆ  d |  ƒ S(   Ns   {p}{c}t   pt   c(   R4   (   R,   (   t   record_prefix(    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyt   <lambda>  s    s=   Conflicting metadata name {name}, need distinguishing prefix t   nameN(   R   R#   R   R   t   Nonet   anyR   R    R   R   R   t   joint   renamet	   iteritemst
   ValueErrorR4   R2   t   arrayt   repeat(   R6   t   record_pathRB   t   meta_prefixRI   R@   R   t   mR:   R&   R   R    (    (	   R(   R?   R@   RA   RB   RC   RD   RI   RE   s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyt   json_normalizec   sJ    T	
	
	1"'$	 (   t   collectionsR    R   t   numpyR2   t   pandas._libs.writersR   t   pandasR   R   R   R   RL   RW   (    (    (    s7   lib/python2.7/site-packages/pandas/io/json/normalize.pyt   <module>   s   	G