-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Wrong error from "reset_index()" when columns are MultiIndex and index name is incomplete column name #16120
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
Comments
yep, must not be very well tested. |
By the way: df = pd.DataFrame(index=range(2), columns=pd.MultiIndex.from_tuples([['A', 'a'], ['B', 'b']]))
df.index.name = ('C', 'c')
df.reset_index() ... also fails, but I guess it's not supported (otherwise, I will open a new issue). |
that's not supported. I suppose it could be but is pretty odd thing to do. |
Would it be useful to provide a |
actually u can do this with col_level i think we should evaluate those args though |
@jreback We could make By the way, if we do want the df.set_index(df.columns[0]).reset_index() if |
certainly not. This is exactly according to specs. This only takes a scalar or a list, you passed a tuple, which is inherently ambiguous.
|
OK, I follow you, but not your example that follows: you pass a list, so according to your argument it should work, shouldn't it? (in fact, while I agree that |
that was a typo. I guess in theory it should work, but what does setting a it mean to set a Multi-Index named column as a single column. Then you have names as tuples.
this gets really complicated really fast and I don't see utility here. |
OK, indeed I'm not pushing too strong on this. But if we do agree on implementing @jorisvandenbossche 's suggestion that for instance df.reset_index(name=('C', 'c')) should interpret |
I actually don't think adding |
Well, without the discussion of whether a tuple should be interpreted in case of a MultiIndex or not (as I agree with Pietro that it would be logical that it would work the same with the existing index name), a possible Consider eg this dataframe:
If you want to reset the index and convert it in a column with a certain name, you could instead of this
do it in a slightly shorter way:
So it is mainly convenience, and the ability to do it in a chain |
So I argued already before about this :-) #6878 |
Code Sample, a copy-pastable example if possible
Problem description
Clearly
% item
should be replaced with% (item,)
.Expected Output
cannot insert ('B', ''), already exists
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.7.0-1-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: it_IT.utf8
LOCALE: it_IT.UTF-8
pandas: 0.20.0rc1+7.gf8b25c282
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
xarray: 0.9.1
IPython: 5.1.0.dev
sphinx: 1.4.9
patsy: 0.3.0-dev
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.6
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: 0.2.1
The text was updated successfully, but these errors were encountered: