Skip to content

Commit

Permalink
Merge pull request #148 from quantopian/fix-column-order
Browse files Browse the repository at this point in the history
Fix column order issue, better handling of NaNs
  • Loading branch information
TimShawver authored Dec 4, 2017
2 parents 8a91c17 + acdbf51 commit 3415091
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Run the following to install and enable qgrid::

OR

conda install -c tim_shawver/label/dev qgrid==1.0.0b8
conda install -c tim_shawver/label/dev qgrid==1.0.0b9

If you haven't enabled the ipywidgets nbextension yet, you'll need to also run this command::

Expand All @@ -61,7 +61,7 @@ If you haven't already install jupyterlab and enabled ipywidgets, do that first

Install the qgrid-jupyterlab extension and enable::

jupyter labextension install [email protected].8
jupyter labextension install [email protected].9
jupyter labextension enable qgrid-jupyterlab

At this point if you run jupyter lab normally with the 'jupyter lab' command, you should be
Expand Down Expand Up @@ -107,7 +107,7 @@ Compatibility:
0.3.x 4.1 4.1.x
0.3.2 4.2 5.x
0.3.3 5.x 6.x
1.0.0b8 5.x 7.x
1.0.0b9 5.x 7.x
================= =========================== ==============================


Expand Down
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qgrid",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "An Interactive Grid for Sorting and Filtering DataFrames in Jupyter Notebook",
"author": "Quantopian Inc.",
"main": "src/index.js",
Expand Down Expand Up @@ -34,7 +34,7 @@
"jquery-ui-dist": "1.12.1",
"json-loader": "^0.5.4",
"moment": "^2.18.1",
"slickgrid-qgrid": "0.0.3",
"slickgrid-qgrid": "0.0.4",
"style-loader": "^0.18.2",
"underscore": "^1.8.3"
},
Expand Down
33 changes: 25 additions & 8 deletions js/src/qgrid.widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class QgridModel extends widgets.DOMWidgetModel {
_view_name : 'QgridView',
_model_module : 'qgrid',
_view_module : 'qgrid',
_model_module_version : '^1.0.0-beta.8',
_view_module_version : '^1.0.0-beta.8',
_model_module_version : '^1.0.0-beta.9',
_view_module_version : '^1.0.0-beta.9',
_df_json: '',
_columns: {}
});
Expand Down Expand Up @@ -244,9 +244,9 @@ class QgridView extends widgets.DOMWidgetView {
super(args);

this.loadValue = (item) => {
var date_value = item[args.column.field];
this.date_value = item[args.column.field];
var formatted_val = self.format_date(
date_value, args.column.field
this.date_value, args.column.field
);
this.input = $(args.container).find('.editor-text');
this.input.val(formatted_val);
Expand All @@ -259,7 +259,14 @@ class QgridView extends widgets.DOMWidgetView {
});
};

this.isValueChanged = () => {
return this.input.val() != this.date_value;
};

this.serializeValue = () => {
if (this.input.val() === "") {
return null;
}
var parsed_date = moment.parseZone(
this.input.val(), "YYYY-MM-DD HH:mm:ss.SSS"
);
Expand All @@ -268,6 +275,9 @@ class QgridView extends widgets.DOMWidgetView {
}
},
formatter: (row, cell, value, columnDef, dataContext) => {
if (value === null){
return "NaT";
}
return this.format_date(value, columnDef.name);
}
},
Expand All @@ -294,9 +304,13 @@ class QgridView extends widgets.DOMWidgetView {
constrainInput: false
});

$.each(columns, (i, cur_column) => {
var sorted_columns = Object.values(columns).sort(
(a, b) => a.position - b.position
);

for(let cur_column of sorted_columns){
if (cur_column.name == this.index_col_name){
return;
continue;
}

var type_info = this.type_infos[cur_column.type] || {};
Expand Down Expand Up @@ -333,10 +347,10 @@ class QgridView extends widgets.DOMWidgetView {
slick_column.editor = editors.IndexEditor;
slick_column.cssClass += ' idx-col';
this.index_columns.push(slick_column);
return;
continue;
}
this.columns.push(slick_column);
});
}

