-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
melt does not recognize numeric column names #29718
Comments
This looks like a bug to me. Thanks for the report! Investigations and PR's welcome. |
So concerning investigation: the root cause is that >>> np.ravel(["string", 1])
array(['string', '1'], dtype='<U5') will give an Concerning PRs: the check for having the columns could also be implemented via just using the I see it was added here fba641f to cope with multiindex. Maybe there is another way to do this? |
Maybe use diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py
index 16c044548..2d3a40fdb 100644
--- a/pandas/core/reshape/melt.py
+++ b/pandas/core/reshape/melt.py
@@ -10,6 +10,7 @@ from pandas.core.dtypes.generic import ABCMultiIndex
from pandas.core.dtypes.missing import notna
from pandas.core.arrays import Categorical
+import pandas.core.common as com
from pandas.core.frame import _shared_docs
from pandas.core.indexes.base import Index
from pandas.core.reshape.concat import concat
@@ -45,7 +46,7 @@ def melt(
else:
# Check that `id_vars` are in frame
id_vars = list(id_vars)
- missing = Index(np.ravel(id_vars)).difference(cols)
+ missing = Index(com.flatten(id_vars)).difference(cols)
if not missing.empty:
raise KeyError(
"The following 'id_vars' are not present" Which seems to make things work: In [1]: import pandas as pd
In [2]: df = pd.DataFrame(columns=[1, "string"])
In [3]: pd.melt(df, id_vars=[1, "string"])
Out[3]:
Empty DataFrame
Columns: [1, string, variable, value]
Index: [] |
Thanks for fix this bug!! Thank God |
Code Sample, a copy-pastable example if possible
Problem description
The shown example fails with
and I guess the reason is that the call of
in
pd.melt
somehow casts the numerical column name1
to the string"1"
.I am not sure if this is intended behavior or if the case of numerical column names is just not supported, but at least in older pandas versions (e.g. 0.23.4) this still worked.
Thanks for looking into this! I am also fine if this is closed with "won't fix" :-)
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-65-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 41.6.0
Cython : None
pytest : 5.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.9.0
pandas_datareader: 0.8.1
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.2
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
The text was updated successfully, but these errors were encountered: