Skip to content

Commit

Permalink
#937: Add check for pandas null in json conversion. (#938)
Browse files Browse the repository at this point in the history
* #937: Add check for pandas null in json conversion.

* #937: Check that object isn't sequence before checking for null.
  • Loading branch information
Liraim authored Jan 9, 2024
1 parent 6f9da2f commit 7a3f5d4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/evidently/utils/numpy_encoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import typing
import uuid
from typing import Callable
from typing import Tuple
Expand Down Expand Up @@ -47,8 +48,14 @@ def default(self, o):
If we cannot convert the object, leave the default `JSONEncoder` behaviour - raise a TypeError exception.
"""

# check mapping rules
for types_list, python_type in _TYPES_MAPPING:
if isinstance(o, types_list):
return python_type(o)

# explicit check pandas null
if not isinstance(o, typing.Sequence) and pd.isnull(o):
return None

return json.JSONEncoder.default(self, o)
8 changes: 6 additions & 2 deletions src/evidently/utils/visualizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,13 @@ def make_hist_for_cat_plot(curr: pd.Series, ref: pd.Series = None, normalize: bo

def get_distribution_for_category_column(column: pd.Series, normalize: bool = False) -> Distribution:
value_counts = column.value_counts(normalize=normalize, dropna=False)

# filter out na values if it amount == 0
new_values = [(k, v) for k, v in value_counts.items() if (not pd.isna(k) or v > 0)]

return Distribution(
x=value_counts.index.values,
y=value_counts.values,
x=[x[0] for x in new_values],
y=[x[1] for x in new_values],
)


Expand Down

0 comments on commit 7a3f5d4

Please sign in to comment.