feature_engine icon indicating copy to clipboard operation
feature_engine copied to clipboard

feat: add `errors` parameter to CategoricalImputer for multimodal variables

Open direkkakkar319-ops opened this issue 8 months ago • 11 comments

Description

Fixes #904

Adds an errors parameter to CategoricalImputer to handle multimodal categorical variables gracefully, instead of always raising a ValueError.

Changes

  • Added errors parameter to CategoricalImputer.__init__() with options 'raise' (default), 'warn', and 'ignore'
  • Updated both single-variable and multi-variable branches in .fit() to respect the new parameter
  • When errors='warn', emits a UserWarning and imputes using the first most frequent category found
  • When errors='ignore', silently imputes using the first most frequent category found
  • Default errors='raise' preserves existing behaviour — no breaking changes
  • Updated docstring with full parameter documentation
  • Added tests covering all three errors values and invalid input
  • Added CHANGELOG.rst entry

Type of Change

  • [ ] Bug fix
  • [x] New feature (non-breaking)
  • [ ] Breaking change
  • [ ] Documentation update

Tests

All new and existing tests pass:

pytest tests/test_imputation/test_categorical_imputer.py

Notes

The existing error message ("contains multiple frequent categories") is preserved so no existing test matchers break.

Screenshot 2026-03-08 195813

direkkakkar319-ops avatar Mar 08 '26 14:03 direkkakkar319-ops

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 98.27%. Comparing base (f72a2b7) to head (6f5b4da). :warning: Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #908   +/-   ##
=======================================
  Coverage   98.27%   98.27%           
=======================================
  Files         116      116           
  Lines        4978     4988   +10     
  Branches      795      800    +5     
=======================================
+ Hits         4892     4902   +10     
  Misses         55       55           
  Partials       31       31           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Mar 08 '26 15:03 codecov[bot]

@solegalli facing issue with the 2 checks will work on this and move the PR for review in some time

direkkakkar319-ops avatar Mar 08 '26 15:03 direkkakkar319-ops

the test pass percentage has increased still below the set benchmark trying to resolve the problems

direkkakkar319-ops avatar Mar 08 '26 17:03 direkkakkar319-ops

Hi, @solegalli , I have made the implementations that you asked me to

also ran all the tests cases for just in case if there would an issue, all of them are passing Screenshot 2026-03-27 002840

direkkakkar319-ops avatar Mar 26 '26 19:03 direkkakkar319-ops

made formatting changes for passing the ci:test_style from this

            "city": ["London", "London", "Paris", "Paris", "Berlin", "Berlin", "Madrid"],
            "country": ["UK", "UK", "FR", "FR", "DE", "DE", "ES"],
            "one_mode": ["London", "London", "London", "Paris", "Paris", "Berlin", "Berlin"],

to

            "city": [
                "London", "London", "Paris", "Paris", "Berlin", "Berlin", "Madrid"
            ],
            "country": ["UK", "UK", "FR", "FR", "DE", "DE", "ES"],
            "one_mode": [
                "London", "London", "London", "Paris", "Paris", "Berlin", "Berlin"
            ],

direkkakkar319-ops avatar Mar 26 '26 19:03 direkkakkar319-ops

Hey @solegalli, the only failing checks are the Codecov coverage ones. Could you guide me on which test file I should add the coverage for the new errors='warn' and errors='raise' branches?

direkkakkar319-ops avatar Mar 26 '26 19:03 direkkakkar319-ops

Changes made

  1. Rename of errors to multimodal in categorical.py , test_categorical_imputer.py
  2. Added missing Unit test case test_warning_when_single_variable_in_list_is_multimodal in test_categorical_imputer.py

tests cases are passing locally image

direkkakkar319-ops avatar Mar 27 '26 14:03 direkkakkar319-ops

Hi, @solegalli the changes required in the comments you mentioned i will do then shortly

direkkakkar319-ops avatar Mar 27 '26 14:03 direkkakkar319-ops

Hi, @solegalli the changes required in the comments you mentioned i will do then shortly

Alright! No problem at all. Give me a shout when you are ready! Thanks a lot for your support.

solegalli avatar Mar 28 '26 12:03 solegalli

Hi, @solegalli , Changes have been added , now the pr is ready for your review

direkkakkar319-ops avatar Mar 28 '26 18:03 direkkakkar319-ops

hi, @solegalli , the changes are done and the checks are passing would appreciate a merge.

direkkakkar319-ops avatar Apr 07 '26 20:04 direkkakkar319-ops