
    Pi                       d dl mZ d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZ d dlZd dlZd dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlm Z m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZBmCZCmDZD d dlEmFZF e	r.d dlGmHZHmIZImJZJ d dlKmLZL d dlMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZW d dlXmYZY  eded                   ZZe	 	 	 	 	 	 	 	 	 	 	 	 ddd;            Z[e	 	 	 	 	 	 	 	 	 	 	 	 ddd<            Z[	 	 	 	 	 	 	 	 	 	 	 	 dddFZ[ G dG dHe          Z\ G dI dJe\          Z] G dK dLe\          Z^ G dM dNe^          Z_edddddddddddddddddOddd            Z`eddddddddddddddddeddh            Z`edddddddddddddddddiddk            Z`eddddddddddddddddddlddn            Z` e#do          ddpddd?d?dAdddqdAddBddeja        drdlddu            Z` e#dv           G dw dxejb        eeZ                               Zc G dy dz          Zd G d{ d|ed          Ze G d} d~ed          ZfddZgdS )    )annotations)ABCabstractmethod)abc)islice)TYPE_CHECKINGAnyGenericLiteralSelfTypeVarfinaloverloadN)option_context)lib)ujson_dumpsujson_loads)iNaT)import_optional_dependency)AbstractMethodErrorOutOfBoundsDatetime)
set_module)check_dtype_backend)
ensure_stris_string_dtypepandas_dtype)PeriodDtype)
ArrowDtype	DataFrameIndex
MultiIndexSeriesisnanotnato_datetime)concat)arrow_table_to_pandas)	IOHandlesdedup_names
get_handleis_potential_multi_indexstringify_path)convert_to_line_delimits)build_table_schemaparse_table_schemaset_default_names)validate_integer)CallableHashableMapping)TracebackType)
CompressionOptionsDtypeArgDtypeBackendFilePath
IndexLabel
JSONEngineJSONSerializable
ReadBufferStorageOptionsWriteBuffer)NDFrameFrameSeriesStrT)frameseries)bound.path_or_buf0FilePath | WriteBuffer[str] | WriteBuffer[bytes]objr@   orient
str | Nonedate_formatstrdouble_precisionintforce_asciibool	date_unitdefault_handler(Callable[[Any], JSONSerializable] | Nonelinescompressionr6   indexbool | Noneindentstorage_optionsr>   modeLiteral['a', 'w']returnNonec                    d S N rE   rG   rH   rJ   rL   rN   rP   rQ   rS   rT   rU   rW   rX   rY   s                 h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/io/json/_json.pyto_jsonrb   e   s	      3    c                    d S r^   r_   r`   s                 ra   rb   rb   x   s	      #rc   epoch
   TmsFinferw7FilePath | WriteBuffer[str] | WriteBuffer[bytes] | NoneStorageOptions | Nonec                   |dv r|
du rt          d          |dv r|
du rt          d          |
d}
|r|dk    rt          d          |d	vrd
| d}t          |          |dk    r|r|dk    rd}t          |          |dk    r2t          |t                    r|                    |j        pd          }|dk    r5|dvrt          d| d          t          |t
                    r[d}t          j        |j        	                    d                     d         }t          |          rZ|                    d          }d}|D ]?}|                    ||j        d d |f         j                            |                     @|j        j        j        dv rK|s|                    d          }d}t          |j                  j                            |          |_        |j        j        j        dv rK|s|                    d          }d}t          |j                  j                            |          |_        nt          |t                    r|j        j        dv r0|                    d          }|j                            |          }|j        j        j        dv rG|                    d          }t          |j                  j                            |          |_        |dk    rt          |t
                    rt(          }nIt          |t                    rt*          }n,t          |t
                    rt,          }nt/          d           |||||||||
|	  	                                        }|rt3          |          }| Ft5          | ||	|          5 }|j                            |           d d d            n# 1 swxY w Y   n|S d S )N)recordsvaluesTzT'index=True' is only valid when 'orient' is 'split', 'table', 'index', or 'columns'.rU   columnsFzV'index=False' is only valid when 'orient' is 'split', 'table', 'records', or 'values'.rm   z3'lines' keyword only valid when 'orient' is records)ari   zmode=z@ is not a valid option.Only 'w' and 'a' are currently supported.rq   zNmode='a' (append) is only supported when lines is True and orient is 'records'tablern   )namere   srg   usnszInvalid value 'z' for option 'date_unit'c                    | j         dv S )N)Mm)kind)dts    ra   <lambda>zto_json.<locals>.<lambda>   s    :8M rc   r   deepMmz''obj' should be a Series or a DataFrame)rH   rJ   rL   ensure_asciirP   rQ   rU   rW   )rT   rX   )
ValueError
isinstancer"   to_framers   r   npnonzerodtypesmaplencopyisetitemilocr|   as_unitrU   dtyper{   rp   JSONTableWriterSeriesWriterFrameWriterNotImplementedErrorwriter-   r*   handle)rE   rG   rH   rJ   rL   rN   rP   rQ   rS   rT   rU   rW   rX   rY   msgcopiedcolscolwriterru   handless                        ra   rb   rb      s     &&&5D==%
 
 	
 
'	'	'EUNN&
 
 	
 
 P9$$NOOO:8D 8 8 8 	 oos{{E{Vy%8%84 	 ooZV44ll 4Hl55g 333RyRRRSSSc9%% 	DF:cjnn-M-MNNOOPQRD4yy NhhEh** N NCLLchqqq#v&6&9&A&A)&L&LMMMMy#t++ "(((..C!F"39--088CC	{ %-- "(((..C!F$S[114<<YGGV$$ 	Dy~%%hhEh**fnnY//y#t++hhEh**"39--088CC	 ZY77 	C	 	  M	C	#	# M!"KLLL) '
	 
	 
	 egg   ($Q'';
 
 
 	$N  ###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
 4s   .OOOc                  `    e Zd ZU ded<   	 	 dddZddZddZeedd                        Z	dS )WriterrK   _default_orientNr   rG   r@   rH   rI   rJ   rL   rM   r   rO   rP   rU   rQ   rR   rW   r[   r\   c
                    || _         || j        }|| _        || _        || _        || _        || _        || _        || _        |	| _	        | 
                                 d S r^   )rG   r   rH   rJ   rL   r   rP   rQ   rU   rW   _format_axes)
selfrG   rH   rJ   rL   r   rP   rU   rQ   rW   s
             ra   __init__zWriter.__init__  sl     >)F& 0(".
rc   c                     t          |           r^   r   r   s    ra   r   zWriter._format_axes      !$'''rc   c           
         | j         dk    }t          | j        | j        | j        | j        | j        || j        | j                  S )Niso)rH   rL   r   rP   	iso_datesrQ   rW   )	rJ   r   obj_to_writerH   rL   r   rP   rQ   rW   )r   r   s     ra   r   zWriter.write"  sP    $-	;!2*n 0;	
 	
 	
 		
rc   "NDFrame | Mapping[IndexLabel, Any]c                    dS )zObject to write in JSON format.Nr_   r   s    ra   r   zWriter.obj_to_write/  s      rc   Nr   )rG   r@   rH   rI   rJ   rK   rL   rM   r   rO   rP   rK   rU   rO   rQ   rR   rW   rM   r[   r\   r[   r\   r[   rK   r[   r   )
__name__
__module____qualname____annotations__r   r   r   propertyr   r   r_   rc   ra   r   r     s          EI    6( ( ( (
 
 
 
 . . . ^ X. . .rc   r   c                  2    e Zd ZdZedd            Zd	dZdS )
r   rU   r[   r   c                f    | j         s$| j        dk    r| j        j        | j        j        dS | j        S )Nsplit)rs   data)rU   rH   rG   rs   rn   r   s    ra   r   zSeriesWriter.obj_to_write8  s5    z 	dkW44 HM48?CCC8Orc   r\   c                r    | j         j        j        s#| j        dk    rt	          d| j         d          d S d S )NrU   z(Series index must be unique for orient='')rG   rU   	is_uniquerH   r   r   s    ra   r   zSeriesWriter._format_axes?  sL    x~' 	XDK7,B,BVVVVWWW	X 	X,B,Brc   Nr   r   r   r   r   r   r   r   r   r_   rc   ra   r   r   5  sQ        O   XX X X X X Xrc   r   c                  2    e Zd ZdZedd            Zd	dZdS )
r   rp   r[   r   c                v    | j         s*| j        dk    r| j                            d          }|d= n| j        }|S )Nr   )rH   rU   )rU   rH   rG   to_dict)r   r   s     ra   r   zFrameWriter.obj_to_writeG  sH    z 	$dkW448++7+;;LW%%8Lrc   r\   c                    | j         j        j        s!| j        dv rt	          d| j         d          | j         j        j        s!| j        dv rt	          d| j         d          dS dS )z:
        Try to format axes if they are datelike.
        ro   z+DataFrame index must be unique for orient='z'.)rU   rp   rm   z-DataFrame columns must be unique for orient='N)rG   rU   r   rH   r   rp   r   s    ra   r   zFrameWriter._format_axesP  s     x~' 	DK;O,O,OMdkMMM   x) 	dk >
 /
 /

 OOOO  	 	 /
 /
rc   Nr   r   r   r_   rc   ra   r   r   D  sK        O   X     rc   r   c                  B     e Zd ZdZ	 	 dd fdZedd            Z xZS )r   rm   Nr   rH   rI   rJ   rK   rL   rM   r   rO   rP   rU   rQ   rR   rW   r[   r\   c
                J   t                                          |||||||||		  	         |dk    rd| d}
t          |
          t          || j                  | _        | j        rt          |          }|j        dk    r)t          |j	        t                    rt          d          |j        dk    r |j        t          |j        j                  v s1t          |j	                            |j        j                            rd	}
t          |
          |                    d
g          j	        }d}t          |          r5|                                }d}||                             d           ||<   | j        s|                    d          | _        npt          |j        j        t.                    r6|s|                    d          }|j                                        |_        |                    d          | _        d| _        d| _        || _        dS )z
        Adds a `schema` attribute with the Table Schema, resets
        the index (can't do in caller, because the schema inference needs
        to know what the index is, forces orient to records, and forces
        date_format to 'iso'.
        )rQ   rW   r   z8Trying to write with `orient='table'` and `date_format='zH'`. Table Schema requires dates to be formatted with `date_format='iso'`)rU      z6orient='table' is not supported for MultiIndex columns   z/Overlapping names between the index and columns	timedelta)includeFTc                *    |                                  S r^   )	isoformat)xs    ra   r}   z*JSONTableWriter.__init__.<locals>.<lambda>  s    AKKMM rc   )dropr~   rm   N)superr   r   r.   rU   schemar0   ndimr   rp   r!   r   rs   setnamesr   intersectionselect_dtypesr   r   reset_indexrG   r   r   to_timestamprJ   rH   )r   rG   rH   rJ   rL   r   rP   rU   rQ   rW   r   
timedeltasr   	__class__s                ra   r   zJSONTableWriter.__init__e  s2   $ 	+ 	 
	
 
	
 
	
 %;!,; ; ; 
 S//!(DJ???: 	)#C((C 8q==ZZ@@=%H  
 X]]S-A-A!A!AsK$$SY_55H
 H
!A DCS//!&&}&==E
z?? 	K((**CF!*o112I2IJJC
O z 	3D11DHH #)/;77 5 /(((..CI2244	E22DH 


rc   r   c                     | j         | j        dS )N)r   r   )r   rG   r   s    ra   r   zJSONTableWriter.obj_to_write  s    +tx888rc   r   )rH   rI   rJ   rK   rL   rM   r   rO   rP   rK   rU   rO   rQ   rR   rW   rM   r[   r\   r   )r   r   r   r   r   r   r   __classcell__)r   s   @ra   r   r   b  sy        O EIJ J J J J J JX 9 9 9 X9 9 9 9 9rc   r   )rH   typr   convert_axesconvert_dateskeep_default_datesprecise_floatrP   encodingencoding_errorsrS   rT   nrowsrX   dtype_backendengine.FilePath | ReadBuffer[str] | ReadBuffer[bytes]r   Literal['frame']r   DtypeArg | Noner   r   bool | list[str]r   r   r   r   	chunksizer   
int | Noner   DtypeBackend | lib.NoDefaultr   r;   JsonReader[Literal['frame']]c                   d S r^   r_   rE   rH   r   r   r   r   r   r   rP   r   r   rS   r   rT   r   rX   r   r   s                     ra   	read_jsonr     s	    * $'3rc   )rH   r   r   r   r   r   rP   r   r   rS   rT   r   rX   r   r   Literal['series']JsonReader[Literal['series']]c                   d S r^   r_   r   s                     ra   r   r     s	    * %(Crc   )rH   r   r   r   r   r   rP   r   r   rS   r   rT   r   rX   r   r   r"   c                   d S r^   r_   r   s                     ra   r   r     s	    * Src   )rH   r   r   r   r   r   r   rP   r   r   rS   r   rT   r   rX   r   r   r   c                   d S r^   r_   r   s                     ra   r   r     s	    * rc   pandasrB   strictujsonLiteral['frame', 'series']DataFrame | Series | JsonReaderc               P   |dk    r|rt          d          |dk    r|rt          d          t          |           ||dk    rd}||dk    rd}t          | fi d|d|d|d	|d
|d|d|d|d|	d|d|d|d|d|d|
d|d|}|r|S |                                S )a*  
    Convert a JSON string to pandas object.

    This method reads JSON files or JSON-like data and converts them into pandas
    objects. It supports a variety of input formats, including line-delimited JSON,
    compressed files, and various data representations (table, records, index-based,
    etc.). When `chunksize` is specified, an iterator is returned instead of loading
    the entire data into memory.

    Parameters
    ----------
    path_or_buf : a str path, path object or file-like object
        Any valid string path is acceptable. The string could be a URL. Valid
        URL schemes include http, ftp, s3, and file. For file URLs, a host is
        expected. A local file could be:
        ``file://localhost/path/to/table.json``.

        If you want to pass in a path object, pandas accepts any
        ``os.PathLike``.

        By file-like object, we refer to objects with a ``read()`` method,
        such as a file handle (e.g. via builtin ``open`` function)
        or ``StringIO``.

    orient : str, optional
        Indication of expected JSON string format.
        Compatible JSON strings can be produced by ``to_json()`` with a
        corresponding orient value.
        The set of possible orients is:

        - ``'split'`` : dict like
          ``{{index -> [index], columns -> [columns], data -> [values]}}``
        - ``'records'`` : list like
          ``[{{column -> value}}, ... , {{column -> value}}]``
        - ``'index'`` : dict like ``{{index -> {{column -> value}}}}``
        - ``'columns'`` : dict like ``{{column -> {{index -> value}}}}``
        - ``'values'`` : just the values array
        - ``'table'`` : dict like ``{{'schema': {{schema}}, 'data': {{data}}}}``

        The allowed and default values depend on the value
        of the `typ` parameter.

        * when ``typ == 'series'``,

          - allowed orients are ``{{'split','records','index'}}``
          - default is ``'index'``
          - The Series index must be unique for orient ``'index'``.

        * when ``typ == 'frame'``,

          - allowed orients are ``{{'split','records','index',
            'columns','values', 'table'}}``
          - default is ``'columns'``
          - The DataFrame index must be unique for orients ``'index'`` and
            ``'columns'``.
          - The DataFrame columns must be unique for orients ``'index'``,
            ``'columns'``, and ``'records'``.

    typ : {{'frame', 'series'}}, default 'frame'
        The type of object to recover.

    dtype : bool or dict, default None
        If True, infer dtypes; if a dict of column to dtype, then use those;
        if False, then don't infer dtypes at all, applies only to the data.

        For all ``orient`` values except ``'table'``, default is True.

    convert_axes : bool, default None
        Try to convert the axes to the proper dtypes.

        For all ``orient`` values except ``'table'``, default is True.

    convert_dates : bool or list of str, default True
        If True then default datelike columns may be converted (depending on
        keep_default_dates).
        If False, no dates will be converted.
        If a list of column names, then those columns will be converted and
        default datelike columns may also be converted (depending on
        keep_default_dates).

    keep_default_dates : bool, default True
        If parsing dates (convert_dates is not False), then try to parse the
        default datelike columns.
        A column label is datelike if

        * it ends with ``'_at'``,

        * it ends with ``'_time'``,

        * it begins with ``'timestamp'``,

        * it is ``'modified'``, or

        * it is ``'date'``.

    precise_float : bool, default False
        Set to enable usage of higher precision (strtod) function when
        decoding string to double values. Default (False) is to use fast but
        less precise builtin functionality.

    date_unit : str, default None
        The timestamp unit to detect if converting dates. The default behaviour
        is to try and detect the correct precision, but if this is not desired
        then pass one of 's', 'ms', 'us' or 'ns' to force parsing only seconds,
        milliseconds, microseconds or nanoseconds respectively.

    encoding : str, default is 'utf-8'
        The encoding to use to decode py3 bytes.

    encoding_errors : str, optional, default "strict"
        How encoding errors are treated. `List of possible values
        <https://docs.python.org/3/library/codecs.html#error-handlers>`_ .

    lines : bool, default False
        Read the file as a json object per line.

    chunksize : int, optional
        Return JsonReader object for iteration.
        See the `line-delimited json docs
        <https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#line-delimited-json>`_
        for more information on ``chunksize``.
        This can only be passed if `lines=True`.
        If this is None, the file will be read into memory all at once.

    compression : str or dict, default 'infer'
        For on-the-fly decompression of on-disk data. If 'infer' and 'path_or_buf' is
        path-like, then detect compression from the following extensions: '.gz',
        '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2'
        (otherwise no compression).
        If using 'zip' or 'tar', the ZIP file must contain only one data file to be
        read in.
        Set to ``None`` for no decompression.
        Can also be a dict with key ``'method'`` set
        to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``, ``'tar'``}
        and other key-value pairs are forwarded to
        ``zipfile.ZipFile``, ``gzip.GzipFile``,
        ``bz2.BZ2File``, ``zstandard.ZstdDecompressor``, ``lzma.LZMAFile`` or
        ``tarfile.TarFile``, respectively.
        As an example, the following could be passed for Zstandard decompression using a
        custom compression dictionary:
        ``compression={'method': 'zstd', 'dict_data': my_compression_dict}``.

    nrows : int, optional
        The number of lines from the line-delimited jsonfile that has to be read.
        This can only be passed if `lines=True`.
        If this is None, all the rows will be returned.

    storage_options : dict, optional
        Extra options that make sense for a particular storage connection, e.g.
        host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
        are forwarded to ``urllib.request.Request`` as header options. For other
        URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
        forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
        details, and for more examples on storage options refer `here
        <https://pandas.pydata.org/docs/user_guide/io.html?
        highlight=storage_options#reading-writing-remote-files>`_.

    dtype_backend : {{'numpy_nullable', 'pyarrow'}}
        Back-end data type applied to the resultant :class:`DataFrame`
        (still experimental). If not specified, the default behavior
        is to not use nullable data types. If specified, the behavior
        is as follows:

        * ``"numpy_nullable"``: returns nullable-dtype-backed :class:`DataFrame`
        * ``"pyarrow"``: returns pyarrow-backed nullable
          :class:`ArrowDtype` :class:`DataFrame`

        .. versionadded:: 2.0

    engine : {{"ujson", "pyarrow"}}, default "ujson"
        Parser engine to use. The ``"pyarrow"`` engine is only available when
        ``lines=True``.

        .. versionadded:: 2.0

    Returns
    -------
    Series, DataFrame, or pandas.api.typing.JsonReader
        A JsonReader is returned when ``chunksize`` is not ``0`` or ``None``.
        Otherwise, the type returned depends on the value of ``typ``.

    See Also
    --------
    DataFrame.to_json : Convert a DataFrame to a JSON string.
    Series.to_json : Convert a Series to a JSON string.
    json_normalize : Normalize semi-structured JSON data into a flat table.

    Notes
    -----
    Specific to ``orient='table'``, if a :class:`DataFrame` with a literal
    :class:`Index` name of `index` gets written with :func:`to_json`, the
    subsequent read operation will incorrectly set the :class:`Index` name to
    ``None``. This is because `index` is also used by :func:`DataFrame.to_json`
    to denote a missing :class:`Index` name, and the subsequent
    :func:`read_json` operation cannot distinguish between the two. The same
    limitation is encountered with a :class:`MultiIndex` and any names
    beginning with ``'level_'``.

    Examples
    --------
    >>> from io import StringIO
    >>> df = pd.DataFrame(
    ...     [["a", "b"], ["c", "d"]],
    ...     index=["row 1", "row 2"],
    ...     columns=["col 1", "col 2"],
    ... )

    Encoding/decoding a Dataframe using ``'split'`` formatted JSON:

    >>> df.to_json(orient="split")
    '{"columns":["col 1","col 2"],"index":["row 1","row 2"],"data":[["a","b"],["c","d"]]}'

    >>> pd.read_json(StringIO(_), orient="split")  # noqa: F821
          col 1 col 2
    row 1     a     b
    row 2     c     d

    Encoding/decoding a Dataframe using ``'index'`` formatted JSON:

    >>> df.to_json(orient="index")
    '{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'

    >>> pd.read_json(StringIO(_), orient="index")  # noqa: F821
          col 1 col 2
    row 1     a     b
    row 2     c     d

    Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
    Note that index labels are not preserved with this encoding.

    >>> df.to_json(orient="records")
    '[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'

    >>> pd.read_json(StringIO(_), orient="records")  # noqa: F821
      col 1 col 2
    0     a     b
    1     c     d

    Encoding with Table Schema

    >>> df.to_json(orient="table")
    '{"schema":{"fields":[{"name":"index","type":"string","extDtype":"str"},{"name":"col 1","type":"string","extDtype":"str"},{"name":"col 2","type":"string","extDtype":"str"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":"row 1","col 1":"a","col 2":"b"},{"index":"row 2","col 1":"c","col 2":"d"}]}'

    The following example uses ``dtype_backend="numpy_nullable"``

    >>> data = '''{"index": {"0": 0, "1": 1},
    ...        "a": {"0": 1, "1": null},
    ...        "b": {"0": 2.5, "1": 4.5},
    ...        "c": {"0": true, "1": false},
    ...        "d": {"0": "a", "1": "b"},
    ...        "e": {"0": 1577.2, "1": 1577.1}}'''
    >>> pd.read_json(StringIO(data), dtype_backend="numpy_nullable")
       index     a    b      c  d       e
    0      0     1  2.5   True  a  1577.2
    1      1  <NA>  4.5  False  b  1577.1
    rr   z)cannot pass both dtype and orient='table'z0cannot pass both convert_axes and orient='table'NTrH   r   r   r   r   r   r   rP   r   rS   r   rT   r   rX   r   r   r   )r   r   
JsonReaderread)rE   rH   r   r   r   r   r   r   rP   r   r   rS   r   rT   r   rX   r   r   json_readers                      ra   r   r     s   l UDEEE\KLLL&&&}7** ' 1 1  v C e	
 "\ $m .- $m )  e )  K e (  (!" $m#$ v%K*  "!!!rc   zpandas.api.typingc                     e Zd ZdZddej        dfd>dZd Zd?dZe	d@d#            Z
e	dAd&            Z
e	dBd)            Z
dCd*Z
dDd+ZdCd,ZdEd.ZdFd/ZdGd1Ze	d@d2            Ze	dAd3            Ze	dBd4            ZdCd5ZdGd6ZdHd=ZdS )Ir   z
    JsonReader provides an interface for reading in a JSON file.

    If initialized with ``lines=True`` and ``chunksize``, can be iterated over
    ``chunksize`` lines at a time. Otherwise, calling ``read`` reads in the
    whole document.
    Nr   r   r   rA   r   rV   r   rO   r   rS   r   r   rT   r6   r   rX   rk   r   rI   r   r   r   r;   r[   r\   c                   || _         || _        || _        || _        || _        || _        || _        |	| _        |
| _        || _	        || _
        || _        || _        || _        d| _        || _        || _        d | _        || _        | j	        dvrt'          d| j	         d          | j        Kt)          d| j        d          | _        | j        st'          d          | j	        dk    rt'          d	          | j        1t)          d
| j        d          | _        | j        st'          d          | j	        dk    r| j        st'          d          || _        d S | j	        dk    rb|                     |          }| j        s=| j        s6| 5  |                                | _        d d d            d S # 1 swxY w Y   d S || _        d S d S )Nr   >   r   pyarrowzThe engine type z is currently not supported.r   r   z*chunksize can only be passed if lines=Truer   z<currently pyarrow engine doesn't support chunksize parameterr   z&nrows can only be passed if lines=TruezEcurrently pyarrow engine only supports the line-delimited JSON formatr   )rH   r   r   r   r   r   r   rP   r   r   rT   rX   rS   r   
nrows_seenr   r   r   r   r   r1   r   _get_data_from_filepathr   )r   filepath_or_bufferrH   r   r   r   r   r   r   rP   r   rS   r   rT   r   rX   r   r   r   r   s                       ra   r   zJsonReader.__init__b  sT   * 
(*"4*" &.
"
..2*;222L4;LLL   >%-k4>1MMDN: O !MNNN{i'' R   :!)'4:qAADJ: K !IJJJ;)##:  5   +DIII[G##//0BCCD N !dj ! , , $		DI, , , , , , , , , , , , , , , , , , !			 $#s   F22F69F6c                    t          |          }	 t          |d| j        | j        | j        | j                  | _        n&# t          $ r}t          d| d          |d}~ww xY w| j        j	        }|S )z
        The function read_json accepts three input types:
            1. filepath (string-like)
            2. file-like object (e.g. open file object, StringIO)
        r)r   rT   rX   errorszFile z does not existN)
r,   r*   r   rT   rX   r   r   OSErrorFileNotFoundErrorr   )r   r   errs      ra   r   z"JsonReader._get_data_from_filepath  s     ,,>??	%" , $ 4+  DLL  	 	 	#;*;;; 	 "\0!!s   .A   
A#
AA#rK   c                \    dd                     d d |D             D                        dS )zG
        Combines a list of JSON objects into one JSON object.
        [,c                    g | ]}||S r_   r_   .0lines     ra   
<listcomp>z-JsonReader._combine_lines.<locals>.<listcomp>  s    TTT4tT$TTTrc   c              3  >   K   | ]}|                                 V  d S r^   )stripr  s     ra   	<genexpr>z,JsonReader._combine_lines.<locals>.<genexpr>  s*      +K+KTDJJLL+K+K+K+K+K+Krc   ])join)r   rS   s     ra   _combine_lineszJsonReader._combine_lines  sB    
 YTT+K+KU+K+K+KTTTUUXXX	
rc   r   r   r   c                    d S r^   r_   r   s    ra   r   zJsonReader.read  s    ?Bsrc   r   r"   c                    d S r^   r_   r   s    ra   r   zJsonReader.read  s    =@Src   &JsonReader[Literal['frame', 'series']]DataFrame | Seriesc                    d S r^   r_   r   s    ra   r   zJsonReader.read  s    RURUrc   c                    | 5  | j         dk    r|                                 }n| j         dk    r|                                 }ddd           n# 1 swxY w Y   |S )zA
        Read the whole JSON input into a pandas object.
        r   r   N)r   _read_pyarrow_read_ujsonr   rG   s     ra   r   zJsonReader.read  s    
  	) 	){i''((**''&&((		) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 
s   A AAAc                   t          d          }d}t          | j        t                    rt          d          }g }| j                                        D ]E\  }}t          |          }t          |t                    r|                    ||j        f           F|	                    |          }|
                    |d          }|                    | j        |          }	t          |	| j                  }
|
S )z5
        Read JSON using the pyarrow engine.
        zpyarrow.jsonNr   rh   )explicit_schemaunexpected_field_behavior)parse_options)r   )r   r   r   dictitemsr   r   appendpyarrow_dtyper   ParseOptionsr   r   r'   r   )r   pyarrow_jsonoptionspafieldsfieldr   pd_dtyper   pa_tabledfs              ra   r  zJsonReader._read_pyarrow  s     2.AAdj$'' 	+I66BF $
 0 0 2 2 C Cu'..h
33 CMM5(*@"ABBBYYv&&F"// &' 0  G  ))$)7)KK"84;MNNN	rc   c                ~   | j         r| j        rt          |           }n| j        rRt	          t          | j        | j                            }|                     |          }|                     |          }nlt          | j                  }|
                    d          }|                     |                     |                    }n|                     | j                  }| j        t          j        urFt          dd          5  |                    d| j                  cddd           S # 1 swxY w Y   dS |S )z3
        Read JSON using the ujson engine.
        
future.distinguish_nan_and_naFinfer_objectsr   N)rS   r   r&   r   listr   r   r  _get_object_parserr   r   r   r   
no_defaultr   convert_dtypes)r   rG   rS   
lines_jsonr   
data_liness         ra   r  zJsonReader._read_ujson  sx   
 : 	5~ 	OTll OVDItz::;;!0077
--j99!$),,!ZZ--
--d.A.A*.M.MNN))$)44CS^33 ?GG  ))"'t7I *                   
 Js   D00D47D4jsonc           	     r   | j         }| j        }| j        | j        | j        | j        | j        | j        | j        | j        d}|dk    rt          |fi |
                                S |dk    r9t          |t                    s||d<   t          |fi |
                                S t          d|d          )z>
        Parses a json document into a pandas object.
        )rH   r   r   r   r   r   rP   r   rB   rC   r   ztyp=z must be 'frame' or 'series'.)r   r   rH   r   r   r   r   rP   r   FrameParserparser   rO   SeriesParserr   )r   r:  r   r   kwargss        ra   r5  zJsonReader._get_object_parser  s     h
kZ -!/"&"9!/!/	
 	
 '>>t..v..44666H__eT** ("'w////55777CCCCDDDrc   c                J    | j         | j                                          dS dS )z
        If we opened a stream earlier, in _get_data_from_filepath, we should
        close it.

        If an open stream or file was passed, we leave it open.
        N)r   closer   s    ra   rA  zJsonReader.close-  s.     <#L      $#rc   r   c                    | S r^   r_   r   s    ra   __iter__zJsonReader.__iter__7      rc   c                    d S r^   r_   r   s    ra   __next__zJsonReader.__next__:  s    CF3rc   c                    d S r^   r_   r   s    ra   rF  zJsonReader.__next__=  s    ADrc   c                    d S r^   r_   r   s    ra   rF  zJsonReader.__next__@  s	     !Src   c                   | j         r+| j        | j         k    r|                                  t          t	          t          | j        | j                            }|s|                                  t          	 |                     |          }| 	                    |          }t          | j        | j        t          |          z             |_        | xj        t          |          z  c_        n(# t          $ r}|                                  |d }~ww xY w| j        t          j        urFt#          dd          5  |                    d| j                  cd d d            S # 1 swxY w Y   d S |S )Nr1  Fr2  )r   r   rA  StopIterationr4  r   r   r   r  r5  ranger   rU   	Exceptionr   r   r6  r   r7  )r   rS   r8  rG   exs        ra   rF  zJsonReader.__next__E  s   : 	 $/TZ77JJLLLVDIt~6677 	 JJLLL		,,U33J))*55C dotS/IJJCIOOs3xx'OOO 	 	 	JJLLLH	 S^33 ?GG  ))"'t7I *                   
 Js+   8A6C/ /
D9DD:E##E'*E'c                    | S r^   r_   r   s    ra   	__enter__zJsonReader.__enter__b  rD  rc   exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                .    |                                   d S r^   )rA  )r   rP  rR  rT  s       ra   __exit__zJsonReader.__exit__e  s     	

rc   )r   rA   r   rV   r   rO   r   rO   rS   rO   r   r   rT   r6   r   r   rX   rk   r   rI   r   r   r   r;   r[   r\   r   )r   r   r[   r   )r   r   r[   r"   )r   r  r[   r  r[   r  r[   r   )r:  rK   r[   r  r   )r[   r   )rP  rQ  rR  rS  rT  rU  r[   r\   )r   r   r   __doc__r   r6  r   r   r  r   r   r  r  r5  rA  rC  rF  rO  rW  r_   rc   ra   r   r   X  s        0 26&.69n$'H! H! H! H! H!T" " ".
 
 
 
 BBB XB@@@ X@UUU XU      2   4E E E E2! ! ! !    FFF XFDDD XD! ! ! X!   :        rc   r   c                      e Zd ZU ded<   ded<   dZdddd	dZded
<   ddddddej        fd-dZe	d.d            Z
e	d/d            Zd/d Ze	d0d"            Zd# Ze		 	 	 d1d2d+            Ze	d3d,            ZdS )4Parserztuple[str, ...]_split_keysrK   r   rt   i3l    ,b/ l    `'rl     F[L'r:  NTFr   r   r   rO   r   r   r   r   r   r   r[   r\   c
                L   || _         || j        }|| _        || _        |G|                                }|| j        vrt          d| j                   | j        |         | _        n| j        d         | _        || _	        || _
        || _        || _        || _        |	| _        d S )Nzdate_unit must be one of ru   )r:  r   rH   r   lower_STAMP_UNITSr   _MIN_STAMPS	min_stampr   r   r   rP   r   r   )
r   r:  rH   r   r   r   r   r   rP   r   s
             ra   r   zParser.__init__{  s     	>)F
 !))I 111 !PT=N!P!PQQQ!-i8DNN!-c2DN*(*""4*rc   decodedr"  c                    t          |                                                              t          | j                            }|r'd                    |          }t          d|           dS )zT
        Checks that dict has only the appropriate keys for orient='split'.
        z, z!JSON data had unexpected key(s): N)r   keys
differencer]  r  r   )r   rc  bad_keysbad_keys_joineds       ra   check_keys_splitzParser.check_keys_split  so    
 w||~~&&11#d6F2G2GHH 	T"ii11ORRRSSS	T 	Trc   r  c                    |                                  }| j        r|                     |          }|                     |          }|S r^   )_parser   _convert_axes_try_convert_typesr  s     ra   r=  zParser.parse  sE    kkmm 	*$$S))C%%c**
rc   c                     t          |           r^   r   r   s    ra   rk  zParser._parse  r   rc   rG   c                    |j         D ]u}|                    |          }t          ||j        d          }|                     ||ddd          \  }}|r(t          ||j        d          }t          |||           v|S )z&
        Try to convert axes.
        F)r   r   T)rs   r   
use_dtypesr   is_axis)_AXIS_ORDERS	_get_axisr"   r   _try_convert_datar    setattr)r   rG   	axis_nameaxsernew_serresultnew_axiss           ra   rl  zParser._convert_axes  s    
 ) 	2 	2Iy))B28%888C"44 " 5  OGV  2 EJJJY111
rc   c                     t          |           r^   r   r  s     ra   rm  zParser._try_convert_types  r   rc   rs   r3   r   r"   rp  rq  tuple[Series, bool]c                   |}|r| j         sCt          t          |                    r|dfS |                    t          j                  }|dfS | j         du rnt          || j        |          spt          | j         t                    r| j         
                    |          n| j         }|3	 |                    |          dfS # t          t          f$ r |dfcY S w xY w|r|                     |          }	|	|ur|	dfS d}
| j        t           j        ur|s|dfS t%          |j                   r0	 |                    d          }d}
n# t          t          f$ r Y nw xY w|j         j        dk    r;|j         dk    r0	 |                    d          }d}
n# t          t          f$ r Y nw xY wt)          |          rY|j         dv rP	 |                    d          }	|	|k                                    r|	}d}
n# t          t          t*          f$ r Y nw xY w|j         dk    r;|j         dk    r0	 |                    d          }d}
n# t          t          f$ r Y nw xY w|d	k    rt)          |          r| j        d
k    r|dfS ||
fS )zI
        Try to parse a Series into a column by inferring dtype.
        FTNfloat64f)floatobjectint64rM   rU   r   )r   allr$   fillnar   nan_should_convert_datesr   r   r"  getastype	TypeErrorr   _try_convert_to_dater   r   r6  r   r{   r   OverflowErrorrH   )r   rs   r   rp  r   rq  org_datafilledr   new_data	converteds              ra   rt  zParser._try_convert_data  s"     	+: +uT{{## ';&RV,,t|#t##*t6  +
 -7tz4,H,HXDJNN4(((dj  $+#{{511477%z2 + + +#U{***+  	&0066Ht##~%	S^33G3:TZ(( 	{{9-- 		z*    :?c!!djI&=&={{9-- 		z*    t99 	':::#??733$))++ %#D $Iz=9    :4:#8#8{{7++ 		z*    7??s4yy?{g%%U{"YsZ   -C CC.E EE9F F%$F%1G3 3HH'H? ?IIc                   t          |          s|S |}|j        dk    s|j        dk    r;	 |                    d          }n$# t          $ r |cY S t          t
          f$ r Y nw xY wt          |j        j        t          j	                  rGt          |j                  || j        k    z  |j        t          k    z  }|                                s|S |j        dk    r|t          j                    5  t          j        dt$                     dD ]2}	 t'          |d|          c cddd           S # t(          $ r Y /w xY w	 ddd           n# 1 swxY w Y   nq| j        r| j        fn| j        }|D ]X}	 t'          |d|	          }|j                            d
          } n(# t2          $ r Y =t
          t          t          f$ r Y Uw xY w|S )z
        Try to parse an ndarray like into a date column.

        Try to coerce object in epoch/iso formats and integer/float in epoch
        formats.
        r  stringr  ignore)Niso8601mixedraise)r  formatN)r  unitrw   )r   r   r  r  r  r   
issubclasstyper   numberr#   _valuesrb  r   r  warningscatch_warningssimplefilterUserWarningr%   rL  rP   r`  r|   r   r   )r   r   r  in_ranger  
date_unitsrP   _s           ra   r  zParser._try_convert_to_date%  s    4yy 	K>X%%8)C)C;;w//    z*    hn)2955 	X%&&dn,.#t+- 
 <<>> >X%%(**   %h<<<8  F*8GFSSSSS        %   	               /3nS$.**$BSJ'  	
 'xiPPPD--AE*   H"M9=   Dse   A A"A"!A".ED.E.
D;8E:D;;EEE2,F  
G,GG)r:  rK   r   r   r   rO   r   r   r   rO   r   rO   r   r   r[   r\   )rc  r"  r[   r\   rX  )rG   r  r[   r  )TTF)rs   r3   r   r"   rp  rO   r   r   rq  rO   r[   r}  )r   r"   r[   r"   )r   r   r   r   r`  ra  r   r6  r   r   ri  r=  rk  rl  rm  rt  r  r_   rc   ra   r\  r\  n  sf            *L	 K III "&!*.#(#69n"+ "+ "+ "+ "+H T T T UT    U( ( ( (    U&( ( ( 
  *.V V V V UVp 9 9 9 U9 9 9rc   r\  c                  &    e Zd ZdZdZd	dZd
dZdS )r>  rU   )rs   rU   r   r[   r"   c                    t          | j        | j                  }| j        dk    r?d |                                D             }|                     |           t          di |S t          |          S )Nr   r   c                4    i | ]\  }}t          |          |S r_   rK   r  kvs      ra   
<dictcomp>z'SeriesParser._parse.<locals>.<dictcomp>j  s$    :::TQs1vvq:::rc   r_   )r   r:  r   rH   r#  ri  r"   )r   r   rc  s      ra   rk  zSeriesParser._parsef  su    49D4FGGG;'!!::TZZ\\:::G!!'***$$G$$$$<<rc   rG   c                F    |                      d|| j                  \  }}|S )Nr   r   )rt  r   )r   rG   r  s      ra   rm  zSeriesParser._try_convert_typesp  s(    ''4CU'VVQ
rc   N)r[   r"   )rG   r"   r[   r"   r   r   r   r   r]  rk  rm  r_   rc   ra   r>  r>  b  sF        O+K            rc   r>  c                  &    e Zd ZdZdZd	dZd
dZdS )r<  rp   )rp   rU   r   r[   r   c                   | j         }| j        }|dk    rd t          || j                                                  D             }|                     |           d |d         D             }t          |t          |d                     |d<   t          ddd i|S |dk    r+t          j	        t          || j                  d d          S |d	k    rt          || j                  S t          t          || j                  d 
          S )Nr   c                4    i | ]\  }}t          |          |S r_   r  r  s      ra   r  z&FrameParser._parse.<locals>.<dictcomp>~  s4       Aq A  rc   r  c                Z    g | ](}t          |t                    rt          |          n|)S r_   )r   r4  tuple)r  r   s     ra   r  z&FrameParser._parse.<locals>.<listcomp>  sA         *#t44=s#  rc   rp   r   rU   )r   rH   rr   )r   r_   )r:  rH   r   r   r#  ri  r)   r+   r   	from_dictr/   )r   r:  rH   rc  
orig_namess        ra   rk  zFrameParser._parsey  s^   yW 'D<NOOOUUWW  G !!'*** "9-  J "-(T::" "GI 33437333w&D0BCCC   
 w%d$:LMMMM D0BCCC4   rc   rG   c           
        g }|                                 D ]T\  }}|                     ||t          | j        | j        |                    \  }}|                    |j                   Ut          j        ||j	        |j
        d          S )N)r   r   r  F)verify_integrity)r#  rt  r  r   r   r$  arrayr   _from_arraysrp   rU   )r   rG   arrays	col_labelrC   rz  r  s          ra   rm  zFrameParser._try_convert_types  s    !$ 
	( 
	(Iv..3&'+'>!   /  IFA MM&,''''%CKU
 
 
 	
rc   NrY  )rG   r   r[   r   r  r_   rc   ra   r<  r<  u  sG        O.K   B
 
 
 
 
 
rc   r<  r   r3   c                    | du rdS t          | t                    s|t          |           v rdS |sdS t          |t                    sdS |                                }|                    d          s|dv s|                    d          rdS dS )zL
    Return bool whether a DataFrame column should be cast to datetime.
    FT)_at_time>   datedatetimemodified	timestamp)r   rO   r   rK   r_  endswith
