-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add dtype argument to StringMethods get_dummies() #59577
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
Merged
rhshadrach
merged 28 commits into
pandas-dev:main
from
aaronchucarroll:stringmethods-get-dummies
Sep 9, 2024
Merged
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e6f9527
Add prefix, prefix_sep, dummy_na, and dtype args to StringMethods get…
aaronchucarroll dafb61d
Fix import issue
aaronchucarroll bb79ef2
Fix typing of dtype
aaronchucarroll 24be84f
Fix NaN type issue
aaronchucarroll 09b2fad
Support categorical string backend
aaronchucarroll 50ed90c
Fix dtype type hints
aaronchucarroll 9e95485
Add dtype to get_dummies docstring
aaronchucarroll 9a47768
Fix get_dummies dtype docstring
aaronchucarroll 0c94bff
Merge branch 'main' into stringmethods-get-dummies
aaronchucarroll 9702bf7
remove changes for unnecessary args
aaronchucarroll 8793516
Merge branch 'stringmethods-get-dummies' of https://github.com/aaronc…
aaronchucarroll bad1038
Merge branch 'main' into stringmethods-get-dummies
aaronchucarroll 163fe09
parametrize dtype tests
aaronchucarroll 3d75fdc
Merge branch 'stringmethods-get-dummies' of https://github.com/aaronc…
aaronchucarroll d68bece
support pyarrow and nullable dtypes
aaronchucarroll c2aa7d5
Merge branch 'main' into stringmethods-get-dummies
aaronchucarroll 0fd2401
fix pyarrow import error
aaronchucarroll 920c865
skip pyarrow tests when not present
aaronchucarroll 800f787
split pyarrow tests
aaronchucarroll d8149e6
Merge branch 'main' into stringmethods-get-dummies
aaronchucarroll 6cbc3e8
parametrize pyarrow tests
aaronchucarroll 532e139
change var name to dummies_dtype
aaronchucarroll cd5c2ab
fix string issue
aaronchucarroll 822b3f4
consolidate conditionals
aaronchucarroll ba05a8d
add tests for str and pyarrow strings
aaronchucarroll 37dddb8
skip pyarrow string tests if not present
aaronchucarroll 6fbe183
add info to whatsnew doc
aaronchucarroll 87a1ee8
change func to meth in doc info
aaronchucarroll 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
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
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 |
---|---|---|
|
@@ -32,22 +32,94 @@ def test_get_dummies_index(): | |
tm.assert_index_equal(result, expected) | ||
|
||
|
||
def test_get_dummies_with_name_dummy(any_string_dtype): | ||
# GH 12180 | ||
# Dummies named 'name' should work as expected | ||
s = Series(["a", "b,name", "b"], dtype=any_string_dtype) | ||
result = s.str.get_dummies(",") | ||
expected = DataFrame([[1, 0, 0], [0, 1, 1], [0, 1, 0]], columns=["a", "b", "name"]) | ||
def test_get_dummies_int8_dtype(): | ||
s = Series(["1|2", "1|3", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.int8) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("123"), dtype=np.int8 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.int8).all() | ||
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. No need to assert this, it is covered by |
||
|
||
|
||
def test_get_dummies_with_name_dummy_index(): | ||
# GH 12180 | ||
# Dummies named 'name' should work as expected | ||
idx = Index(["a|b", "name|c", "b|name"]) | ||
result = idx.str.get_dummies("|") | ||
def test_get_dummies_uint8_dtype(): | ||
s = Series(["a|b", "a|c", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.uint8) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"), dtype=np.uint8 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.uint8).all() | ||
|
||
expected = MultiIndex.from_tuples( | ||
[(1, 1, 0, 0), (0, 0, 1, 1), (0, 1, 0, 1)], names=("a", "b", "c", "name") | ||
|
||
def test_get_dummies_int16_dtype(): | ||
s = Series(["a|b", "a|c", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.int16) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"), dtype=np.int16 | ||
) | ||
tm.assert_index_equal(result, expected) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.int16).all() | ||
|
||
|
||
def test_get_dummies_uint16_dtype(): | ||
s = Series(["a|b", "a|c", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.uint16) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"), dtype=np.uint16 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.uint16).all() | ||
|
||
|
||
def test_get_dummies_int32_dtype(): | ||
s = Series(["x|y", "x|z", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.int32) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("xyz"), dtype=np.int32 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.int32).all() | ||
|
||
|
||
def test_get_dummies_uint32_dtype(): | ||
s = Series(["x|y", "x|z", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.uint32) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("xyz"), dtype=np.uint32 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.uint32).all() | ||
|
||
|
||
def test_get_dummies_int64_dtype(): | ||
s = Series(["foo|bar", "foo|baz", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.int64) | ||
expected = DataFrame( | ||
[[1, 0, 1], [0, 1, 1], [0, 0, 0]], columns=["bar", "baz", "foo"], dtype=np.int64 | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.int64).all() | ||
|
||
|
||
def test_get_dummies_uint64_dtype(): | ||
s = Series(["foo|bar", "foo|baz", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=np.uint64) | ||
expected = DataFrame( | ||
[[1, 0, 1], [0, 1, 1], [0, 0, 0]], | ||
columns=["bar", "baz", "foo"], | ||
dtype=np.uint64, | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == np.uint64).all() | ||
|
||
|
||
def test_get_dummies_bool_dtype(): | ||
s = Series(["a|b", "a|c", np.nan], dtype="string") | ||
result = s.str.get_dummies("|", dtype=bool) | ||
expected = DataFrame( | ||
[[True, True, False], [True, False, True], [False, False, False]], | ||
columns=["a", "b", "c"], | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
assert (result.dtypes == bool).all() |
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.
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.
Can you parametrize these tests with
dtype
.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 addition, can you add
dtype=str
, PyArrow, and nullable dtypes (e.g. "Int64"). Specifying PyArrow and nullable dtypes currently fails:but is successful with
pd.get_dummies
I think this will need to be fixed. You may find it necessary to have multiple tests - perhaps one for NumPy (which are already present), one for
str
, one for PyArrow etc. But just try to consolidate withpytest.parametrize
as much as is reasonable.