Skip to content

Commit c867166

Browse files
committed
Use super() in dervived class and add comments to tests
1 parent 1e52760 commit c867166

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

pandas/io/json/json.py

+10-33
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
5555
obj, orient=orient, date_format=date_format,
5656
double_precision=double_precision, ensure_ascii=force_ascii,
5757
date_unit=date_unit, default_handler=default_handler,
58-
index=index).write()
58+
index=index)._write()
5959

6060
if lines:
6161
s = _convert_to_line_delimits(s)
@@ -95,7 +95,7 @@ def __init__(self, obj, orient, date_format, double_precision,
9595
def _format_axes(self):
9696
raise AbstractMethodError(self)
9797

98-
def write(self):
98+
def _write(self):
9999
return dumps(
100100
self.obj,
101101
orient=self.orient,
@@ -115,18 +115,10 @@ def _format_axes(self):
115115
raise ValueError("Series index must be unique for orient="
116116
"'{orient}'".format(orient=self.orient))
117117

118-
def write(self):
118+
def _write(self):
119119
if not self.index and self.orient == 'split':
120120
self.obj = {"name": self.obj.name, "data": self.obj.values}
121-
return dumps(
122-
self.obj,
123-
orient=self.orient,
124-
double_precision=self.double_precision,
125-
ensure_ascii=self.ensure_ascii,
126-
date_unit=self.date_unit,
127-
iso_dates=self.date_format == 'iso',
128-
default_handler=self.default_handler
129-
)
121+
return super(SeriesWriter, self)._write()
130122

131123

132124
class FrameWriter(Writer):
@@ -143,19 +135,11 @@ def _format_axes(self):
143135
raise ValueError("DataFrame columns must be unique for orient="
144136
"'{orient}'.".format(orient=self.orient))
145137

146-
def write(self):
138+
def _write(self):
147139
if not self.index and self.orient == 'split':
148140
self.obj = self.obj.to_dict(orient='split')
149141
del self.obj["index"]
150-
return dumps(
151-
self.obj,
152-
orient=self.orient,
153-
double_precision=self.double_precision,
154-
ensure_ascii=self.ensure_ascii,
155-
date_unit=self.date_unit,
156-
iso_dates=self.date_format == 'iso',
157-
default_handler=self.default_handler
158-
)
142+
return super(FrameWriter, self)._write()
159143

160144

161145
class JSONTableWriter(FrameWriter):
@@ -205,21 +189,14 @@ def __init__(self, obj, orient, date_format, double_precision,
205189
self.obj = obj.reset_index()
206190
self.date_format = 'iso'
207191
self.orient = 'records'
192+
self.index = index
208193

209-
def write(self):
194+
def _write(self):
210195
if not self.index:
211196
self.obj = self.obj.drop('index', axis=1)
212-
data = dumps(
213-
self.obj,
214-
orient=self.orient,
215-
double_precision=self.double_precision,
216-
ensure_ascii=self.ensure_ascii,
217-
date_unit=self.date_unit,
218-
iso_dates=self.date_format == 'iso',
219-
default_handler=self.default_handler
220-
)
197+
data = super(JSONTableWriter, self)._write()
221198
serialized = '{{"schema": {schema}, "data": {data}}}'.format(
222-
schema=dumps(self.schema), data=data)
199+
schema=dumps(self.schema), data=data)
223200
return serialized
224201

225202

pandas/tests/io/json/test_pandas.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
read_json, compat)
1010
from datetime import timedelta
1111
import pandas as pd
12+
import json
1213

1314
from pandas.util.testing import (assert_almost_equal, assert_frame_equal,
1415
assert_series_equal, network,
@@ -1151,7 +1152,8 @@ def test_data_frame_size_after_to_json(self):
11511152
def test_index_false_to_json(self):
11521153
# GH 17394
11531154
# Testing index parameter in to_json
1154-
import json
1155+
1156+
# Testing DataFrame.to_json(orient='split', index=False)
11551157
df = pd.DataFrame([[1, 2], [4, 5]], columns=['a', 'b'])
11561158

11571159
result = df.to_json(orient='split', index=False)
@@ -1164,6 +1166,7 @@ def test_index_false_to_json(self):
11641166

11651167
assert result == expected
11661168

1169+
# Testing DataFrame.to_json(orient='table', index=False)
11671170
result = df.to_json(orient='table', index=False)
11681171
result = json.loads(result)
11691172

@@ -1180,6 +1183,7 @@ def test_index_false_to_json(self):
11801183

11811184
assert result == expected
11821185

1186+
# Testing Series.to_json(orient='split', index=False)
11831187
s = pd.Series([1, 2, 3], name='A')
11841188

11851189
result = s.to_json(orient='split', index=False)
@@ -1192,6 +1196,7 @@ def test_index_false_to_json(self):
11921196

11931197
assert result == expected
11941198

1199+
# Testing Series.to_json(orient='table', index=False)
11951200
result = s.to_json(orient='table', index=False)
11961201
result = json.loads(result)
11971202

0 commit comments

Comments
 (0)