Skip to content

Commit f0f8f43

Browse files
csmcallistervictor
authored and
victor
committed
BUG:reorder type check/conversion so wide_to_long handles str arg for stubnames. GH22468 (pandas-dev#22490)
1 parent 0ebf318 commit f0f8f43

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ Reshaping
802802
- Bug in :meth:`DataFrame.replace` raises ``RecursionError`` when replacing empty lists (:issue:`22083`)
803803
- Bug in :meth:`Series.replace` and meth:`DataFrame.replace` when dict is used as the ``to_replace`` value and one key in the dict is is another key's value, the results were inconsistent between using integer key and using string key (:issue:`20656`)
804804
- Bug in :meth:`DataFrame.drop_duplicates` for empty ``DataFrame`` which incorrectly raises an error (:issue:`20516`)
805+
- Bug in :func:`pandas.wide_to_long` when a string is passed to the stubnames argument and a column name is a substring of that stubname (:issue:`22468`)
805806

806807
Build Changes
807808
^^^^^^^^^^^^^

pandas/core/reshape/melt.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,14 @@ def melt_stub(df, stub, i, j, value_vars, sep):
409409

410410
return newdf.set_index(i + [j])
411411

412-
if any(col in stubnames for col in df.columns):
413-
raise ValueError("stubname can't be identical to a column name")
414-
415412
if not is_list_like(stubnames):
416413
stubnames = [stubnames]
417414
else:
418415
stubnames = list(stubnames)
419416

417+
if any(col in stubnames for col in df.columns):
418+
raise ValueError("stubname can't be identical to a column name")
419+
420420
if not is_list_like(i):
421421
i = [i]
422422
else:

pandas/tests/reshape/test_melt.py

+21
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,24 @@ def test_float_suffix(self):
640640
result = wide_to_long(df, ['result', 'treatment'],
641641
i='A', j='colname', suffix='[0-9.]+', sep='_')
642642
tm.assert_frame_equal(result, expected)
643+
644+
def test_col_substring_of_stubname(self):
645+
# GH22468
646+
# Don't raise ValueError when a column name is a substring
647+
# of a stubname that's been passed as a string
648+
wide_data = {'node_id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
649+
'A': {0: 0.80, 1: 0.0, 2: 0.25, 3: 1.0, 4: 0.81},
650+
'PA0': {0: 0.74, 1: 0.56, 2: 0.56, 3: 0.98, 4: 0.6},
651+
'PA1': {0: 0.77, 1: 0.64, 2: 0.52, 3: 0.98, 4: 0.67},
652+
'PA3': {0: 0.34, 1: 0.70, 2: 0.52, 3: 0.98, 4: 0.67}
653+
}
654+
wide_df = pd.DataFrame.from_dict(wide_data)
655+
expected = pd.wide_to_long(wide_df,
656+
stubnames=['PA'],
657+
i=['node_id', 'A'],
658+
j='time')
659+
result = pd.wide_to_long(wide_df,
660+
stubnames='PA',
661+
i=['node_id', 'A'],
662+
j='time')
663+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)