Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the rendering of Dict/Struct #292

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/itables/datatables_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
or (x.dtype == pl.UInt64 and (x > JS_MAX_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 @@ -129,6 +130,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.4"
__version__ = "2.1.5-dev"
13 changes: 13 additions & 0 deletions tests/test_polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ def test_encode_mixed_contents():
datatables_rows(df)
== '[[BigInt("1666767918216000000"), 1699300000000, 0.9510565400123596, -0.30901700258255005]]'
)


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
Loading