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 Jul 3, 2024
1 parent dfe261c commit 4d0d124
Show file tree
Hide file tree
Showing 3 changed files with 19 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 @@ -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

0 comments on commit 4d0d124

Please sign in to comment.