if (this.index_columns.length > 0) {
this.columns = this.index_columns.concat(this.columns);
Expand Down Expand Up @@ -554,6 +568,9 @@ class QgridView extends widgets.DOMWidgetView {
}

format_number(row, cell, value, columnDef, dataContext) {
if (value === null){
return 'NaN';
}
return value;
}

Expand Down
4 changes: 2 additions & 2 deletions jslab/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qgrid-jupyterlab",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "A JupyterLab extension exposing qgrid",
"main": "index.js",
"scripts": {
Expand All @@ -17,7 +17,7 @@
"license": "Apache-2.0",
"dependencies": {
"@jupyter-widgets/jupyterlab-manager": "^0.27.0",
"qgrid": "1.0.0-beta.7"
"qgrid": "1.0.0-beta.9"
},
"jupyterlab": {
"extension": true
Expand Down
2 changes: 1 addition & 1 deletion qgrid/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version_info = (1, 0, 0, 'beta', 8)
version_info = (1, 0, 0, 'beta', 9)

_specifier_ = {'alpha': 'a', 'beta': 'b', 'candidate': 'rc', 'final': ''}

Expand Down
11 changes: 7 additions & 4 deletions qgrid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ class QgridWidget(widgets.DOMWidget):
_model_name = Unicode('QgridModel').tag(sync=True)
_view_module = Unicode('qgrid').tag(sync=True)
_model_module = Unicode('qgrid').tag(sync=True)
_view_module_version = Unicode('1.0.0-beta.8').tag(sync=True)
_model_module_version = Unicode('1.0.0-beta.8').tag(sync=True)
_view_module_version = Unicode('1.0.0-beta.9').tag(sync=True)
_model_module_version = Unicode('1.0.0-beta.9').tag(sync=True)

_df = Instance(pd.DataFrame)
_df_json = Unicode('', sync=True)
Expand Down Expand Up @@ -453,7 +453,7 @@ def _update_table(self, update_columns=False, triggered_by=None,
self._primary_key = [df.index.name]

columns = {}
for cur_column in df_schema['fields']:
for i, cur_column in enumerate(df_schema['fields']):
col_name = cur_column['name']
if 'constraints' in cur_column and \
isinstance(cur_column['constraints']['enum'][0], dict):
Expand All @@ -466,6 +466,7 @@ def _update_table(self, update_columns=False, triggered_by=None,
if col_name in self._primary_key:
cur_column['is_index'] = True

cur_column['position'] = i
columns[col_name] = cur_column

self._columns = columns
Expand Down Expand Up @@ -538,6 +539,8 @@ def _update_sort(self):
inplace=True
)
except TypeError:
self.log.info('TypeError occurred, assuming mixed data type '
'column')
# if there's a TypeError, assume it means that we have a mixed
# type column, and attempt to create a stringified version of
# the column to use for sorting/filtering
Expand All @@ -560,7 +563,7 @@ def _initialize_sort_column(self, col_name, to_timestamp=False):
self._get_col_series_from_df(col_name, self._df)
sort_col_series_unfiltered = \
self._get_col_series_from_df(col_name, self._unfiltered_df)
sort_column_name = col_name + self._sort_col_suffix
sort_column_name = str(col_name) + self._sort_col_suffix

if to_timestamp:
self._df[sort_column_name] = sort_col_series.to_timestamp()
Expand Down
14 changes: 14 additions & 0 deletions qgrid/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ def test_mixed_type_column():
'search_val': None
})

def test_nans():
df = pd.DataFrame([(pd.Timestamp('2017-02-02'), np.nan), (4, 2), ('foo', 'bar')])
view = QgridWidget(df=df)
view._handle_qgrid_msg_helper({
'type': 'sort_changed',
'sort_field': 1,
'sort_ascending': True
})
view._handle_qgrid_msg_helper({
'type': 'get_column_min_max',
'field': 1,
'search_val': None
})

def test_period_object_column():
range_index = pd.period_range(start='2000', periods=10, freq='B')
df = pd.DataFrame({'a': 5, 'b': range_index}, index=range_index)
Expand Down

0 comments on commit 3415091

Please sign in to comment.