startswith)r   r   r   	col_lowers       ra   r  r    s     ut,, M8J8J1J1Jt uS!! u		I+,,888,, 9 t5rc   )............)rE   rF   rG   r@   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rK   rQ   rR   rS   rO   rT   r6   rU   rV   rW   rM   rX   r>   rY   rZ   r[   r\   )rE   r\   rG   r@   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rK   rQ   rR   rS   rO   rT   r6   rU   rV   rW   rM   rX   r>   rY   rZ   r[   rK   )Nre   rf   Trg   NFrh   Nr   Nri   )rE   rj   rG   r@   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rK   rQ   rR   rS   rO   rT   r6   rU   rV   rW   rM   rX   rk   rY   rZ   r[   rI   )&rE   r   rH   rI   r   r   r   r   r   rV   r   r   r   rO   r   rO   rP   rI   r   rI   r   rI   rS   rO   r   rM   rT   r6   r   r   rX   r>   r   r   r   r;   r[   r   )&rE   r   rH   rI   r   r   r   r   r   rV   r   r   r   rO   r   rO   rP   rI   r   rI   r   rI   rS   rO   r   rM   rT   r6   r   r   rX   r>   r   r   r   r;   r[   r   )&rE   r   rH   rI   r   r   r   r   r   rV   r   r   r   rO   r   rO   rP   rI   r   rI   r   rI   rS   rO   r   r\   rT   r6   r   r   rX   r>   r   r   r   r;   r[   r"   )&rE   r   rH   rI   r   r   r   r   r   rV   r   r   r   rO   r   rO   rP   rI   r   rI   r   rI   rS   rO   r   r\   rT   r6   r   r   rX   r>   r   r   r   r;   r[   r   )&rE   r   rH   rI   r   r   r   r   r   rV   r   r   r   rO   r   rO   rP   rI   r   rI   r   rI   rS   rO   r   r   rT   r6   r   r   rX   rk   r   r   r   r;   r[   r   )r   r   r   rO   r   r3   r[   rO   )h
__future__r   r   r   r   collections	itertoolsr   typingr   r	   r
   r   r   r   r   r   r  numpyr   pandas._configr   pandas._libsr   pandas._libs.jsonr   r   pandas._libs.tslibsr   pandas.compat._optionalr   pandas.errorsr   r   pandas.util._decoratorsr   pandas.util._validatorsr   pandas.core.dtypes.commonr   r   r   pandas.core.dtypes.dtypesr   r   r   r   r    r!   r"   r#   r$   r%   pandas.core.reshape.concatr&   pandas.io._utilr'   pandas.io.commonr(   r)   r*   r+   r,   pandas.io.json._normalizer-   pandas.io.json._table_schemar.   r/   r0   pandas.io.parsers.readersr1   collections.abcr2   r3   r4   typesr5   pandas._typingr6   r7   r8   r9   r:   r;   r<   r=   r>   r?   pandas.core.genericr@   rA   rb   r   r   r   r   r   r6  Iteratorr   r\  r>  r<  r  r_   rc   ra   <module>r     su   " " " " " "                   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      ) ) ) ) ) )              % $ $ $ $ $ > > > > > >        / . . . . . 7 7 7 7 7 7         
 2 1 1 1 1 1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 . - - - - - 1 1 1 1 1 1              ? > > > > >         
 7 6 6 6 6 6 ,         
 $#####                        ,+++++'+7;L3MNNN 
 @C&)&)!    
$ 
 @C&)&)!    
* @D&--1!s s s s sl1. 1. 1. 1. 1.S 1. 1. 1.hX X X X X6 X X X    &   <Q9 Q9 Q9 Q9 Q9k Q9 Q9 Q9h 
   #&)""%&)&)25'' ' ' ' ' 
'. 
   #&)""%&)&)25'( ( ( ( ( 
(. 
   #&)""%&)&)25'     
. 
   #&)""%&)&)25'     
. H &-! $&*# "* &--125. '~" ~" ~" ~" ~" ~"B
   R R R R Rw7 R R ! Rjq q q q q q q qh    6   &4
 4
 4
 4
 4
& 4
 4
 4
n     rc   