Skip to content
Closed
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
22 changes: 22 additions & 0 deletions mapie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,3 +1657,25 @@ def _raise_error_if_fit_called_in_prefit_mode(
"The fit method must be skipped when the prefit parameter is set to True. "
"Use the conformalize method directly after instanciation."
)


def check_if_X_dataframe(
X: ArrayLike,
) -> None:
"""
Check if X is a pandas DataFrame.

Parameters
----------
X: ArrayLike
The input data to check.

Raises
------
TypeError
If X is not a pandas DataFrame.
"""
if not hasattr(X, 'iloc') or not hasattr(X, 'columns'):
raise TypeError(
"X must be a pandas DataFrame."
)
22 changes: 21 additions & 1 deletion tests_v1/test_unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
_raise_error_if_previous_method_not_called,
_raise_error_if_method_already_called,
_raise_error_if_fit_called_in_prefit_mode,
train_conformalize_test_split
train_conformalize_test_split,
check_if_X_dataframe
)
from unittest.mock import patch
import pandas as pd


RANDOM_STATE = 1
Expand Down Expand Up @@ -264,3 +266,21 @@ def test_raises_error_in_prefit_mode(self):

def test_does_nothing_when_not_in_prefit_mode(self):
assert _raise_error_if_fit_called_in_prefit_mode(False) is None


class TestCheckIfXDataFrame:
def test_valid_dataframe(self):
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
# Should not raise
assert check_if_X_dataframe(df) is None

@pytest.mark.parametrize("invalid_X", [
np.array([[1, 2], [3, 4]]),
[[1, 2], [3, 4]],
{"a": [1, 2]},
42,
"not a dataframe"
])
def test_invalid_types_raise(self, invalid_X):
with pytest.raises(TypeError):
check_if_X_dataframe(invalid_X)
Loading