maxframe.dataframe.Series.to_json#

Series.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,日期时间对象将被转换为 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}} 的字典 描述数据,其中 data 部分类似 orient='records'

  • date_format ({None, 'epoch', 'iso'}) -- 日期转换类型。'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'])