Skip to content

Commit 9bf1e96

Browse files
GYHHAHAmroeschke
andauthored
BUG: wide_to_long fails when stubname misses and i contains string type column (pandas-dev#47757)
* Update test_melt.py * Update v1.5.0.rst * Update melt.py * Update test_melt.py * Update melt.py * fix type * Update doc/source/whatsnew/v1.5.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * Update melt.py * Update test_melt.py Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 3a39d25 commit 9bf1e96

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ Reshaping
10211021
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)
10221022
- Bug in :meth:`DataFrame.pivot_table` with ``sort=False`` results in sorted index (:issue:`17041`)
10231023
- Bug in :meth:`concat` when ``axis=1`` and ``sort=False`` where the resulting Index was a :class:`Int64Index` instead of a :class:`RangeIndex` (:issue:`46675`)
1024+
- Bug in :meth:`wide_to_long` raises when ``stubnames`` is missing in columns and ``i`` contains string dtype column (:issue:`46044`)
10241025

10251026
Sparse
10261027
^^^^^^

pandas/core/reshape/melt.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ def melt(
131131
for col in id_vars:
132132
id_data = frame.pop(col)
133133
if is_extension_array_dtype(id_data):
134-
id_data = concat([id_data] * K, ignore_index=True)
134+
if K > 0:
135+
id_data = concat([id_data] * K, ignore_index=True)
136+
else:
137+
# We can't concat empty list. (GH 46044)
138+
id_data = type(id_data)([], name=id_data.name, dtype=id_data.dtype)
135139
else:
136140
# error: Incompatible types in assignment (expression has type
137141
# "ndarray[Any, dtype[Any]]", variable has type "Series")

pandas/tests/reshape/test_melt.py

+24
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,27 @@ def test_warn_of_column_name_value(self):
10861086
with tm.assert_produces_warning(FutureWarning):
10871087
result = df.melt(id_vars="value")
10881088
tm.assert_frame_equal(result, expected)
1089+
1090+
@pytest.mark.parametrize("dtype", ["O", "string"])
1091+
def test_missing_stubname(self, dtype):
1092+
# GH46044
1093+
df = DataFrame({"id": ["1", "2"], "a-1": [100, 200], "a-2": [300, 400]})
1094+
df = df.astype({"id": dtype})
1095+
result = wide_to_long(
1096+
df,
1097+
stubnames=["a", "b"],
1098+
i="id",
1099+
j="num",
1100+
sep="-",
1101+
)
1102+
index = pd.Index(
1103+
[("1", 1), ("2", 1), ("1", 2), ("2", 2)],
1104+
name=("id", "num"),
1105+
)
1106+
expected = DataFrame(
1107+
{"a": [100, 200, 300, 400], "b": [np.nan] * 4},
1108+
index=index,
1109+
)
1110+
new_level = expected.index.levels[0].astype(dtype)
1111+
expected.index = expected.index.set_levels(new_level, level=0)
1112+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)