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

Raise error on empty frequent itemset to association_rules #843

Merged
merged 2 commits into from
Sep 2, 2021
Merged
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
1 change: 1 addition & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The CHANGELOG for the current development version is available at
- Adds a `title_fontsize` parameter to `plot_learning_curves` for controlling the title font size; also the plot style is now the matplotlib default. ([#818](https://github.com/rasbt/mlxtend/pull/818))
- Internal change using `'c': 'none'` instead of `'c': ''` in `mlxtend.plotting.plot_decision_regions`'s scatterplot highlights to stay compatible with Matplotlib 3.4 and newer. ([#822](https://github.com/rasbt/mlxtend/pull/822))
- Adds a `fontcolor_threshold` parameter to the `mlxtend.plotting.plot_confusion_matrix` function as an additional option for determining the font color cut-off manually. ([#825](https://github.com/rasbt/mlxtend/pull/825))
- The `frequent_patterns.association_rules` now raises a `ValueError` if an empty frequent itemset DataFrame is passed. ([#842](https://github.com/rasbt/mlxtend/pull/842))

##### Bug Fixes

Expand Down
3 changes: 3 additions & 0 deletions mlxtend/frequent_patterns/association_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def association_rules(df, metric="confidence",
http://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/

"""
if not df.shape[0]:
raise ValueError('The input DataFrame `df` containing '
'the frequent itemsets is empty.')

# check for mandatory columns
if not all(col in df.columns for col in ["support", "itemsets"]):
Expand Down
7 changes: 7 additions & 0 deletions mlxtend/frequent_patterns/tests/test_association_rules.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
import pytest
from mlxtend.frequent_patterns import apriori, association_rules
from numpy.testing import assert_raises as numpy_assert_raises

Expand Down Expand Up @@ -224,3 +225,9 @@ def test_on_df_with_missing_entries_support_only():

assert df_result['support'].shape == (18,)
assert int(np.isnan(df_result['support'].values).any()) != 1


def test_with_empty_dataframe():
df = df_freq_items_with_colnames.iloc[:0]
with pytest.raises(ValueError):
association_rules(df)