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

Ability to internally store counts of each category in a CategoricalColumn #296

Merged
merged 5 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 8 additions & 9 deletions dataprofiler/profilers/categorical_column_profile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from . import BaseColumnProfiler
from .profiler_options import CategoricalOptions
from . import utils
Expand Down Expand Up @@ -29,7 +30,7 @@ def __init__(self, name, options=None):
raise ValueError("CategoricalColumn parameter 'options' must be of"
" type CategoricalOptions.")
super(CategoricalColumn, self).__init__(name)
self._categories = list()
self._categories = defaultdict(int)
self.__calculations = {}
self._filter_properties_w_options(self.__calculations, options)

Expand All @@ -49,8 +50,8 @@ def __add__(self, other):
other.__class__.__name__))

merged_profile = CategoricalColumn(None)
merged_profile._categories = self._categories.copy()
merged_profile._update_categories(other._categories)
merged_profile._categories = \
utils.add_nested_dictionaries(self._categories, other._categories)
BaseColumnProfiler._add_helper(merged_profile, self, other)
self._merge_calculations(merged_profile.__calculations,
self.__calculations,
Expand Down Expand Up @@ -82,7 +83,7 @@ def categories(self):
"""
Property for categories.
"""
return self._categories
return list(self._categories.keys())

@property
def unique_ratio(self):
Expand Down Expand Up @@ -126,11 +127,9 @@ def _update_categories(self, df_series, prev_dependent_properties=None,
:type df_series: pandas.DataFrame
:return: None
"""
if hasattr(df_series, 'tolist'):
df_series = df_series.tolist()

self._categories = utils._combine_unique_sets(
self._categories, df_series)
category_count = df_series.value_counts(dropna=False).to_dict()
self._categories = utils.add_nested_dictionaries(self._categories,
category_count)

def _update_helper(self, df_series_clean, profile):
"""
Expand Down
19 changes: 16 additions & 3 deletions dataprofiler/tests/profilers/test_categorical_column_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def test_categorical_mapping(self):
self.assertEqual(
num_nan_count, len(
column_profile.null_types_index["nan"]))

expected = {"abcd": 2, "aa": 2, "b": 1, "4": 1, "3": 1, "2": 2,
"dfd": 1}
self.assertDictEqual(expected, cat_profiler._categories)
num_null_types = 4
num_nan_count = 2
categories = pd.concat([df1, df2]).apply(str).unique().tolist()
Expand All @@ -127,6 +129,9 @@ def test_categorical_mapping(self):
self.assertEqual(num_null_types, len(column_profile.null_types))
self.assertEqual(
num_nan_count, len(column_profile.null_types_index["nan"]))
expected = {"abcd": 2, "aa": 3, "b": 2, "4": 1, "3": 1, "2": 2,
"dfd": 1, "1": 1, "ee": 2, "ff": 1, "gg": 1}
self.assertDictEqual(expected, cat_profiler._categories)

num_null_types = 4
num_nan_count = 3
Expand Down Expand Up @@ -191,9 +196,13 @@ def test_categorical_merge(self):
profile = CategoricalColumn("Name")
profile.update(df1)

expected_dict = {"abcd": 2, "aa": 2, "b": 1, "4": 1, "3": 1, "2": 2,
"dfd": 1, np.nan: 1}
self.assertDictEqual(expected_dict, profile._categories)

profile2 = CategoricalColumn("Name")
profile2.update(df2)

# Add profiles
profile3 = profile + profile2
self.assertCountEqual(expected_categories, profile3.categories)
Expand All @@ -202,6 +211,10 @@ def test_categorical_merge(self):
profile.sample_size +
profile2.sample_size)
self.assertEqual(profile3.is_match, False)
expected_dict = {"abcd": 2, "aa": 3, "b": 2, "4": 1, "3": 1, "2": 2,
np.nan: 1, "dfd": 1, "1": 1, "ee": 2, "ff": 1, "gg": 1,
"NaN": 1, "None": 1, "nan": 1, "null": 1}
self.assertDictEqual(expected_dict, profile3._categories)

# Add again
profile3 = profile + profile3
Expand Down Expand Up @@ -264,7 +277,7 @@ def test_greater_than_CATEGORICAL_THRESHOLD_DEFAULT_identify_as_text(self):
cat_sentence_df = pd.Series(cat_sentence_list)
column_profile = StructuredColProfiler(cat_sentence_df)
cat_profiler = column_profile.profiles['data_stats_profile']._profiles["category"]

self.assertEqual(False, cat_profiler.is_match)

def test_less_than_CATEGORICAL_THRESHOLD_DEFAULT(self):
Expand Down