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

JSON encoding of non-finite floats #996

Open
wants to merge 2 commits into
base: spark-branch
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/pandas_profiling/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class Html(BaseModel):
full_width: bool = False


# Describes how non-finite numbers (such as NaN or Infinity) should be encoded in the JSON output
class JsonNonFiniteEncoding(Enum):
# Use the default python behaviour, which violates the official JSON standard
PYTHON_NATIVE = 0
# Encode non-finite numbers as null values
NULL = 1
# Encode non-finite numbers as null values
Comment on lines +187 to +189
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same comment, can you update the string one?

STRING = 2


class Duplicates(BaseModel):
head: int = 10
key: str = "# duplicates"
Expand Down Expand Up @@ -293,6 +303,9 @@ class Config:
n_freq_table_max: int = 10
n_extreme_obs: int = 10

# JSON output
json_non_finite_encoding: JsonNonFiniteEncoding = JsonNonFiniteEncoding.NULL

# Report rendering
report: Report = Report()
html: Html = Html()
Expand Down
27 changes: 25 additions & 2 deletions src/pandas_profiling/profile_report.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import json
import math
import warnings
from pathlib import Path
from typing import Any, Dict, Optional, Union
Expand All @@ -10,7 +11,13 @@
from tqdm.auto import tqdm
from visions import VisionsTypeset

from pandas_profiling.config import Config, PandasSettings, Settings, SparkSettings
from pandas_profiling.config import (
Config,
PandasSettings,
Settings,
SparkSettings,
JsonNonFiniteEncoding
)
from pandas_profiling.expectations_report import ExpectationsReport
from pandas_profiling.model.alerts import AlertType
from pandas_profiling.model.describe import describe as describe_df
Expand Down Expand Up @@ -335,8 +342,24 @@ def encode_it(o: Any) -> Any:
if isinstance(o, dict):
return {encode_it(k): encode_it(v) for k, v in o.items()}
else:
if isinstance(o, (bool, int, float, str)):
if isinstance(o, (bool, int, str)):
return o
elif isinstance(o, float):
if not math.isfinite(o):
# Special handling for non-finite floats.
# This is necessary because JSON does not support NaN/Infinity values.
# The default in Python is to generate invalid JSON.
# Depending on the configuration, we can encode them as null values,
# stringify the non-finite value, or output it as is to keep the default
# Python behaviour.
if self.config.json_non_finite_encoding == JsonNonFiniteEncoding.NULL:
return None
elif self.config.json_non_finite_encoding == JsonNonFiniteEncoding.STRING:
return str(o)
else:
return o
else:
return o
elif isinstance(o, list):
return [encode_it(v) for v in o]
elif isinstance(o, set):
Expand Down