Skip to content

Commit

Permalink
Merge pull request #90 from martinRenou/from_dataframe_dtype_int
Browse files Browse the repository at this point in the history
Fix integer formatting when loading numpy arrays
  • Loading branch information
martinRenou committed Mar 18, 2019
2 parents 84508e3 + 549bcca commit bd59050
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
8 changes: 4 additions & 4 deletions ipysheet/easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def current():

def cell(row, column, value=0., type=None, color=None, background_color=None,
font_style=None, font_weight=None, style=None, label_left=None, choice=None,
read_only=False, numeric_format='0.[000]', date_format='YYYY/MM/DD', renderer=None, **kwargs):
read_only=False, numeric_format='0.000', date_format='YYYY/MM/DD', renderer=None, **kwargs):
"""Adds a new `Cell` widget to the current sheet
Parameters
Expand Down Expand Up @@ -124,7 +124,7 @@ def cell(row, column, value=0., type=None, color=None, background_color=None,


def row(row, value, type=None, column_start=0, column_end=None, choice=None,
read_only=False, numeric_format='0.[000]', date_format='YYYY/MM/DD', renderer=None,
read_only=False, numeric_format='0.000', date_format='YYYY/MM/DD', renderer=None,
color=None, background_color=None, font_style=None, font_weight=None, **kwargs):
"""Create a CellRange widget, representing multiple cells in a sheet, in a horizontal column
Expand All @@ -150,7 +150,7 @@ def row(row, value, type=None, column_start=0, column_end=None, choice=None,


def column(column, value, type=None, row_start=0, row_end=None, choice=None,
read_only=False, numeric_format='0.[000]', date_format='YYYY/MM/DD', renderer=None,
read_only=False, numeric_format='0.000', date_format='YYYY/MM/DD', renderer=None,
color=None, background_color=None, font_style=None, font_weight=None, **kwargs):
"""Create a CellRange widget, representing multiple cells in a sheet, in a vertical column
Expand Down Expand Up @@ -178,7 +178,7 @@ def column(column, value, type=None, row_start=0, row_end=None, choice=None,

def cell_range(value, row_start=0, column_start=0, row_end=None, column_end=None, transpose=False,
squeeze_row=False, squeeze_column=False, type=None, choice=None,
read_only=False, numeric_format='0.[000]', date_format='YYYY/MM/DD', renderer=None, style=None,
read_only=False, numeric_format='0.000', date_format='YYYY/MM/DD', renderer=None, style=None,
color=None, background_color=None, font_style=None, font_weight=None, **kwargs):
"""Create a CellRange widget, representing multiple cells in a sheet, in a horizontal column
Expand Down
11 changes: 8 additions & 3 deletions ipysheet/numpy_loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .easy import sheet, column, cell_range
from .utils import extract_data
from .utils import extract_data, get_cell_type, get_cell_numeric_format


def from_array(array):
Expand Down Expand Up @@ -30,11 +30,16 @@ def from_array(array):
rows = array.shape[0]
columns = 1 if len(array.shape) == 1 else array.shape[1]

kwargs = {
'numeric_format': get_cell_numeric_format(array.dtype),
'type': get_cell_type(array.dtype)
}

out_sheet = sheet(rows=rows, columns=columns)
if columns == 1:
column(0, array)
column(0, array, **kwargs)
else:
cell_range(array)
cell_range(array, **kwargs)
return out_sheet


Expand Down
22 changes: 3 additions & 19 deletions ipysheet/pandas_loader.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
from .sheet import Cell, Sheet
from .utils import extract_data


def _get_cell_type(dt):
# TODO Differentiate integer and float? Using custom renderers and
# validators for integers?
# Add support for void type from NumPy?
# See https://handsontable.com/docs/6.2.2/tutorial-cell-types.html
return {
'b': 'checkbox',
'i': 'numeric',
'u': 'numeric',
'f': 'numeric',
'm': 'numeric',
'M': 'date',
'S': 'text',
'U': 'text'
}.get(dt.kind, 'text')
from .utils import extract_data, get_cell_numeric_format, get_cell_type


def _format_date(date):
Expand Down Expand Up @@ -76,7 +59,8 @@ def from_dataframe(dataframe):
row_end=len(rows) - 1,
column_start=idx,
column_end=idx,
type=_get_cell_type(arr.dtype),
type=get_cell_type(arr.dtype),
numeric_format=get_cell_numeric_format(arr.dtype),
squeeze_row=False,
squeeze_column=True
))
Expand Down
2 changes: 1 addition & 1 deletion ipysheet/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Cell(widgets.Widget):
squeeze_column = Bool(True).tag(sync=True)
transpose = Bool(False).tag(sync=True)
choice = List(Unicode(), allow_none=True, default_value=None).tag(sync=True)
numeric_format = Unicode('0.[000]', allow_none=True).tag(sync=True)
numeric_format = Unicode('0.000', allow_none=True).tag(sync=True)
date_format = Unicode('YYYY/MM/DD', allow_none=True).tag(sync=True)

