Skip to content

Commit a9c7cf9

Browse files
committed
fix: allow NoneType values to be string formatted
This change addresses issue #1723. It implements a "N/A" string as the default when formatting NoneType values.
1 parent 588be6b commit a9c7cf9

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var/
2424
*.egg-info/
2525
.installed.cfg
2626
*.egg
27+
uv.lock
2728

2829
# PyInstaller
2930
# Usually these files are written by a python script from a template
@@ -85,3 +86,6 @@ docsrc/source/pages/api/_autosummary/
8586
# User created
8687
VERSION
8788
version.py
89+
90+
# local java/spark sdk environment files
91+
.sdkmanrc

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ dev = [
8787
"sphinx-autodoc-typehints>=1.10.3",
8888
"sphinx-multiversion>=0.2.3",
8989
"autodoc_pydantic",
90+
"standard-imghdr"
9091
]
9192

9293
docs = [

src/ydata_profiling/report/formatters.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,16 @@ def fmt_numeric(value: float, precision: int = 10) -> str:
245245
Returns:
246246
The numeric value with the given precision.
247247
"""
248-
fmtted = f"{{:.{precision}g}}".format(value)
249-
for v in ["e+", "e-"]:
250-
if v in fmtted:
251-
sign = "-" if v in "e-" else ""
252-
fmtted = fmtted.replace(v, " × 10<sup>") + "</sup>"
253-
fmtted = fmtted.replace("<sup>0", "<sup>")
254-
fmtted = fmtted.replace("<sup>", f"<sup>{sign}")
248+
if value is None:
249+
fmtted = "N/A"
250+
else:
251+
fmtted = f"{{:.{precision}g}}".format(value)
252+
for v in ["e+", "e-"]:
253+
if v in fmtted:
254+
sign = "-" if v in "e-" else ""
255+
fmtted = fmtted.replace(v, " × 10<sup>") + "</sup>"
256+
fmtted = fmtted.replace("<sup>0", "<sup>")
257+
fmtted = fmtted.replace("<sup>", f"<sup>{sign}")
255258

256259
return fmtted
257260

tests/unit/test_formatters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_fmt_array(array, threshold, expected):
8383
(1e20, 10, "1 × 10<sup>20</sup>"),
8484
(1e-20, 10, "1 × 10<sup>-20</sup>"),
8585
(1e8, 3, "1 × 10<sup>8</sup>"),
86+
(None, 3, "N/A"),
8687
],
8788
)
8889
def test_fmt_numeric(value, precision, expected):

0 commit comments

Comments
 (0)