maxframe.dataframe.DataFrame.to_json#

DataFrame.to_json(path: str | None = None, orient: str | None = None, date_format: str | None = None, double_precision: int = 10, force_ascii: bool = True, date_unit: str | None = 'ms', default_handler: callable | None = None, lines: bool = False, compression: str | Dict[str, Any] | None = 'infer', index: bool | None = None, indent: int | None = None, storage_options: Dict[str, Any] | None = None, partition_cols: str | list | None = None, **kwargs)#

将对象转换为 JSON 字符串。

注意 NaN 和 None 将被转换为 null,datetime 对象将被转换为 UNIX 时间戳。

参数:
  • path (str, path object, file-like object, or None, default None) -- 字符串、路径对象(实现 os.PathLike[str])或实现 write() 函数的类文件对象。如果为 None,则结果以字符串形式返回。

  • orient (str) -- 指示预期的 JSON 字符串格式。 * Series: - 默认为 'index' - 允许的值为: {'split', 'records', 'index', 'table'}。 * DataFrame: - 默认为 'columns' - 允许的值为: {'split', 'records', 'index', 'columns', 'values', 'table'}。 * JSON 字符串的格式: - 'split' : 类似 {'index' -> [index], 'columns' -> [columns], 'data' -> [values]} 的字典 - 'records' : 类似 [{column -> value}, ... , {column -> value}] 的列表 - 'index' : 类似 {index -> {column -> value}} 的字典 - 'columns' : 类似 {column -> {index -> value}} 的字典 - 'values' : 仅值数组 - 'table' : 类似 {'schema': {schema}, 'data': {data}} 的字典 描述数据,其中数据组件类似于 orient='records'

  • date_format ({None, 'epoch', 'iso'}) -- 日期转换的类型。'epoch' = epoch 毫秒,'iso' = ISO8601。默认值取决于 orient。对于 orient='table',默认值为 'iso'。对于所有其他 orient,默认值为 'epoch'。

  • double_precision (int, default 10) -- 编码浮点数时使用的小数位数。

  • force_ascii (bool, default True) -- 强制编码后的字符串为 ASCII。

  • date_unit (str, default 'ms' (milliseconds)) -- 要编码到的时间单位,控制时间戳和 ISO8601 精度。分别为 's', 'ms', 'us', 'ns' 对应秒、毫秒、微秒和纳秒。

  • default_handler (callable, default None) -- 如果对象无法转换为适合 JSON 的格式时调用的处理程序。应接收一个参数(要转换的对象),并返回一个可序列化的对象。

  • lines (bool, default False) -- 如果 'orient' 为 'records',则输出行分隔的 JSON 格式。如果使用了不正确的 'orient' 将抛出 ValueError。

  • compression (str or dict, default 'infer') -- 对输出数据进行即时压缩。如果是字符串,则表示压缩模式。如果是字典,则 'method' 的值为压缩模式。压缩模式可以是以下任一可能值: {'infer', 'gzip', 'bz2', 'zip', 'xz', None}。如果压缩模式为 'infer' 且 path_or_buf 为路径类对象,则从以下扩展名: '.gz', '.bz2', '.zip' 或 '.xz' 推断压缩模式。(否则不压缩)。如果给出字典且模式为 {'zip', 'xz'} 之一,则其他项作为额外的压缩选项传递。

  • index (bool, default None) -- 是否在 JSON 字符串中包含索引值。不包含索引(index=False)仅在 orient 为 'split' 或 'table' 时受支持。

  • indent (int, optional) -- 用于缩进每条记录的空白字符长度。

  • partition_cols (list, optional, default None) -- 用于对数据集进行分区的列名。列按照给定的顺序进行分区。

参见

read_json

将 JSON 字符串转换为 pandas 对象。

备注

indent=0 的行为与标准库不同,标准库不会缩进输出但会插入换行符。目前在 pandas 中,indent=0 与默认值 indent=None 等效,但在未来版本中可能会发生变化。

orient='table' 时,'schema' 下包含一个 'pandas_version' 字段。该字段存储了在最新修订版 schema 中使用的 pandas 版本。

示例

>>> import maxframe.dataframe as md
>>> df = md.DataFrame([['a', 'b'], ['c', 'd']],
...                   index=['row 1', 'row 2'],
...                   columns=['col 1', 'col 2'])
>>> df.to_json('data.json')
>>> # Writing to a file with orient='records'
>>> df.to_json('records.json', orient='records')
>>> # Writing in line-delimited json format
>>> df.to_json('ldjson.json', orient='records', lines=True)
>>> # Write partitioned dataset
>>> df.to_json('dataset', partition_cols=['col 1'])