28
28
# interface to/from
29
29
def to_json (path_or_buf , obj , orient = None , date_format = 'epoch' ,
30
30
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'" )
32
37
33
38
path_or_buf = _stringify_path (path_or_buf )
34
39
if lines and orient != 'records' :
@@ -49,7 +54,8 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
49
54
s = writer (
50
55
obj , orient = orient , date_format = date_format ,
51
56
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 ()
53
59
54
60
if lines :
55
61
s = _convert_to_line_delimits (s )
@@ -69,7 +75,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
69
75
class Writer (object ):
70
76
71
77
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 ):
73
79
self .obj = obj
74
80
75
81
if orient is None :
@@ -81,6 +87,7 @@ def __init__(self, obj, orient, date_format, double_precision,
81
87
self .ensure_ascii = ensure_ascii
82
88
self .date_unit = date_unit
83
89
self .default_handler = default_handler
90
+ self .index = index
84
91
85
92
self .is_copy = None
86
93
self ._format_axes ()
@@ -89,6 +96,24 @@ def _format_axes(self):
89
96
raise AbstractMethodError (self )
90
97
91
98
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' )
92
117
return dumps (
93
118
self .obj ,
94
119
orient = self .orient ,
@@ -128,7 +153,7 @@ class JSONTableWriter(FrameWriter):
128
153
_default_orient = 'records'
129
154
130
155
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 ):
132
157
"""
133
158
Adds a `schema` attribut with the Table Schema, resets
134
159
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,
137
162
"""
138
163
super (JSONTableWriter , self ).__init__ (
139
164
obj , orient , date_format , double_precision , ensure_ascii ,
140
- date_unit , default_handler = default_handler )
165
+ date_unit , index , default_handler = default_handler )
141
166
142
167
if date_format != 'iso' :
143
168
msg = ("Trying to write with `orient='table'` and "
@@ -146,7 +171,7 @@ def __init__(self, obj, orient, date_format, double_precision,
146
171
.format (fmt = date_format ))
147
172
raise ValueError (msg )
148
173
149
- self .schema = build_table_schema (obj )
174
+ self .schema = build_table_schema (obj , index = self . index )
150
175
151
176
# NotImplementd on a column MultiIndex
152
177
if obj .ndim == 2 and isinstance (obj .columns , MultiIndex ):
0 commit comments