-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: add from_dummies #31795
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
Closed
Closed
ENH: add from_dummies #31795
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
159d54e
first draft
MarcoGorelli 3bfafe6
rename columns to prefix
MarcoGorelli 15f6818
fix docstring
MarcoGorelli 9eed071
black
MarcoGorelli f5d9088
infer prefixes
4be43af
lint
9b111b7
update test_api
cfa74ce
add see also and versionadded
3827e01
remove mytest
5cf2ab8
fix docstring validation
6fd7513
Merge branch 'master' into from-dummies
MarcoGorelli 2a6b471
Merge remote-tracking branch 'upstream/master' into from-dummies
MarcoGorelli 8ffb0ba
type signature, simplify validation
MarcoGorelli 2e2cb57
don't blacken pandas/__init__
MarcoGorelli b6e575e
Merge remote-tracking branch 'upstream/master' into from-dummies
MarcoGorelli d2916d9
revert taking check_len out of get_dummies
MarcoGorelli 6a84569
reword comment
MarcoGorelli 5efee29
put space at end of string, rather than beginning
MarcoGorelli 1bf16c9
put space at end of string
MarcoGorelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ | |
get_dummies, | ||
cut, | ||
qcut, | ||
from_dummies, | ||
) | ||
|
||
import pandas.api | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype, expected_dict", | ||
[ | ||
("str", {"col1": ["a", "a", "b"]}), | ||
(str, {"col1": ["a", "a", "b"]},), | ||
(None, {"col1": ["a", "a", "b"]}), | ||
MarcoGorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
) | ||
def test_dtype(dtype, expected_dict): | ||
df = pd.DataFrame({"col1_a": [1, 1, 0], "col1_b": [0, 0, 1]}) | ||
result = pd.from_dummies(df, dtype=dtype) | ||
expected = pd.DataFrame(expected_dict) | ||
if dtype is None: | ||
expected = expected.astype("category") | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_malformed(): | ||
df = pd.DataFrame({"col1_a": [1, 1, 0], "col1_b": [1, 0, 1]}) | ||
msg = ( | ||
"Data cannot be decoded! Each row must contain only 0s and 1s" | ||
", and each row may have at most one 1" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
pd.from_dummies(df) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"prefix_sep, input_dict", | ||
[ | ||
("_", {"col1_a": [1, 1, 0], "col1_b": [0, 0, 1]}), | ||
("*", {"col1*a": [1, 1, 0], "col1*b": [0, 0, 1]}), | ||
(".", {"col1.a": [1, 1, 0], "col1.b": [0, 0, 1]}), | ||
], | ||
) | ||
def test_prefix_sep(prefix_sep, input_dict): | ||
df = pd.DataFrame(input_dict) | ||
result = pd.from_dummies(df, prefix_sep=prefix_sep) | ||
expected = pd.DataFrame({"col1": ["a", "a", "b"]}, dtype="category") | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_no_prefix(): | ||
df = pd.DataFrame({"a": [1, 1, 0], "b": [0, 0, 1]}) | ||
result = pd.from_dummies(df, prefix="letter") | ||
expected = pd.DataFrame({"letter": ["a", "a", "b"]}, dtype="category") | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_multiple_columns(): | ||
df = pd.DataFrame( | ||
{"col1_a": [1, 0], "col1_b": [0, 1], "col2_a": [0, 0], "col2_c": [1, 1]} | ||
) | ||
result = pd.from_dummies(df) | ||
expected = pd.DataFrame({"col1": ["a", "b"], "col2": ["c", "c"]}, dtype="category") | ||
tm.assert_frame_equal(result, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.