In [1]:
import json
import numpy as np
import pandas as pd
from jupyter_scisheets_widget import scisheets_widget
In [2]:
import pandas_datareader as pdr
ibm_data = pdr.get_data_yahoo('IBM')
In [3]:
income_data = pd.read_csv('income_data.csv', sep=';')
income_data
Out[3]:
In [ ]:
income_data['test_col'] = income_data['2006']
In [ ]:
BUG currently seems to be coming from handonstable rendering
In [6]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_df(income_data)
tbl
In [6]:
tbl._model_row_header
Out[6]:
In [7]:
tbl._model_header
Out[7]:
In [21]:
tbl._model_row_header.encode('utf-8')
Out[21]:
In [36]:
import ast
In [39]:
data_dic = {}
data_dic['columns'] = ast.literal_eval(tbl._model_header)
data_dic['index'] = ast.literal_eval(tbl._model_row_header)
data_dic['data'] = ast.literal_eval(tbl._model_data)
pd.read_json(json.dumps(data_dic), orient='split')
Out[39]:
In [12]:
income_data.to_json(orient='split')
Out[12]:
In [38]:
data_dic
Out[38]:
In [25]:
cols = {'columns': tbl._model_header.encode('utf-8')}
ind = {'index': tbl._model_row_header.encode('utf-8')}
data = {'data': tbl._model_data.encode('utf-8')}
In [27]:
z=cols
z.update(ind)
z.update(data)
In [28]:
z
Out[28]:
In [29]:
json.dumps(z)
Out[29]:
In [33]:
pd.read_json(json.dumps(z))
In [ ]:
model_data = df.to_json(orient='split')
model_data = ast.literal_eval(model_data)
self._model_data = json.dumps(model_data['data'])
self._model_header = json.dumps(model_data['columns'])
self._model_row_header = json.dumps(model_data['index'])
In [5]:
tbl1._widget._model_data
Out[5]:
In [4]:
tbl1 = scisheets_widget.HandsonDataFrame(income_data)
tbl1.show()
In [ ]:
import ipywidgets as widgets
print(widgets.Widget.unobserve.__doc__)
In [ ]:
tbl1._on_displayed()
In [ ]:
tbl1._widget._model_header
In [ ]:
tbl_ibm = scisheets_widget.SciSheetTable()
tbl_ibm.load_df(ibm_data)
tbl_ibm
In [ ]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_df(income_data[['GEOID', 'State']])
tbl
In [ ]:
In [ ]:
In [ ]:
%%html
<div id="example1" class="hot handsontable"></div>
<script src="https://docs.handsontable.com/0.34.4/bower_components/handsontable/dist/handsontable.full.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/0.34.4/bower_components/handsontable/dist/handsontable.full.min.css">
In [ ]:
%%javascript
document.addEventListener("DOMContentLoaded", function() {
var
data1 = [
['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
['2017', 10, 11, 12, 13, 15, 16],
['2018', 10, 11, 12, 13, 15, 16],
['2019', 10, 11, 12, 13, 15, 16],
['2020', 10, 11, 12, 13, 15, 16],
['2021', 10, 11, 12, 13, 15, 16]
],
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
container1.append('allice')
hot1 = new Handsontable(container1, settings1);
data1[0][1] = 'Ford'; // change "Kia" to "Ford" programmatically
hot1.render();
});
In [ ]:
%%javascript
this.element.append('steve')
In [ ]:
data = [{"name": "Lisp", "value": 1}, {"name": "Scala", "value": 2}, {"name": "Perl", "value": 4}, {"name": "Java", "value": 5}, {"name": "C++", "value": 8}, {"name": "Python", "value": 10}]
In [ ]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_list(data)
tbl
In [ ]:
income_data.to_json(orient='index')
In [ ]:
income_data.to_json(orient='values')
In [ ]:
income_data.to_json(orient='records')
In [ ]:
income_data.to_json(orient='columns')
In [ ]:
income_data.to_json(orient='split')
In [ ]:
bob = income_data.to_json(orient='split')
In [ ]:
import ast
bob2 = ast.literal_eval(bob)
In [ ]:
for key, value in bob2.items():
print("Key:")
print(key)
In [ ]:
bob2['columns']
In [ ]:
json.dumps(bob2['columns'])
In [ ]:
bob2['columns']
In [ ]:
In [ ]:
'''Illustrates how to detect an assignment for a DataFrame.'''
import pandas as pd
class VirtualDataFrame(object):
def __init__(self, df):
self._df = df.copy()
def _value_changed_(self):
if self._df.__setitem__:
print "Setting %s to %s" % (str(key), str(value))
def __setitem__(self, key, value):
print "Setting %s to %s" % (str(key), str(value))
In [ ]:
class VirtualDataFrame2(object):
def __init__(self, df):
self._df = df
# def _value_changed_(self):
# if self._df.__setitem__:
# print "Setting %s to %s" % (str(key), str(value))
# def __setitem__(self, key, value):
# print "Setting %s to %s" % (str(key), str(value))
In [ ]:
bob2 = VirtualDataFrame2(income_data)
In [ ]:
bob2._df.loc[3,'State'] = 'Louisiana'
In [ ]:
income_data
In [ ]:
bob = VirtualDataFrame(income_data)
In [ ]:
bob._df.loc[2,'State'] = 'Alex'
In [ ]:
income_data
In [ ]:
class with_called_attribute:
def __init__(self, func):
self.func = func
self.called = False
def __call__(self, *args, **kwargs):
self.called = True
self.func(*args, **kwargs)
@with_called_attribute
def run_some_thing(*args, **kwargs):
print("You called with:", *args, **kwargs)
run_some_thing.called
#>>> False
run_some_thing(10)
#>>> You called with: 10
run_some_thing.called
#>>> True
In [ ]:
class Detector(object):
def __init__(self, df):
self.loadTableIsCalledForObjA = False
self._df = df
def __setitem__(self):
if type(self._df) is pd.DataFrame:
print('True')
In [ ]:
bob = Detector(income_data)
In [ ]:
bob._df.loc[1, 'State'] =
In [ ]:
In [ ]:
loadTable(income_data)
In [ ]:
detector.loadTableIsCalledForObjA
In [ ]:
bob._df.loc[1, 'State'] = 'Barber'
In [ ]:
bob['Alice'] = bob['State']
In [ ]:
income_data
In [ ]:
income_data
In [ ]:
import pandas as pd
import numpy as np
class MyDF(pd.DataFrame):
@property
def _constructor(self):
return MyDF
mydf = MyDF(np.random.randn(3,4), columns=['A','B','C','D'])
print type(mydf)
mydf_sub = mydf[['A','C']]
print type(mydf_sub)
In [ ]:
import pandas as pd
import numpy as np
class MyDF(pd.DataFrame):
_attributes_ = "myattr1,myattr2"
def __init__(self, *args, **kw):
super(MyDF, self).__init__(*args, **kw)
if len(args) == 1 and isinstance(args[0], MyDF):
args[0]._copy_attrs(self)
def _copy_attrs(self, df):
for attr in self._attributes_.split(","):
df.__dict__[attr] = getattr(self, attr, None)
@property
def _constructor(self):
def f(*args, **kw):
df = MyDF(*args, **kw)
self._copy_attrs(df)
return df
return f
mydf = MyDF(np.random.randn(3,4), columns=['A','B','C','D'])
print type(mydf)
mydf_sub = mydf[['A','C']]
print type(mydf_sub)
mydf.myattr1 = 1
mydf_cp1 = MyDF(mydf)
mydf_cp2 = mydf.copy()
print mydf_cp1.myattr1, mydf_cp2.myattr1
In [ ]:
mydf_cp1
In [ ]:
mydf_cp2
In [ ]:
def write(self):
return dumps(
self.obj,
orient=self.orient,
double_precision=self.double_precision,
ensure_ascii=self.ensure_ascii,
date_unit=self.date_unit,
iso_dates=self.date_format == 'iso',
default_handler=self.default_handler
)
In [ ]:
import json
In [ ]:
json.dumps(income_data)
In [ ]:
import pandas._libs.json as json
In [ ]:
dumps = json.dumps
In [ ]:
dumps(income_data)
In [ ]:
import pandas._libs.
In [ ]:
print('help')
In [ ]:
print 'help'
In [ ]: