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

[BUG] Extend fill_empty's column_names type range #1014

Merged
merged 5 commits into from
Feb 10, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [ENH] Add `xlsx_table`, for reading tables from an Excel sheet. @samukweku
- [ENH] minor improvements for conditional_join; equality only joins are no longer supported; there has to be at least one non-equi join present. @samukweku
- [BUG] `sort_column_value_order` no longer mutates original dataframe.
- [BUG] Extend `fill_empty`'s `column_names` type range. Issue #998. @Zeroto521

## [v0.22.0] - 2021-11-21

Expand Down
3 changes: 2 additions & 1 deletion janitor/functions/fill.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Iterable as abcIterable
from enum import Enum
from operator import methodcaller
from typing import Hashable, Iterable, Union
Expand Down Expand Up @@ -145,7 +146,7 @@ def fill_empty(
return _fill_empty(df, column_names, value=value)


@dispatch(pd.DataFrame, (list, tuple))
@dispatch(pd.DataFrame, abcIterable)
def _fill_empty(df, column_names, value=None):
"""Fill empty function for the case that column_names is list or tuple."""
fill_mapping = {c: value for c in column_names}
Expand Down
20 changes: 20 additions & 0 deletions tests/functions/test_fill_empty.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import pandas as pd


@pytest.mark.functions
Expand All @@ -11,3 +12,22 @@ def test_fill_empty(null_df):
def test_fill_empty_column_string(null_df):
df = null_df.fill_empty(column_names="2", value=3)
assert set(df.loc[:, "2"]) == set([3])


@pytest.mark.functions
@pytest.mark.parametrize(
"column_names",
[
(0, 1, "2", "3"), # tuple
[0, 1, "2", "3"], # list
{0, 1, "2", "3"}, # set
({0: 0, 1: 1, "2": "2", "3": "3"}).keys(), # dict key
({0: 0, 1: 1, "2": "2", "3": "3"}).values(), # dict value
pd.Index([0, 1, "2", "3"]), # Index
],
)
def test_column_names_iterable_type(null_df, column_names):
result = null_df.fill_empty(column_names=column_names, value=3)
excepted = null_df.fillna(3)

assert result.equals(excepted)