Skip to content

Commit 1f2e456

Browse files
authored
[ENH] Fix check_column to support single inputs (#811)
* fix check_column to support single inputs; change sklearn dep to scikit-learn * add entry to changelog and fix dupe changelog entry
1 parent 87ee618 commit 1f2e456

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

.requirements/base.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
natsort
55
# seaborn
66
pandas_flavor
7-
sklearn
7+
scikit-learn
88
multipledispatch

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
- [ENH] Add `dropna` parameter to groupby_agg. @samukweku
66
- [ENH] `complete` adds a `by` parameter to expose explicit missing values per group, via groupby. @samukweku
7-
8-
- [ENH] Add `dropna` parameter to groupby_agg. @samukweku
9-
- [ENH] `complete` adds a `by` parameter to expose explicit missing values per group, via groupby. @samukweku
7+
- [ENH] Fix check_column to support single inputs - fixes `label_encode`. @zbarry
108

119
## [v0.20.13] - 2021-02-25
1210

janitor/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def check(varname: str, value, expected_types: list):
4242
4343
check('x', x, [int, float])
4444
45-
:param varname: The name of the variable.
45+
:param varname: The name of the variable (for diagnostic error message).
4646
:param value: The value of the varname.
4747
:param expected_types: The types we expect the item to be.
4848
:raises TypeError: if data is not the expected type.
@@ -360,6 +360,9 @@ def check_column(
360360
in df.columns.
361361
:raises ValueError: if data is not the expected type.
362362
"""
363+
if isinstance(column_names, str) or not isinstance(column_names, Iterable):
364+
column_names = [column_names]
365+
363366
for column_name in column_names:
364367
if present and column_name not in df.columns: # skipcq: PYL-R1720
365368
raise ValueError(

tests/utils/test_check_column.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ def test_check_column(dataframe):
1111
assert check_column(dataframe, ["a"]) is None
1212

1313

14+
@pytest.mark.utils
15+
def test_check_column_single(dataframe):
16+
"""
17+
Check works with a single input
18+
"""
19+
20+
assert check_column(dataframe, "a") is None
21+
22+
with pytest.raises(ValueError):
23+
check_column(dataframe, "b")
24+
25+
# should also work with non-string inputs
26+
27+
with pytest.raises(ValueError):
28+
check_column(dataframe, 2)
29+
30+
dataframe[2] = "asdf"
31+
32+
assert check_column(dataframe, 2) is None
33+
34+
1435
@pytest.mark.utils
1536
def test_check_column_absent_column(dataframe):
1637
"""

0 commit comments

Comments
 (0)