Skip to content

Commit 340f3ca

Browse files
R1j1tproost
authored andcommitted
Pandas get_dummies validate columns input (pandas-dev#28463)
1 parent dfeb9a9 commit 340f3ca

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ Reshaping
387387
- Bug in :func:`merge`, did not append suffixes correctly with MultiIndex (:issue:`28518`)
388388
- :func:`qcut` and :func:`cut` now handle boolean input (:issue:`20303`)
389389
- Fix to ensure all int dtypes can be used in :func:`merge_asof` when using a tolerance value. Previously every non-int64 type would raise an erroneous ``MergeError`` (:issue:`28870`).
390+
- Better error message in :func:`get_dummies` when `columns` isn't a list-like value (:issue:`28383`)
390391

391392
Sparse
392393
^^^^^^

pandas/core/reshape/reshape.py

+2
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,8 @@ def get_dummies(
864864
# determine columns being encoded
865865
if columns is None:
866866
data_to_encode = data.select_dtypes(include=dtypes_to_encode)
867+
elif not is_list_like(columns):
868+
raise TypeError("Input must be a list-like for parameter `columns`")
867869
else:
868870
data_to_encode = data[columns]
869871

pandas/tests/reshape/test_reshape.py

+17
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,23 @@ def test_get_dummies_all_sparse(self):
608608
)
609609
tm.assert_frame_equal(result, expected)
610610

611+
@pytest.mark.parametrize("values", ["baz"])
612+
def test_get_dummies_with_string_values(self, values):
613+
# issue #28383
614+
df = pd.DataFrame(
615+
{
616+
"bar": [1, 2, 3, 4, 5, 6],
617+
"foo": ["one", "one", "one", "two", "two", "two"],
618+
"baz": ["A", "B", "C", "A", "B", "C"],
619+
"zoo": ["x", "y", "z", "q", "w", "t"],
620+
}
621+
)
622+
623+
msg = "Input must be a list-like for parameter `columns`"
624+
625+
with pytest.raises(TypeError, match=msg):
626+
pd.get_dummies(df, columns=values)
627+
611628

612629
class TestCategoricalReshape:
613630
def test_reshaping_multi_index_categorical(self):

0 commit comments

Comments
 (0)