4
4
import operator
5
5
import weakref
6
6
import gc
7
+ import json
7
8
8
9
import numpy as np
9
10
import pandas .lib as lib
@@ -129,6 +130,37 @@ def __init__(self, data, axes=None, copy=False, dtype=None,
129
130
object .__setattr__ (self , '_data' , data )
130
131
object .__setattr__ (self , '_item_cache' , {})
131
132
133
+ def _ipython_display_ (self ):
134
+ try :
135
+ from IPython .display import display
136
+ except ImportError :
137
+ return None
138
+
139
+ # Series doesn't define _repr_html_ or _repr_latex_
140
+ latex = self ._repr_latex_ () if hasattr (self , '_repr_latex_' ) else None
141
+ html = self ._repr_html_ () if hasattr (self , '_repr_html_' ) else None
142
+ table_schema = self ._repr_table_schema_ ()
143
+ # We need the inital newline since we aren't going through the
144
+ # usual __repr__. See
145
+ # https://github.com/pandas-dev/pandas/pull/14904#issuecomment-277829277
146
+ text = "\n " + repr (self )
147
+
148
+ reprs = {"text/plain" : text , "text/html" : html , "text/latex" : latex ,
149
+ "application/vnd.dataresource+json" : table_schema }
150
+ reprs = {k : v for k , v in reprs .items () if v }
151
+ display (reprs , raw = True )
152
+
153
+ def _repr_table_schema_ (self ):
154
+ """
155
+ Not a real Jupyter special repr method, but we use the same
156
+ naming convention.
157
+ """
158
+ if config .get_option ("display.html.table_schema" ):
159
+ data = self .head (config .get_option ('display.max_rows' ))
160
+ payload = json .loads (data .to_json (orient = 'table' ),
161
+ object_pairs_hook = collections .OrderedDict )
162
+ return payload
163
+
132
164
def _validate_dtype (self , dtype ):
133
165
""" validate the passed dtype """
134
166
@@ -1094,10 +1126,9 @@ def __setstate__(self, state):
1094
1126
strings before writing.
1095
1127
"""
1096
1128
1097
- def to_json (self , path_or_buf = None , orient = None , date_format = 'epoch' ,
1098
- timedelta_format = 'epoch' , double_precision = 10 ,
1099
- force_ascii = True , date_unit = 'ms' , default_handler = None ,
1100
- lines = False ):
1129
+ def to_json (self , path_or_buf = None , orient = None , date_format = None ,
1130
+ double_precision = 10 , force_ascii = True , date_unit = 'ms' ,
1131
+ default_handler = None , lines = False ):
1101
1132
"""
1102
1133
Convert the object to a JSON string.
1103
1134
@@ -1131,17 +1162,16 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1131
1162
- columns : dict like {column -> {index -> value}}
1132
1163
- values : just the values array
1133
1164
- table : dict like {'schema': {schema}, 'data': {data}}
1134
- the schema component is a `Table Schema_`
1135
1165
describing the data, and the data component is
1136
1166
like ``orient='records'``.
1137
1167
1138
1168
.. versionchanged:: 0.20.0
1139
1169
1140
- date_format : {'epoch', 'iso'}
1170
+ date_format : {None, 'epoch', 'iso'}
1141
1171
Type of date conversion. `epoch` = epoch milliseconds,
1142
- `iso` = ISO8601. Default is epoch, except when orient is
1143
- table_schema, in which case this parameter is ignored
1144
- and iso formatting is always used .
1172
+ `iso` = ISO8601. The default depends on the ` orient`. For
1173
+ `orient='table'`, the default is `'iso'`. For all other orients,
1174
+ the default is `'epoch'` .
1145
1175
double_precision : The number of decimal places to use when encoding
1146
1176
floating point values, default 10.
1147
1177
force_ascii : force encoded string to be ASCII, default True.
@@ -1160,6 +1190,7 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1160
1190
1161
1191
.. versionadded:: 0.19.0
1162
1192
1193
+ .. _Table Schema: http://specs.frictionlessdata.io/json-table-schema/
1163
1194
1164
1195
Returns
1165
1196
-------
@@ -1204,6 +1235,10 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1204
1235
"""
1205
1236
1206
1237
from pandas .io import json
1238
+ if date_format is None and orient == 'table' :
1239
+ date_format = 'iso'
1240
+ elif date_format is None :
1241
+ date_format = 'epoch'
1207
1242
return json .to_json (path_or_buf = path_or_buf , obj = self , orient = orient ,
1208
1243
date_format = date_format ,
1209
1244
double_precision = double_precision ,
0 commit comments