-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Pandas get_dummies validate columns
input
#28463
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
Changes from 9 commits
87fa4ea
09a4f5e
ef2d577
faa8f07
81c2597
3aa8749
af99037
2353462
7d68181
ef1b9cd
38f13f9
4da4149
aea5019
9cd8d40
f266f47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -608,6 +608,69 @@ def test_get_dummies_all_sparse(self): | |
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"values", | ||
[ | ||
["baz", "zoo"], | ||
np.array(["baz", "zoo"]), | ||
pd.Series(["baz", "zoo"]), | ||
pd.Index(["baz", "zoo"]), | ||
], | ||
) | ||
def test_get_dummies_with_list_like_values(self, values): | ||
# issue #28383 | ||
df = pd.DataFrame( | ||
{ | ||
"bar": [1, 2, 3, 4, 5, 6], | ||
"foo": ["one", "one", "one", "two", "two", "two"], | ||
"baz": ["A", "B", "C", "A", "B", "C"], | ||
"zoo": ["x", "y", "z", "q", "w", "t"], | ||
} | ||
) | ||
|
||
result = pd.get_dummies(df, columns=values, dtype="int64") | ||
|
||
data = [ | ||
[1, "one", 1, 0, 0, 0, 0, 0, 1, 0, 0], | ||
[2, "one", 0, 1, 0, 0, 0, 0, 0, 1, 0], | ||
[3, "one", 0, 0, 1, 0, 0, 0, 0, 0, 1], | ||
[4, "two", 1, 0, 0, 1, 0, 0, 0, 0, 0], | ||
[5, "two", 0, 1, 0, 0, 0, 1, 0, 0, 0], | ||
[6, "two", 0, 0, 1, 0, 1, 0, 0, 0, 0], | ||
] | ||
columns = [ | ||
"bar", | ||
"foo", | ||
"baz_A", | ||
"baz_B", | ||
"baz_C", | ||
"zoo_q", | ||
"zoo_t", | ||
"zoo_w", | ||
"zoo_x", | ||
"zoo_y", | ||
"zoo_z", | ||
] | ||
expected = DataFrame(data=data, columns=columns) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("values", ["baz", "zoo"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these two values testing meaningfully distinct cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought of adding 2 different values just for the check but they are testing the same case. I will remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only one now! |
||
def test_get_dummies_with_string_values(self, values): | ||
# issue #28383 | ||
df = pd.DataFrame( | ||
{ | ||
"bar": [1, 2, 3, 4, 5, 6], | ||
"foo": ["one", "one", "one", "two", "two", "two"], | ||
"baz": ["A", "B", "C", "A", "B", "C"], | ||
"zoo": ["x", "y", "z", "q", "w", "t"], | ||
} | ||
) | ||
|
||
msg = "Input must be a list-like for parameter `columns`" | ||
|
||
with pytest.raises(TypeError, match=msg): | ||
pd.get_dummies(df, columns=values) | ||
|
||
|
||
class TestCategoricalReshape: | ||
def test_reshaping_multi_index_categorical(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this testing new behavior? Are we not already testing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the file
reshape.py
there was no check on dtype passed to one of the parameterscolumns
and so on the issue it was mentioned add this check. Hence, after adding the test I added these tests specifically tocolumns
parameter. And regarding doubt whether this test was already present, I checked all tests again and there was no test to check the inputs to parametercolumns
and so I referred to a similar test as mentioned in PR and added it. Please let me know if you spotted a similar test forget_dummies
which I might have missed. I hope this clears it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like
pandas/pandas/tests/reshape/test_reshape.py
Line 90 in 367670e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line you are have pointed to does not check for string inputs to the
columns
parameters also it does not test for other list-like objects passed to it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this test seems superfluous; this PR should just check for invalid values in columns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove this test. It would be great help if could you elaborate/point me to a similar test for my reference? Because as far i understand if the value is not list-like the error message will be raised because of this PR and if invalid value (i.e. value not in data columns) is passed to columns then error will be raised because value will not be present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is redundant with
pandas/pandas/tests/reshape/test_reshape.py
Line 90 in 367670e
We want to keep your test
test_get_dummies_with_string_values
, which is testing the code you've added in this PR.