Skip to content

Commit 34d9839

Browse files
DGradyjreback
authored andcommitted
BUG: Handle variables named 'name' in get_dummies, #12180
closes #12180 Author: Daniel Grady <[email protected]> Closes #12192 from DGrady/issue-12180 and squashes the following commits: aa19dbf [Daniel Grady] BUG: Handle variables named 'name' in get_dummies, #12180
1 parent de46056 commit 34d9839

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ Bug Fixes
521521
- Bug in ``read_csv`` when reading from a ``StringIO`` in threads (:issue:`11790`)
522522
- Bug in not treating ``NaT`` as a missing value in datetimelikes when factorizing & with ``Categoricals`` (:issue:`12077`)
523523
- Bug in getitem when the values of a ``Series`` were tz-aware (:issue:`12089`)
524+
- Bug in ``Series.str.get_dummies`` when one of the variables was 'name' (:issue:`12180`)
524525

525526

526527

pandas/core/strings.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,11 @@ def _wrap_result(self, result, use_codes=True, name=None):
11051105

11061106
if not hasattr(result, 'ndim'):
11071107
return result
1108-
name = name or getattr(result, 'name', None) or self._orig.name
11091108

11101109
if result.ndim == 1:
1110+
# Wait until we are sure result is a Series or Index before
1111+
# checking attributes (GH 12180)
1112+
name = name or getattr(result, 'name', None) or self._orig.name
11111113
if isinstance(self._orig, Index):
11121114
# if result is a boolean np.array, return the np.array
11131115
# instead of wrapping it into a boolean Index (GH 8875)

pandas/tests/test_strings.py

+8
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,14 @@ def test_get_dummies(self):
812812
idx = Index(['a|b', 'a|c', 'b|c'])
813813
idx.str.get_dummies('|')
814814

815+
# GH 12180
816+
# Dummies named 'name' should work as expected
817+
s = Series(['a', 'b,name', 'b'])
818+
result = s.str.get_dummies(',')
819+
expected = DataFrame([[1, 0, 0], [0, 1, 1], [0, 1, 0]],
820+
columns=['a', 'b', 'name'])
821+
tm.assert_frame_equal(result, expected)
822+
815823
def test_join(self):
816824
values = Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h'])
817825
result = values.str.split('_').str.join('_')

0 commit comments

Comments
 (0)