Code Sample, a copy-pastable example if possible
import pandas as pd
df = pd.DataFrame({'x': [1., 5., 3.]}, index=[0, 2, 4])
df.index.name = 'frame'
df.reset_index('frame')
Problem description
Resetting the index on pandas 0.20.1 and python 2.7 results in the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\envs\tpissue\lib\site-packages\pandas\core\frame.py", line 3055, in reset_index
if level is None or i in level:
TypeError: 'in <string>' requires string as left operand, not int
In the example, it doesn't matter if you use Unicode or non-unicode strings. (using from __future__ import unicode_literals
). Ref https://github.com/soft-matter/trackpy/issues/446 @manuescobedo
Expected Output
A reindexed DataFrame.
Output of pd.show_versions()
Comment From: gfyoung
I believe you are incorrectly using the reset_index
method. The label
you pass in MUST belong to the index of the DataFrame
. In the context of your example, that means DataFrame.index.name
must be "frame"
per the documentation here.
That being said, this error message makes no sense, so we can improve on that so that it won't be as confusing next. Would you like to update / improve it?
Comment From: caspervdw
@gfyoung Thanks for the fast reply. I think I might have simplified the code example too much. Sorry about that. The same exception is there if I execute the following script:
import pandas as pd
df = pd.DataFrame({'x': [1., 5., 3.]}, index=[0, 2, 4])
df.index.name = 'frame'
df.reset_index('frame')
Specifying the name of the only indexer does work on pandas 0.13/py27, 0.16/py27, 0.19/py35, and 0.20.1/py35.
Comment From: jreback
these are as expected. You cannot reset a named index that doesn't exist.
Comment From: caspervdw
@jreback The named index does exist, I made a mistake in my first code example. For clarity, I now edited my first message.
Comment From: gfyoung
@caspervdw : I can't reproduce the error. The code works perfectly fine on Python 2.7 and 3.6 (using 0.20.2
though). Can you double-check this?
Comment From: gfyoung
Also @jreback , I'd like to keep this open, as I would like to see if we can improve the error message
Comment From: caspervdw
Thanks @gfyoung for reopening. I can have a look a the error message for sure, but first I want to see where my issue is coming from.
@manuescobedo brought this up at https://github.com/soft-matter/trackpy/issues/446. We use CI and never encountered this, however with the specific configuration Py27, pandas 0.20.1, I can reproduce. @manuescobedo, could you try to reproduce your error using my minimal example above?
When I have more time I'll try to dive a bit deeper into this. I'll let you know if I find anything.
Comment From: gfyoung
Thanks @jreback for reopening.
Ummm...that was me in fact 😉
Also, try upgrading pandas
to 0.20.2
, I can't reproduce the issue there. I can however, confirm that that was (we're past that release now) on 0.20.1
.
Comment From: caspervdw
I can confirm that the issue disappears on 0.20.2
. If I try to reindex using a non-existing index name, I get TypeError: Level name must be same as name '(None)'
, which is OK I think. So the issue can be closed for my part.
Comment From: jreback
this was fixed by https://github.com/pandas-dev/pandas/pull/16266