U
    b,D                     @  s  d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 d dl
Zd dlmZ d dlmZ d dlmZ d dlZd dlmZ d	d	d
ddZd,d	d	dddddZdd	dd	ddddZdd	ddddZd-dd	dddd Zd.dd"d#d$d$d	d	dd%d&	d'd(Zed)ed*d+ZdS )/    )annotations)abcdefaultdictN)AnyDefaultDictIterableconvert_json_to_lines)Scalar)	deprecate)	DataFramestr)sreturnc                 C  s0   | d dks| d dkr| S | dd } t | S )zJ
    Helper function that converts JSON lists to line delimited JSON.
    r   []   r   )r    r   =/tmp/pip-unpacked-wheel-ck39h295/pandas/io/json/_normalize.pyconvert_to_line_delimits   s    r    .intz
int | None)prefixseplevel	max_levelc              
   C  s   d}t | tr| g} d}g }| D ]}t|}| D ]\}	}
t |	tsPt|	}	|dkr^|	}n|| |	 }t |
tr|dk	r||kr|dkr6||	}
|
||< q6q6||	}
|t|
|||d | q6|	| q |r|d S |S )a  
    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 : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar
    level: int, optional, default: 0
        The number of levels in the json string.

    max_level: int, optional, default: None
        The max depth to normalize.

        .. versionadded:: 0.25.0

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

    Examples
    --------
    >>> nested_to_record(
    ...     dict(flat1=1, dict1=dict(c=1, d=2), nested=dict(e=dict(c=1, d=2), d=2))
    ... )
    {'flat1': 1, 'dict1.c': 1, 'dict1.d': 2, 'nested.e.c': 1, 'nested.e.d': 2, 'nested.d': 2}
    FTr   Nr   )

isinstancedictcopydeepcopyitemsr   popupdatenested_to_recordappend)dsr   r   r   r   Z	singletonZnew_dsdZnew_dkvZnewkeyr   r   r   r%   '   s8    .





r%   r   dict[str, Any])data
key_stringnormalized_dict	separatorr   c                 C  sn   t | trb|  D ]L\}}| | | }t||dt| |krF|n|t|d ||d qn| ||< |S )a3  
    Main recursive function
    Designed for the most basic use case of pd.json_normalize(data)
    intended as a performance improvement, see #15621

    Parameters
    ----------
    data : Any
        Type dependent on types contained within nested Json
    key_string : str
        New key (with separator(s) in) for data
    normalized_dict : dict
        The new normalized/flattened Json dict
    separator : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar
    Nr,   r-   r.   r/   )r   r   r"   _normalise_jsonlen)r,   r-   r.   r/   keyvalueZnew_keyr   r   r   r1   z   s    

r1   )r,   r/   r   c                 C  s8   dd |   D }tdd |   D di |d}||S )aw  
    Order the top level keys and then recursively go to depth

    Parameters
    ----------
    data : dict or list of dicts
    separator : str, default '.'
        Nested records will generate names separated by sep,
        e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar

    Returns
    -------
    dict or list of dicts, matching `normalised_json_object`
    c                 S  s    i | ]\}}t |ts||qS r   r   r   .0r)   r*   r   r   r   
<dictcomp>   s     
  z+_normalise_json_ordered.<locals>.<dictcomp>c                 S  s    i | ]\}}t |tr||qS r   r5   r6   r   r   r   r8      s     
  r   r0   )r"   r1   )r,   r/   Z	top_dict_Znested_dict_r   r   r   _normalise_json_ordered   s    r9   zdict | list[dict]zdict | list[dict] | Any)r'   r   r   c                   s@   i }t | trt|  d}n t | tr< fdd| D }|S |S )a  
    A optimized basic json_normalize

    Converts a nested dict into a flat dict ("record"), unlike
    json_normalize and nested_to_record it doesn't do anything clever.
    But for the most basic use cases it enhances performance.
    E.g. pd.json_normalize(data)

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

    Returns
    -------
    frame : DataFrame
    d - dict or list of dicts, matching `normalised_json_object`

    Examples
    --------
    >>> _simple_json_normalize(
    ...     {
    ...         "flat1": 1,
    ...         "dict1": {"c": 1, "d": 2},
    ...         "nested": {"e": {"c": 1, "d": 2}, "d": 2},
    ...     }
    ... )
    {'flat1': 1, 'dict1.c': 1, 'dict1.d': 2, 'nested.e.c': 1, 'nested.e.d': 2, 'nested.d': 2}

    )r,   r/   c                   s   g | ]}t | d qS )r   )_simple_json_normalize)r7   rowr:   r   r   
