Skip to content

BUG: pd.wide_to_long can't handle stubname equal to suffix #46939

Open
@YMRTN

Description

@YMRTN

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame({"year1" : {0 : 4.5, 1 : 1.7, 2 : .8},
                   "year2" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "year3" : {0 : 2.6, 1 : 1.6, 2 : .6},
                   "year4" : {0 : 3.2, 1 : 1.3, 2 : .1},
                   "X"     : dict(zip(range(3), np.random.randn(3)))
                  })
df["id"] = df.index
pd.wide_to_long(df, stubnames="year", i="id", j="year")  # AttributeError: 'DataFrame' object has no attribute 'str'

Issue Description

pd.wide_to_long raises an AttributeError when the parameters stubnames and j (suffix) are equal. The AttributeError does not really tell you what's the problem.

Expected Behavior

I see two potential fixes:

  • Raise an AttributeError that tells you that stubnames and j can't be identical
  • Make it so that stubnames and j can be identical

Installed Versions

commit : 4bfe3d0
python : 3.9.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 1.4.2
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.2
setuptools : 57.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : 3.4.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

Labels

BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions