Skip to content

Commit b204d09

Browse files
committed
parametrize tests and allow index parameter to handle MultiIndex and index name
1 parent 1a3e41a commit b204d09

File tree

3 files changed

+43
-61
lines changed

3 files changed

+43
-61
lines changed

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1673,9 +1673,9 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
16731673
.. versionadded:: 0.21.0
16741674
16751675
index : boolean, default True
1676-
Whether to include the index values in the JSON string. A
1677-
ValueError will be thrown if index is False when orient is not
1678-
'split' or 'table'.
1676+
Whether to include the index values in the JSON string. Not
1677+
including the index (``index=False``) is only supported when
1678+
orient is 'split' or 'table'.
16791679
16801680
.. versionadded:: 0.22.0
16811681

pandas/io/json/json.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,17 @@ def __init__(self, obj, orient, date_format, double_precision,
200200
if is_period_dtype(obj.index):
201201
obj.index = obj.index.to_timestamp()
202202

203-
self.obj = obj.reset_index()
203+
# exclude index from obj if index=False
204+
if not self.index:
205+
self.obj = obj.reset_index(drop=True)
206+
else:
207+
self.obj = obj.reset_index(drop=False)
204208
self.date_format = 'iso'
205209
self.orient = 'records'
206210
self.index = index
207211

208212
def _write(self, obj, orient, double_precision, ensure_ascii,
209213
date_unit, iso_dates, default_handler):
210-
if not self.index:
211-
obj = obj.drop('index', axis=1)
212214
data = super(JSONTableWriter, self)._write(obj, orient,
213215
double_precision,
214216
ensure_ascii, date_unit,

pandas/tests/io/json/test_pandas.py

+35-55
Original file line numberDiff line numberDiff line change
@@ -1149,80 +1149,60 @@ def test_data_frame_size_after_to_json(self):
11491149

11501150
assert size_before == size_after
11511151

1152-
def test_index_false_to_json(self):
1152+
@pytest.mark.parametrize('data, expected', [
1153+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b']),
1154+
{'columns': ['a', 'b'], 'data': [[1, 2], [4, 5]]}),
1155+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b']).rename_axis('foo'),
1156+
{'columns': ['a', 'b'], 'data': [[1, 2], [4, 5]]}),
1157+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b'],
1158+
index=[['a', 'b'], ['c', 'd']]),
1159+
{'columns': ['a', 'b'], 'data': [[1, 2], [4, 5]]}),
1160+
(Series([1, 2, 3], name='A'),
1161+
{'name': 'A', 'data': [1, 2, 3]}),
1162+
(Series([1, 2, 3], name='A').rename_axis('foo'),
1163+
{'name': 'A', 'data': [1, 2, 3]}),
1164+
(Series([1, 2], name='A', index=[['a', 'b'], ['c', 'd']]),
1165+
{'name': 'A', 'data': [1, 2]}),
1166+
])
1167+
def test_index_false_to_json_split(self, data, expected):
11531168
# GH 17394
1154-
# Testing index parameter in to_json
1155-
1156-
# Testing DataFrame.to_json(orient='split', index=False)
1157-
df = pd.DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])
1158-
1159-
result = df.to_json(orient='split', index=False)
1160-
result = json.loads(result)
1161-
1162-
expected = {
1163-
'columns': ['a', 'b'],
1164-
'data': [[1, 2], [4, 5]]
1165-
}
1166-
1167-
assert result == expected
1169+
# Testing index=False in to_json with orient='split'
11681170

1169-
# Testing DataFrame.to_json(orient='table', index=False)
1170-
result = df.to_json(orient='table', index=False)
1171+
result = data.to_json(orient='split', index=False)
11711172
result = json.loads(result)
11721173

1173-
schema = {
1174-
'fields': [{'name': 'a', 'type': 'integer'},
1175-
{'name': 'b', 'type': 'integer'}],
1176-
'pandas_version': '0.20.0'
1177-
}
1178-
1179-
expected = {
1180-
'schema': schema,
1181-
'data': [{'a': 1, 'b': 2}, {'a': 4, 'b': 5}]
1182-
}
1183-
11841174
assert result == expected
11851175

1186-
# Testing Series.to_json(orient='split', index=False)
1187-
s = pd.Series([1, 2, 3], name='A')
1188-
1189-
result = s.to_json(orient='split', index=False)
1190-
result = json.loads(result)
1191-
1192-
expected = {
1193-
'name': 'A',
1194-
'data': [1, 2, 3]
1195-
}
1196-
1197-
assert result == expected
1176+
@pytest.mark.parametrize('data', [
1177+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])),
1178+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b']).rename_axis('foo')),
1179+
(DataFrame([[1, 2], [4, 5]], columns=['a', 'b'],
1180+
index=[['a', 'b'], ['c', 'd']])),
1181+
(Series([1, 2, 3], name='A')),
1182+
(Series([1, 2, 3], name='A').rename_axis('foo')),
1183+
(Series([1, 2], name='A', index=[['a', 'b'], ['c', 'd']])),
1184+
])
1185+
def test_index_false_to_json_table(self, data):
1186+
# GH 17394
1187+
# Testing index=False in to_json with orient='table'
11981188

1199-
# Testing Series.to_json(orient='table', index=False)
1200-
result = s.to_json(orient='table', index=False)
1189+
result = data.to_json(orient='table', index=False)
12011190
result = json.loads(result)
12021191

1203-
fields = [{'name': 'A', 'type': 'integer'}]
1204-
1205-
schema = {
1206-
'fields': fields,
1207-
'pandas_version': '0.20.0'
1208-
}
1209-
12101192
expected = {
1211-
'schema': schema,
1212-
'data': [{'A': 1}, {'A': 2}, {'A': 3}]
1193+
'schema': pd.io.json.build_table_schema(data, index=False),
1194+
'data': DataFrame(data).to_dict(orient='records')
12131195
}
12141196

12151197
assert result == expected
12161198

12171199
@pytest.mark.parametrize('orient', [
1218-
('records'),
1219-
('index'),
1220-
('columns'),
1221-
('values'),
1200+
'records', 'index', 'columns', 'values'
12221201
])
12231202
def test_index_false_error_to_json(self, orient):
12241203
# GH 17394
12251204
# Testing error message from to_json with index=False
1205+
12261206
df = pd.DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])
12271207

12281208
with tm.assert_raises_regex(ValueError, "'index=False' is only "

0 commit comments

Comments
 (0)