Skip to content

Commit b69feca

Browse files
authored
FIX - Adding condition and test for Polars dataframes without PyArrow installed in TableReport (skrub-data#1742)
1 parent 98dfba2 commit b69feca

4 files changed

Lines changed: 64 additions & 1 deletion

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Bugfixes
6060
- Fixed the use of :class:`TableReport` and :class:`Cleaner` with Polars dataframes
6161
containing a column with empty string as name.
6262
:pr:`1722` by :user:`Marie Sacksick <MarieSacksick>`.
63+
- Fixed an issue where :class:`TableReport` would fail when computing associations
64+
for Polars dataframes if PyArrow was not installed.
65+
:pr:`1742` by :user:`Riccardo Cappuzzo <rcap107>`.
6366

6467
Release 0.6.2
6568
=============

skrub/_reporting/_data/templates/column-associations.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
</div>
99
</div>
1010

11+
{% elif summary.get("associations_skipped_polars_no_pyarrow", False) %}
12+
13+
<div id="skipped-associations-polars-pyarrow-alert" class="alert-info-dismissable flex space-between">
14+
<div class="alert-content shrinkable-text">
15+
Computing pairwise associations is not available for Polars dataframes when PyArrow is not installed.
16+
To enable associations, install PyArrow: <code>pip install pyarrow</code>, or use a Pandas dataframe.
17+
</div>
18+
</div>
19+
1120
{% elif summary["top_associations"] %}
1221

1322
<div class="horizontal-scroll padding-b-s">

skrub/_reporting/_summarize.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
from .. import _dataframe as sbd
77
from . import _plotting, _sample_table, _utils
88

9+
try:
10+
import pyarrow # noqa F401
11+
12+
_PYARROW_INSTALLED = True
13+
except ImportError:
14+
_PYARROW_INSTALLED = False
15+
16+
917
_SUBSAMPLE_SIZE = 3000
1018
_N_TOP_ASSOCIATIONS = 20
1119

@@ -112,7 +120,10 @@ def summarize_dataframe(
112120
summary["n_constant_columns"] = sum(
113121
c["value_is_constant"] for c in summary["columns"]
114122
)
115-
if with_associations:
123+
if not _PYARROW_INSTALLED and summary["dataframe_module"] == "polars":
124+
with_associations = False
125+
summary["associations_skipped_polars_no_pyarrow"] = True
126+
elif with_associations:
116127
if n_rows and n_columns:
117128
_add_associations(df, summary)
118129
else:

skrub/_reporting/tests/test_table_report.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,43 @@ def test_numpy_array_columns(input_array, expected_columns):
379379
report = TableReport(input_array, max_association_columns=0)
380380

381381
assert report._summary["n_columns"] == expected_columns
382+
383+
384+
def _pyarrow_available():
385+
try:
386+
import pyarrow # noqa: F401
387+
388+
return True
389+
except ImportError:
390+
return False
391+
392+
393+
@pytest.mark.xfail(
394+
condition=_pyarrow_available(),
395+
reason="Test expects pyarrow to not be installed, but it is installed",
396+
)
397+
def test_polars_df_no_pyarrow():
398+
# Test that when using a Polars dataframe without pyarrow installed,
399+
# the appropriate flag is set in the summary and the message appears in the HTML.
400+
pl = pytest.importorskip("polars")
401+
402+
df = pl.DataFrame(
403+
{
404+
"A": [1, 2, 3, 4, 5],
405+
"B": ["a", "b", "c", "d", "e"],
406+
"C": [10, 20, 30, 40, 50],
407+
}
408+
)
409+
410+
report = TableReport(df, verbose=0)
411+
summary = report._summary
412+
413+
assert summary.get("associations_skipped_polars_no_pyarrow", False) is True
414+
assert summary.get("dataframe_module", "") == "polars"
415+
416+
html_snippet = report.html_snippet()
417+
assert (
418+
"Computing pairwise associations is not available for Polars dataframes "
419+
"when PyArrow is not installed"
420+
in html_snippet
421+
)

0 commit comments

Comments
 (0)