Skip to content

BUG: wide_to_long fails when stubname misses and i contains string type column #47757

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
merged 9 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ Reshaping
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)
- Bug in :meth:`DataFrame.pivot_table` with ``sort=False`` results in sorted index (:issue:`17041`)
- 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`)
- Bug in :meth:`wide_to_long` raises when ``stubnames`` is missing in columns and ``i`` contains string dtype column (:issue:`46044`)

Sparse
^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def melt(
for col in id_vars:
id_data = frame.pop(col)
if is_extension_array_dtype(id_data):
id_data = concat([id_data] * K, ignore_index=True)
if K > 0:
id_data = concat([id_data] * K, ignore_index=True)
else:
# We can't concat empty list. (GH 46044)
id_data = type(id_data)([], name=id_data.name, dtype=id_data.dtype)
else:
# error: Incompatible types in assignment (expression has type
# "ndarray[Any, dtype[Any]]", variable has type "Series")
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,3 +1086,27 @@ def test_warn_of_column_name_value(self):
with tm.assert_produces_warning(FutureWarning):
result = df.melt(id_vars="value")
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("dtype", ["O", "string"])
def test_missing_stubname(self, dtype):
# GH46044
df = DataFrame({"id": ["1", "2"], "a-1": [100, 200], "a-2": [300, 400]})
df = df.astype({"id": dtype})
result = wide_to_long(
df,
stubnames=["a", "b"],
i="id",
j="num",
sep="-",
)
index = pd.Index(
[("1", 1), ("2", 1), ("1", 2), ("2", 2)],
name=("id", "num"),
)
expected = DataFrame(
{"a": [100, 200, 300, 400], "b": [np.nan] * 4},
index=index,
)
new_level = expected.index.levels[0].astype(dtype)
expected.index = expected.index.set_levels(new_level, level=0)
tm.assert_frame_equal(result, expected)