Skip to content

Commit 11c90be

Browse files
committed
EHN: Add index parameter to to_json (pandas-dev#17394)
1 parent 7627cca commit 11c90be

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

json_test.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pandas as pd
2+
3+
s = pd.Series([1, 2, 3], name='A')
4+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['a', 'b', 'c'])
5+
6+
# orient='split'
7+
# print(df.to_json(orient='split', index=False))
8+
# print(df.to_json(orient='split', index=True))
9+
10+
print(s.to_json(orient='split', index=False))
11+
print(s.to_json(orient='split', index=True))
12+
13+
# orient='table'
14+
print(df.to_json(orient='table', index=False))
15+
print(df.to_json(orient='table', index=True))
16+
# print(df.to_json(orient='records', index=False))
17+
# print(df.to_json(orient='records', index=True))
18+
19+
print(s.to_json(orient='table', index=False))
20+
print(s.to_json(orient='table', index=True))
21+
22+
23+
# Errors
24+
# print(df.to_json(orient='records', index=False))
25+
# print(df.to_json(orient='index', index=False))
26+
# print(df.to_json(orient='columns', index=False))
27+
# print(df.to_json(orient='values', index=False))

pandas/core/generic.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ def _repr_latex_(self):
12951295

12961296
def to_json(self, path_or_buf=None, orient=None, date_format=None,
12971297
double_precision=10, force_ascii=True, date_unit='ms',
1298-
default_handler=None, lines=False, compression=None):
1298+
default_handler=None, lines=False, compression=None,
1299+
index=True):
12991300
"""
13001301
Convert the object to a JSON string.
13011302
@@ -1415,7 +1416,8 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
14151416
double_precision=double_precision,
14161417
force_ascii=force_ascii, date_unit=date_unit,
14171418
default_handler=default_handler,
1418-
lines=lines, compression=compression)
1419+
lines=lines, compression=compression,
1420+
index=index)
14191421

14201422
def to_hdf(self, path_or_buf, key, **kwargs):
14211423
"""Write the contained data to an HDF5 file using HDFStore.

pandas/io/json/json.py

+31-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
# interface to/from
2929
def to_json(path_or_buf, obj, orient=None, date_format='epoch',
3030
double_precision=10, force_ascii=True, date_unit='ms',
31-
default_handler=None, lines=False, compression=None):
31+
default_handler=None, lines=False, compression=None,
32+
index=True):
33+
34+
if not index and orient not in ['split', 'table']:
35+
raise ValueError("'index=False' is only valid when 'orient' is "
36+
"'split' or 'table'")
3237

3338
path_or_buf = _stringify_path(path_or_buf)
3439
if lines and orient != 'records':
@@ -49,7 +54,8 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
4954
s = writer(
5055
obj, orient=orient, date_format=date_format,
5156
double_precision=double_precision, ensure_ascii=force_ascii,
52-
date_unit=date_unit, default_handler=default_handler).write()
57+
date_unit=date_unit, default_handler=default_handler,
58+
index=index).write()
5359

5460
if lines:
5561
s = _convert_to_line_delimits(s)
@@ -69,7 +75,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
6975
class Writer(object):
7076

7177
def __init__(self, obj, orient, date_format, double_precision,
72-
ensure_ascii, date_unit, default_handler=None):
78+
ensure_ascii, date_unit, index, default_handler=None):
7379
self.obj = obj
7480

