Skip to content

Commit 9f84cae

Browse files
Nismamjad1NISMAMJADrcap107
authored
FEAT - add support for numpy arrays to the TableReport (#1676)
Co-authored-by: Nismamjad1 <nisma.amjad1@gmail.com> Co-authored-by: Riccardo Cappuzzo <7548232+rcap107@users.noreply.github.com>
1 parent 80d2ce4 commit 9f84cae

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ New features
2626
environments produced by a CV splitter -- similar to
2727
:meth:`DataOp.skb.train_test_split` but for multiple cross-validation splits.
2828
:pr:`1653` by :user:`Jérôme Dockès <jeromedockes>`.
29+
- :class:`TableReport` now supports ``np.array``. :pr:`1676` by :user: `Nisma Amjad <Nismamjad1>`.
2930
- :meth:`DataOp.skb.full_report` now accepts a new parameter, title, that is displayed
3031
in the html report.
3132
:pr:`1654` by :user:`Marie Sacksick <MarieSacksick>`.

skrub/_apply_to_cols.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ class ApplyToCols(TransformerMixin, BaseEstimator):
220220
"""Map a transformer to columns in a dataframe.
221221
222222
A separate clone of the transformer is applied to each column separately.
223+
224+
Columns that are not selected in the ``cols`` parameter are passed through
225+
without modification.
226+
223227
All columns not listed in ``cols`` remain unmodified in the output.
224228
Moreover, if ``allow_reject`` is ``True`` and the transformers'
225229
``fit_transform`` raises a ``RejectColumn`` exception for a particular

skrub/_reporting/_table_report.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import numbers
55
from pathlib import Path
66

7+
import numpy as np
8+
import pandas as pd
9+
710
from .. import _config
811
from .. import _dataframe as sbd
912
from ._html import to_html
@@ -46,9 +49,9 @@ def _check_max_cols(max_plot_columns, max_association_columns):
4649
class TableReport:
4750
r"""Summarize the contents of a dataframe.
4851
49-
This class summarizes a dataframe, providing information such as the type
50-
and summary statistics (mean, number of missing values, etc.) for each
51-
column.
52+
This class summarizes a dataframe or numpy array, providing information such as
53+
the type and summary statistics (mean, number of missing values, etc.) for each
54+
column. Numpy arrays are converted to pandas DataFrame or Series.
5255
5356
Parameters
5457
----------
@@ -186,6 +189,21 @@ def __init__(
186189
max_plot_columns=None,
187190
max_association_columns=None,
188191
):
192+
if isinstance(dataframe, np.ndarray):
193+
if dataframe.ndim == 1:
194+
dataframe = pd.Series(dataframe, name="0")
195+
196+
elif dataframe.ndim == 2:
197+
dataframe = pd.DataFrame(
198+
dataframe, columns=[str(i) for i in range(dataframe.shape[1])]
199+
)
200+
201+
else:
202+
raise ValueError(
203+
f"Input NumPy array has {dataframe.ndim} dimensions. "
204+
"TableReport only supports 1D and 2D arrays"
205+
)
206+
189207
n_rows = max(1, n_rows)
190208
self._summary_kwargs = {
191209
"order_by": order_by,

skrub/_reporting/tests/test_table_report.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,45 @@ def test_bad_cols_parameter(pd_module, arg):
337337
df = pd_module.example_dataframe
338338
with pytest.raises(ValueError):
339339
TableReport(df, **{arg: -1})
340+
341+
342+
def test_array_dim_check():
343+
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
344+
assert array_3d.ndim == 3
345+
with pytest.raises(ValueError, match=r"Input (NumPy )?array has 3 dimensions"):
346+
TableReport(array_3d)
347+
348+
array_4d = np.array([[[[1]]]])
349+
assert array_4d.ndim == 4
350+
with pytest.raises(ValueError, match=r"Input (NumPy )?array has 4 dimensions"):
351+
TableReport(array_4d)
352+
353+
array_1d = np.array([1, 2, 3])
354+
assert array_1d.ndim == 1
355+
356+
TableReport(array_1d)
357+
358+
359+
numpy_test_cases = [
360+
(
361+
np.array(
362+
[
363+
[1, 2, 3],
364+
[
365+
4,
366+
5,
367+
6,
368+
],
369+
]
370+
),
371+
3,
372+
),
373+
(np.array([[10, 20], [30, 40], [50, 60], [60, 70]]), 2),
374+
]
375+
376+
377+
@pytest.mark.parametrize("input_array, expected_columns", numpy_test_cases)
378+
def test_numpy_array_columns(input_array, expected_columns):
379+
report = TableReport(input_array, max_association_columns=0)
380+
381+
assert report._summary["n_columns"] == expected_columns

0 commit comments

Comments
 (0)