-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Add Indent Support in to_json #28130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bb9c174
4c84106
b99f42b
367b494
1bcf354
43ab17b
592be66
5c0e8a3
d11e47f
21672ed
79c1cbc
0007e34
abdd27f
2da6fbf
a4f740a
cd0c9e6
c740359
2b5cb50
638d055
b870585
ba7f044
df589e3
517377b
4aec9d7
1aa424d
c896b8a
b046061
ae93309
f037d05
ccb9823
7d757e4
95251b1
65315c3
9827a94
0b440e0
0024f41
4869425
f03f05f
b894b8c
c4dba2e
dc68364
6da8684
dab8df1
b679fee
966fadb
c8efda6
5067eb7
f376f12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import re | ||
from textwrap import dedent | ||
from typing import ( | ||
Any, | ||
Callable, | ||
Dict, | ||
FrozenSet, | ||
|
@@ -61,7 +62,7 @@ | |
from pandas.core.dtypes.missing import isna, notna | ||
|
||
import pandas as pd | ||
from pandas._typing import Dtype, FilePathOrBuffer | ||
from pandas._typing import Dtype, FilePathOrBuffer, Scalar | ||
from pandas.core import missing, nanops | ||
import pandas.core.algorithms as algos | ||
from pandas.core.base import PandasObject, SelectionMixin | ||
|
@@ -2249,17 +2250,18 @@ def to_excel( | |
|
||
def to_json( | ||
self, | ||
path_or_buf=None, | ||
orient=None, | ||
date_format=None, | ||
double_precision=10, | ||
force_ascii=True, | ||
date_unit="ms", | ||
default_handler=None, | ||
lines=False, | ||
compression="infer", | ||
index=True, | ||
): | ||
path_or_buf: Optional[FilePathOrBuffer] = None, | ||
orient: Optional[str] = None, | ||
date_format: Optional[str] = None, | ||
double_precision: int = 10, | ||
force_ascii: bool_t = True, | ||
date_unit: str = "ms", | ||
default_handler: Optional[Callable[[Any], Union[Scalar, List, Dict]]] = None, | ||
lines: bool_t = False, | ||
compression: Optional[str] = "infer", | ||
index: bool_t = True, | ||
indent: Optional[int] = None, | ||
) -> Optional[str]: | ||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Convert the object to a JSON string. | ||
|
||
|
@@ -2339,6 +2341,11 @@ def to_json( | |
|
||
.. versionadded:: 0.23.0 | ||
|
||
indent : integer, optional | ||
Length of whitespace used to indent each record. | ||
|
||
.. versionadded:: 1.0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this match the semantics of the stdlib, e.g. default is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this only accepts int for now and 0 does not insert new lines. This matches ujson behavior instead of stdlib There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be possible to support this in the long term I just think will take a little bit of effort to bridge the gap with our vendored ujson. Thinking for now it might make the most sense to change the signature to I think that should avoid a deprecation cycle in the future for |
||
|
||
Returns | ||
------- | ||
None or str | ||
|
@@ -2349,6 +2356,13 @@ def to_json( | |
-------- | ||
read_json | ||
|
||
Notes | ||
----- | ||
The behavior of ``indent=0`` varies from the stdlib, which does not | ||
indent the output but does insert newlines. Currently, ``indent=0`` | ||
and the default ``indent=None`` are equivalent in pandas, though this | ||
may change in a future release. | ||
|
||
Examples | ||
-------- | ||
|
||
|
@@ -2399,6 +2413,10 @@ def to_json( | |
date_format = "iso" | ||
elif date_format is None: | ||
date_format = "epoch" | ||
|
||
config.is_nonnegative_int(indent) | ||
indent = indent or 0 | ||
|
||
return json.to_json( | ||
path_or_buf=path_or_buf, | ||
obj=self, | ||
|
@@ -2411,6 +2429,7 @@ def to_json( | |
lines=lines, | ||
compression=compression, | ||
index=index, | ||
indent=indent, | ||
) | ||
|
||
def to_hdf(self, path_or_buf, key, **kwargs): | ||
|
Uh oh!
There was an error while loading. Please reload this page.