Skip to content

Commit

Permalink
WIP show dicts using str
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jun 19, 2024
1 parent c299b83 commit 0c77970
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/itables/datatables_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
and ((x > JS_MAX_SAFE_INTEGER).any() or (x < JS_MIN_SAFE_INTEGER).any())
for x in (df[col] for col in df.columns)
)
data = replace_dicts_with_strings(data)
js = json.dumps(data, cls=generate_encoder(False), allow_nan=not pure_json)

if has_bigints:
Expand All @@ -126,6 +127,10 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
return js


def replace_dicts_with_strings(data):
return [[str(x) if isinstance(x, dict) else x for x in row] for row in data]


def n_suffix_for_bigints(js, pure_json=False):
def n_suffix(matchobj):
if pure_json:
Expand Down
2 changes: 1 addition & 1 deletion src/itables/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""ITables' version number"""

__version__ = "2.1.2"
__version__ = "2.1.3-dev"
14 changes: 14 additions & 0 deletions tests/test_polars.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from itables import to_html_datatable
from itables.javascript import datatables_rows
from itables.sample_dfs import get_dict_of_test_dfs, get_dict_of_test_series

try:
Expand All @@ -21,3 +22,16 @@ def test_show_polars_series(name, x, use_to_html):
)
def test_show_polars_df(name, df, use_to_html):
to_html_datatable(df, use_to_html)


def test_value_counts_shown_as_string():
"""
We don't want to pass dicts to datatable
as these appear as 'Object', cf. #290
"""
count = polars.DataFrame(["id_1"], schema={"col_1"}).select(
polars.col("col_1").value_counts()
)
assert datatables_rows(count) == [
["{'col_1': 'id_1', 'count': 1}"]
] # e.g. a str, not a dict

0 comments on commit 0c77970

Please sign in to comment.