44import operator
55import weakref
66import gc
7+ import json
78
89import numpy as np
910import pandas .lib as lib
@@ -129,6 +130,37 @@ def __init__(self, data, axes=None, copy=False, dtype=None,
129130 object .__setattr__ (self , '_data' , data )
130131 object .__setattr__ (self , '_item_cache' , {})
131132
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+
132164 def _validate_dtype (self , dtype ):
133165 """ validate the passed dtype """
134166
@@ -1094,10 +1126,9 @@ def __setstate__(self, state):
10941126 strings before writing.
10951127 """
10961128
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 ):
11011132 """
11021133 Convert the object to a JSON string.
11031134
@@ -1131,17 +1162,16 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
11311162 - columns : dict like {column -> {index -> value}}
11321163 - values : just the values array
11331164 - table : dict like {'schema': {schema}, 'data': {data}}
1134- the schema component is a `Table Schema_`
11351165 describing the data, and the data component is
11361166 like ``orient='records'``.
11371167
11381168 .. versionchanged:: 0.20.0
11391169
1140- date_format : {'epoch', 'iso'}
1170+ date_format : {None, 'epoch', 'iso'}
11411171 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'` .
11451175 double_precision : The number of decimal places to use when encoding
11461176 floating point values, default 10.
11471177 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',
11601190
11611191 .. versionadded:: 0.19.0
11621192
1193+ .. _Table Schema: http://specs.frictionlessdata.io/json-table-schema/
11631194
11641195 Returns
11651196 -------
@@ -1204,6 +1235,10 @@ def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
12041235 """
12051236
12061237 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'
12071242 return json .to_json (path_or_buf = path_or_buf , obj = self , orient = orient ,
12081243 date_format = date_format ,
12091244 double_precision = double_precision ,
0 commit comments