<listcomp>   s     z*_simple_json_normalize.<locals>.<listcomp>)r   r   r9   list)r'   r   Znormalised_json_objectZnormalised_json_listr   r:   r   r;      s    +

r;   raisezstr | list | Nonez"str | list[str | list[str]] | Nonez
str | Noner   )	r,   record_pathmetameta_prefixrecord_prefixerrorsr   r   r   c                   s  ddddddfddddd	d
fddt | trD| sDt S t | trV| g} n$t | tjrvt | tsvt| } nt|dkr|dkr|dkr	dkrdkrtt| dS |dkrt	dd | D rt
| d} t| S t |ts|g}|dkrg }nt |ts|g}dd |D  g 
g ttfdd D d  
f
dd	| |i dd t
}	dk	r|j	fddd} D ]N\}	}
|dk	r||	 }	|	|krtd|	 dtj|
td||	< q|S )!a  
    Normalize semi-structured JSON data into a flat table.

    Parameters
    ----------
    data : dict or list of dicts
        Unserialized JSON objects.
    record_path : str or list of str, 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 (str or list of str), default None
        Fields to use as metadata for each record in resulting table.
    meta_prefix : str, default None
        If True, prefix records with dotted (?) path, e.g. foo.bar.field if
        meta is ['foo', 'bar'].
    record_prefix : str, 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'
        Configures error handling.

        * '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.
    sep : str, default '.'
        Nested records will generate names separated by sep.
        e.g., for sep='.', {'foo': {'bar': 0}} -> foo.bar.
    max_level : int, default None
        Max number of levels(depth of dict) to normalize.
        if None, normalizes all levels.

        .. versionadded:: 0.25.0

    Returns
    -------
    frame : DataFrame
    Normalize semi-structured JSON data into a flat table.

    Examples
    --------
    >>> data = [
    ...     {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
    ...     {"name": {"given": "Mark", "family": "Regner"}},
    ...     {"id": 2, "name": "Faye Raker"},
    ... ]
    >>> pd.json_normalize(data)
        id name.first name.last name.given name.family        name
    0  1.0     Coleen      Volk        NaN         NaN         NaN
    1  NaN        NaN       NaN       Mark      Regner         NaN
    2  2.0        NaN       NaN        NaN         NaN  Faye Raker

    >>> data = [
    ...     {
    ...         "id": 1,
    ...         "name": "Cole Volk",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
    ...     {
    ...         "id": 2,
    ...         "name": "Faye Raker",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ... ]
    >>> pd.json_normalize(data, max_level=0)
        id        name                        fitness
    0  1.0   Cole Volk  {'height': 130, 'weight': 60}
    1  NaN    Mark Reg  {'height': 130, 'weight': 60}
    2  2.0  Faye Raker  {'height': 130, 'weight': 60}

    Normalizes nested data up to level 1.

    >>> data = [
    ...     {
    ...         "id": 1,
    ...         "name": "Cole Volk",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
    ...     {
    ...         "id": 2,
    ...         "name": "Faye Raker",
    ...         "fitness": {"height": 130, "weight": 60},
    ...     },
    ... ]
    >>> pd.json_normalize(data, max_level=1)
        id        name  fitness.height  fitness.weight
    0  1.0   Cole Volk             130              60
    1  NaN    Mark Reg             130              60
    2  2.0  Faye Raker             130              60

    >>> 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 = pd.json_normalize(
    ...     data, "counties", ["state", "shortname", ["info", "governor"]]
    ... )
    >>> result
             name  population    state shortname info.governor
    0        Dade       12345   Florida    FL    Rick Scott
    1     Broward       40000   Florida    FL    Rick Scott
    2  Palm Beach       60000   Florida    FL    Rick Scott
    3      Summit        1234   Ohio       OH    John Kasich
    4    Cuyahoga        1337   Ohio       OH    John Kasich

    >>> data = {"A": [1, 2]}
    >>> pd.json_normalize(data, "A", record_prefix="Prefix.")
        Prefix.0
    0          1
    1          2

    Returns normalized data with columns prefixed with the given string.
    Fr+   z
list | strboolzScalar | Iterable)jsspecextract_recordr   c              
     s   | }z:t |tr4|D ]}|dkr(t||| }qn|| }W nh tk
