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
@@ -1088,10 +1120,9 @@ def __setstate__(self, state):
1088
1120
strings before writing.
1089
1121
"""
1090
1122
1091
- def to_json (self , path_or_buf = None , orient = None , date_format = 'epoch' ,
1092
- timedelta_format = 'epoch' , double_precision = 10 ,
1093
- force_ascii = True , date_unit = 'ms' , default_handler = None ,
1094
- lines = False ):
1123
+ def to_json (self , path_or_buf = None , orient = None , date_format = None ,
1124
+ double_precision = 10 , force_ascii = True , date_unit = 'ms' ,
1125
+ default_handler = None , lines = False ):
1095
1126
"""
1096
1127
Convert the object to a JSON string.
1097
1128
@@ -1124,19 +1155,19 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1124
1155
- index : dict like {index -> {column -> value}}
1125
1156
- columns : dict like {column -> {index -> value}}
1126
1157
- values : just the values array
1127
- - ``' table'`` dict like
1158
+ - table : dict like {'schema': {schema}, 'data': {data}}
1128
1159
``{'schema': [schema], 'data': [data]}``
1129
- where the schema component is a Table Schema
1130
- describing the data, and the data component is
1131
- like ``orient='records'``.
1160
+ the schema component is a ` Table Schema_`
1161
+ describing the data, and the data component is
1162
+ like ``orient='records'``.
1132
1163
1133
1164
.. versionchanged:: 0.20.0
1134
1165
1135
- date_format : {'epoch', 'iso'}
1166
+ date_format : {None, 'epoch', 'iso'}
1136
1167
Type of date conversion. `epoch` = epoch milliseconds,
1137
- `iso` = ISO8601. Default is epoch, except when orient is
1138
- table_schema, in which case this parameter is ignored
1139
- and iso formatting is always used .
1168
+ `iso` = ISO8601. The default depends on the ` orient`. For
1169
+ `orient='table'`, the default is `'iso'`. For all other orients,
1170
+ the default is `'epoch'` .
1140
1171
double_precision : The number of decimal places to use when encoding
1141
1172
floating point values, default 10.
1142
1173
force_ascii : force encoded string to be ASCII, default True.
@@ -1155,6 +1186,7 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1155
1186
1156
1187
.. versionadded:: 0.19.0
1157
1188
1189
+ .. _Table Schema: http://specs.frictionlessdata.io/json-table-schema/
1158
1190
1159
1191
Returns
1160
1192
-------
@@ -1199,6 +1231,10 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
1199
1231
"""
1200
1232
1201
1233
from pandas .io import json
1234
+ if date_format is None and orient == 'table' :
1235
+ date_format = 'iso'
1236
+ elif date_format is None :
1237
+ date_format = 'epoch'
1202
1238
return json .to_json (path_or_buf = path_or_buf , obj = self , orient = orient ,
1203
1239
date_format = date_format ,
1204
1240
double_precision = double_precision ,
0 commit comments