7581
if orient is None:
@@ -81,6 +87,7 @@ def __init__(self, obj, orient, date_format, double_precision,
8187
self.ensure_ascii = ensure_ascii
8288
self.date_unit = date_unit
8389
self.default_handler = default_handler
90+
self.index = index
8491

8592
self.is_copy = None
8693
self._format_axes()
@@ -89,6 +96,24 @@ def _format_axes(self):
8996
raise AbstractMethodError(self)
9097

9198
def write(self):
99+
if not self.index and self.orient == 'split':
100+
if isinstance(self.obj, DataFrame):
101+
obj_dict = self.obj.to_dict(orient='split')
102+
del obj_dict["index"]
103+
elif isinstance(self.obj, Series):
104+
obj_dict = {"name":self.obj.name, "data":self.obj.values}
105+
return dumps(
106+
obj_dict,
107+
orient=self.orient,
108+
double_precision=self.double_precision,
109+
ensure_ascii=self.ensure_ascii,
110+
date_unit=self.date_unit,
111+
iso_dates=self.date_format == 'iso',
112+
default_handler=self.default_handler
113+
)
114+
if not self.index and self.orient == 'records':
115+
print(self.obj)
116+
self.obj = self.obj.drop('index', axis=1, errors='ignore')
92117
return dumps(
93118
self.obj,
94119
orient=self.orient,
@@ -128,7 +153,7 @@ class JSONTableWriter(FrameWriter):
128153
_default_orient = 'records'
129154

130155
def __init__(self, obj, orient, date_format, double_precision,
131-
ensure_ascii, date_unit, default_handler=None):
156+
ensure_ascii, date_unit, index, default_handler=None):
132157
"""
133158
Adds a `schema` attribut with the Table Schema, resets
134159
the index (can't do in caller, because the schema inference needs
@@ -137,7 +162,7 @@ def __init__(self, obj, orient, date_format, double_precision,
137162
"""
138163
super(JSONTableWriter, self).__init__(
139164
obj, orient, date_format, double_precision, ensure_ascii,
140-
date_unit, default_handler=default_handler)
165+
date_unit, index, default_handler=default_handler)
141166

142167
if date_format != 'iso':
143168
msg = ("Trying to write with `orient='table'` and "
@@ -146,7 +171,7 @@ def __init__(self, obj, orient, date_format, double_precision,
146171
.format(fmt=date_format))
147172
raise ValueError(msg)
148173

149-
self.schema = build_table_schema(obj)
174+
self.schema = build_table_schema(obj, index=self.index)
150175

151176
# NotImplementd on a column MultiIndex
152177
if obj.ndim == 2 and isinstance(obj.columns, MultiIndex):

pandas/tests/io/json/test_pandas.py

+37
Original file line numberDiff line numberDiff line change
@@ -1147,3 +1147,40 @@ def test_data_frame_size_after_to_json(self):
11471147
size_after = df.memory_usage(index=True, deep=True).sum()
11481148

11491149
assert size_before == size_after
1150+
1151+
def test_index_false_to_json(self):
1152+
1153+
df = pd.DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])
1154+
1155+
result = df.to_json(orient='split', index=False)
1156+
expected = '{"columns":["a","b"],"data":[[1,2],[4,5]]}'
1157+
assert result == expected
1158+
1159+
result = df.to_json(orient='table', index=False)
1160+
expected = '{"schema": {"fields":[{"name":"a","type":"integer"},{"name":"b","type":"integer"}],"pandas_version":"0.20.0"}, "data": [{"a":1,"b":2},{"a":4,"b":5}]}'
1161+
assert result == expected
1162+
1163+
s = pd.Series([1, 2, 3], name='A')
1164+
1165+
result = s.to_json(orient='split', index=False)
1166+
expected = '{"name":"A","data":[1,2,3]}'
1167+
assert result == expected
1168+
1169+
result = s.to_json(orient='table', index=False)
1170+
expected = '{"schema": {"fields":[{"name":"A","type":"integer"}],"pandas_version":"0.20.0"}, "data": [{"A":1},{"A":2},{"A":3}]}'
1171+
assert result == expected
1172+
1173+
@pytest.mark.parametrize('orient', [
1174+
('records'),
1175+
('index'),
1176+
('columns'),
1177+
('values'),
1178+
])
1179+
def test_index_false_error_to_json(self, orient):
1180+
1181+
df = pd.DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])
1182+
1183+
with tm.assert_raises_regex(ValueError, "'index=False' is only "
1184+
"valid when 'orient' is "
1185+
"'split' or 'table'"):
1186+
df.to_json(orient=orient, index=False)

0 commit comments

Comments
 (0)