r } zJ|rhtd| d|n. dkr~tj W Y S td| d| d|W 5 d}~X Y nX |S )zInternal function to pull fieldNzKey zS not found. If specifying a record_path, all elements of data should have the path.ignorez) not found. To replace missing values of z% with np.nan, pass in errors='ignore')r   r>   KeyErrornpnan)rF   rG   rH   resultfielde)rD   r   r   _pull_field  s.    

z$_json_normalize.<locals>._pull_fieldr>   )rF   rG   r   c                   sF    | |dd}t |tsBt|r(g }nt|  d| d| d|S )z
        Internal function to pull field for records, and similar to
        _pull_field, but require to return list. And will raise error
        if has non iterable value.
        T)rH   z has non list value z
 for path z. Must be list or null.)r   r>   pdZisnull	TypeError)rF   rG   rM   )rP   r   r   _pull_records  s    

z&_json_normalize.<locals>._pull_recordsNr:   c                 s  s    | ]}d d |  D V  qdS )c                 S  s   g | ]}t |tqS r   r5   )r7   xr   r   r   r=     s     z-_json_normalize.<locals>.<genexpr>.<listcomp>N)values)r7   yr   r   r   	<genexpr>  s     z"_json_normalize.<locals>.<genexpr>r   r   c                 S  s    g | ]}t |tr|n|gqS r   )r   r>   )r7   mr   r   r   r=     s     z#_json_normalize.<locals>.<listcomp>c                   s   g | ]}  |qS r   )join)r7   valr:   r   r   r=     s     r   c           	        s  t | tr| g} t|dkr| D ]^}t D ]*\}}|d t|kr.||d ||< q.||d  |dd  ||d d q n| D ]}||d }	fdd|D }t| t D ]B\}}|d t|kr|| }n|||d  }| | qĈ| qd S )Nr   r   r   r   c                   s(   g | ] }t |tr t| d n|qS )rX   )r   r   r%   )r7   r)r   r   r   r   r=     s   z?_json_normalize.<locals>._recursive_extract.<locals>.<listcomp>)r   r   r2   zipr&   extend)	r,   pathZ	seen_metar   objr[   r3   ZrecsZmeta_val)
_metarP   rS   _recursive_extractlengthsr   	meta_keys	meta_valsrecordsr   r   r   rc     s(    
(
z+_json_normalize.<locals>._recursive_extractr\   c                   s     |  S )Nr   )rT   )rC   r   r   <lambda>      z!_json_normalize.<locals>.<lambda>)columnszConflicting metadata name z, need distinguishing prefix )Zdtype)F)r   )r   r>   r   r   r   r   r   NotImplementedErrorr;   anyr%   r   renamer"   
ValueErrorrK   arrayobjectrepeat)r,   r@   rA   rB   rC   rD   r   r   rM   r)   r*   r   )rb   rP   rS   rc   rD   rd   r   re   rf   rC   rg   r   r   _json_normalize   sj      



 




rr   zpandas.io.json.json_normalizez1.0.0zpandas.json_normalize)r   r   r   N)r   )NNNNr?   r   N)
__future__r   collectionsr   r   r    typingr   r   r   ZnumpyrK   Zpandas._libs.writersr	   Zpandas._typingr
   Zpandas.util._decoratorsr   ZpandasrQ   r   r   r%   r1   r9   r;   rr   Zjson_normalizer   r   r   r   <module>   sF       S) 7          ,   