@traitlets.validate('value')
Expand Down
10 changes: 7 additions & 3 deletions ipysheet/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,25 +434,29 @@ def test_from_dataframe():
'C': pd.Series(1, index=list(range(4)), dtype='float32'),
'D': np.array([False, True, False, False], dtype='bool'),
'S': pd.Categorical(["test", "train", "test", "train"]),
'T': 'foo'})
'T': 'foo',
'X': np.array([0, 3, 9, 18])})

df.loc[[0, 2], ['B']] = np.nan

sheet = ipysheet.from_dataframe(df)
assert len(sheet.cells) == 6
assert sheet.column_headers == ['A', 'B', 'C', 'D', 'S', 'T']
assert len(sheet.cells) == 7
assert sheet.cells[0].value == [1., 1., 1., 1.]
assert sheet.cells[0].type == 'numeric'
assert sheet.cells[1].value == [None, '2013/01/02', None, '2013/01/02']
assert sheet.cells[1].type == 'date'
assert sheet.cells[2].value == [1., 1., 1., 1.]
assert sheet.cells[2].type == 'numeric'
assert sheet.cells[2].numeric_format == '0.000'
assert sheet.cells[3].value == [False, True, False, False]
assert sheet.cells[3].type == 'checkbox'
assert sheet.cells[4].value == ['test', 'train', 'test', 'train']
assert sheet.cells[4].type == 'text'
assert sheet.cells[5].value == ['foo', 'foo', 'foo', 'foo']
assert sheet.cells[5].type == 'text'
assert sheet.cells[6].value == [0, 3, 9, 18]
assert sheet.cells[6].type == 'numeric'
assert sheet.cells[6].numeric_format == '0[.]0'


def test_from_to_dataframe():
Expand Down
24 changes: 24 additions & 0 deletions ipysheet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,27 @@ def extract_data(sheet):
extract_cell_data(cell, data)

return data


def get_cell_type(dt):
# TODO Differentiate integer and float? Using custom renderers and
# validators for integers?
# Add support for void type from NumPy?
# See https://handsontable.com/docs/6.2.2/tutorial-cell-types.html
return {
'b': 'checkbox',
'i': 'numeric',
'u': 'numeric',
'f': 'numeric',
'm': 'numeric',
'M': 'date',
'S': 'text',
'U': 'text'
}.get(dt.kind, 'text')


def get_cell_numeric_format(dt):
return {
'i': '0[.]0',
'f': '0.000',
}.get(dt.kind)
2 changes: 1 addition & 1 deletion js/src/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let CellRangeModel = widgets.WidgetModel.extend({
squeeze_row: true,
squeeze_column: true,
transpose: false,
numeric_format: '0.[000]',
numeric_format: '0.000',
date_format: 'YYYY/MM/DD'
});
},
Expand Down

0 comments on commit bd59050

Please sign in to comment.