Skip to content

Commit ca39cb4

Browse files
authored
Support nullable types (#101)
* Compute the default value for 'order' after that of 'showIndex' * Add support for nullable types * Version 1.3.0
1 parent 2bfa48c commit ca39cb4

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

docs/changelog.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ ITables ChangeLog
55
------------------
66

77
**Changed**
8-
- The order of rows is preserved by default (unless you explicitly pass an `order` parameter).
8+
- The order of rows is preserved by default (unless you explicitly pass an `order` parameter) ([#99](https://github.com/mwouts/itables/issues/99)).
9+
10+
**Fixed**
11+
- Nullable types (bool, int) are now supported ([#98](https://github.com/mwouts/itables/issues/98))
912

1013

1114
1.2.0 (2022-08-15)
1215
------------------
1316

1417
**Added**
15-
- New `to_html_datatable` function to export a DataFrame to an HTML div (#88)
16-
- We have added examples on how to use `itables` in [Shiny](https://shiny.rstudio.com/py/) for Python (#86)
18+
- New `to_html_datatable` function to export a DataFrame to an HTML div ([#88](https://github.com/mwouts/itables/issues/88))
19+
- We have added examples on how to use `itables` in [Shiny](https://shiny.rstudio.com/py/) for Python ([#86](https://github.com/mwouts/itables/issues/86))
1720

1821

1922
1.1.3 (2022-08-11)

docs/sample_dataframes.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,30 @@ init_notebook_mode(all_interactive=True)
3030
show(dict_of_test_dfs["empty"])
3131
```
3232

33+
## bool
34+
35+
```{code-cell}
36+
show(dict_of_test_dfs["bool"])
37+
```
38+
39+
## Nullable boolean
40+
41+
```{code-cell}
42+
show(dict_of_test_dfs["nullable_boolean"])
43+
```
44+
3345
## int
3446

3547
```{code-cell}
3648
show(dict_of_test_dfs["int"])
3749
```
3850

51+
## Nullable integer
52+
53+
```{code-cell}
54+
show(dict_of_test_dfs["nullable_int"])
55+
```
56+
3957
## float
4058

4159
```{code-cell}

itables/javascript.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def _formatted_values(df):
122122
except ValueError:
123123
pass
124124

125-
return formatted_df.values.tolist()
125+
rows = formatted_df.values.tolist()
126+
127+
# Replace pd.NA with None
128+
return [[cell if cell is not pd.NA else None for cell in row] for row in rows]
126129

127130

128131
def _table_header(
@@ -250,20 +253,6 @@ def to_html_datatable(df=None, tableId=None, connected=True, **kwargs):
250253

251254
df = downsample(df, max_rows=maxRows, max_columns=maxColumns, max_bytes=maxBytes)
252255

253-
# Unless an 'order' parameter is given, we preserve the current order of rows #99
254-
order = kwargs.pop("order", None)
255-
256-
if order is None:
257-
order = []
258-
259-
if showIndex:
260-
if df.index.is_monotonic_increasing:
261-
order = [[i, "asc"] for i, _ in enumerate(df.index.names)]
262-
elif df.index.is_monotonic_decreasing:
263-
order = [[i, "desc"] for i, _ in enumerate(df.index.names)]
264-
265-
kwargs["order"] = order
266-
267256
footer = kwargs.pop("footer")
268257
column_filters = kwargs.pop("column_filters")
269258
if column_filters == "header":
@@ -296,6 +285,20 @@ def to_html_datatable(df=None, tableId=None, connected=True, **kwargs):
296285
if not showIndex:
297286
df = df.set_index(pd.RangeIndex(len(df.index)))
298287

288+
# Unless an 'order' parameter is given, we preserve the current order of rows #99
289+
order = kwargs.pop("order", None)
290+
291+
if order is None:
292+
order = []
293+
294+
if showIndex:
295+
if df.index.is_monotonic_increasing:
296+
order = [[i, "asc"] for i, _ in enumerate(df.index.names)]
297+
elif df.index.is_monotonic_decreasing:
298+
order = [[i, "desc"] for i, _ in enumerate(df.index.names)]
299+
300+
kwargs["order"] = order
301+
299302
table_header = _table_header(
300303
df, tableId, showIndex, classes, style, tags, footer, column_filters
301304
)

itables/sample_dfs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ def get_dict_of_test_dfs(N=100, M=100):
4343

4444
return {
4545
"empty": pd.DataFrame(),
46+
"bool": pd.DataFrame(
47+
[[True, True, False, False], [True, False, True, False]],
48+
columns=list("abcd"),
49+
),
50+
"nullable_boolean": pd.DataFrame(
51+
[
52+
[True, True, False, None],
53+
[True, False, None, False],
54+
[None, False, True, False],
55+
],
56+
columns=list("abcd"),
57+
dtype="boolean",
58+
),
4659
"int": pd.DataFrame(
4760
[[-1, 2, -3, 4, -5], [6, -7, 8, -9, 10]], columns=list("abcde")
4861
),
62+
"nullable_int": pd.DataFrame(
63+
[[-1, 2, -3], [4, -5, 6], [None, 7, None]],
64+
columns=list("abc"),
65+
dtype="Int64",
66+
),
4967
"float": pd.DataFrame(
5068
{
5169
"int": [0.0, 1],

itables/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""ITables' version number"""
22

3-
__version__ = "1.3.0-dev"
3+
__version__ = "1.3.0"

0 commit comments

Comments
 (0)