Skip to content

Commit

Permalink
Merge pull request #843 from rasbt/assoc-rule-warning
Browse files Browse the repository at this point in the history
Raise error on empty frequent itemset to association_rules
  • Loading branch information
rasbt authored Sep 2, 2021
2 parents 0a3eb5d + 47f8ace commit bf17ad5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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)

0 comments on commit bf17ad5

Please sign in to comment.