question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Series.reset_index(level_name, drop=True) accepts invalid name when index is flat

See original GitHub issue

Code Sample, a copy-pastable example if possible

In [2]: s = pd.Series(range(4))

In [3]: s.reset_index('wrong', drop=True)
Out[3]: 
0    0
1    1
2    2
3    3
dtype: int64

In [4]: s.reset_index('wrong')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-a7b872dfe0cf> in <module>()
----> 1 s.reset_index('wrong')

~/nobackup/repo/pandas/pandas/core/series.py in reset_index(self, level, drop, name, inplace)
   1215         else:
   1216             df = self.to_frame(name)
-> 1217             return df.reset_index(level=level, drop=drop)
   1218 
   1219     def __unicode__(self):

~/nobackup/repo/pandas/pandas/core/frame.py in reset_index(self, level, drop, inplace, col_level, col_fill)
   4098             if not isinstance(level, (tuple, list)):
   4099                 level = [level]
-> 4100             level = [self.index._get_level_number(lev) for lev in level]
   4101             if isinstance(self.index, MultiIndex):
   4102                 if len(level) < self.index.nlevels:

~/nobackup/repo/pandas/pandas/core/frame.py in <listcomp>(.0)
   4098             if not isinstance(level, (tuple, list)):
   4099                 level = [level]
-> 4100             level = [self.index._get_level_number(lev) for lev in level]
   4101             if isinstance(self.index, MultiIndex):
   4102                 if len(level) < self.index.nlevels:

~/nobackup/repo/pandas/pandas/core/indexes/base.py in _get_level_number(self, level)
   1944 
   1945     def _get_level_number(self, level):
-> 1946         self._validate_index_level(level)
   1947         return 0
   1948 

~/nobackup/repo/pandas/pandas/core/indexes/base.py in _validate_index_level(self, level)
   1941         elif level != self.name:
   1942             raise KeyError('Level %s must be same as name (%s)' %
-> 1943                            (level, self.name))
   1944 
   1945     def _get_level_number(self, level):

KeyError: 'Level wrong must be same as name (None)'

In [5]: s = pd.Series(range(4), index=pd.MultiIndex.from_product([[1, 2]]*2))

In [6]: s.reset_index('wrong', drop=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/nobackup/repo/pandas/pandas/core/indexes/multi.py in _get_level_number(self, level)
    775                                  'level number' % level)
--> 776             level = self.names.index(level)
    777         except ValueError:

ValueError: 'wrong' is not in list

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-e0d143f488d8> in <module>()
----> 1 s.reset_index('wrong', drop=True)

~/nobackup/repo/pandas/pandas/core/series.py in reset_index(self, level, drop, name, inplace)
   1199                 if not isinstance(level, (tuple, list)):
   1200                     level = [level]
-> 1201                 level = [self.index._get_level_number(lev) for lev in level]
   1202                 if len(level) < len(self.index.levels):
   1203                     new_index = self.index.droplevel(level)

~/nobackup/repo/pandas/pandas/core/series.py in <listcomp>(.0)
   1199                 if not isinstance(level, (tuple, list)):
   1200                     level = [level]
-> 1201                 level = [self.index._get_level_number(lev) for lev in level]
   1202                 if len(level) < len(self.index.levels):
   1203                     new_index = self.index.droplevel(level)

~/nobackup/repo/pandas/pandas/core/indexes/multi.py in _get_level_number(self, level)
    777         except ValueError:
    778             if not isinstance(level, int):
--> 779                 raise KeyError('Level %s not found' % str(level))
    780             elif level < 0:
    781                 level += self.nlevels

KeyError: 'Level wrong not found'

Problem description

In [3]: should raise too.

Expected Output

Same as In [4]:

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.5.3.final.0 python-bits: 64 OS: Linux OS-release: 4.9.0-6-amd64 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: it_IT.UTF-8 LOCALE: it_IT.UTF-8

pandas: 0.23.0.dev0+825.ga3facfc55 pytest: 3.5.0 pip: 9.0.1 setuptools: 39.0.1 Cython: 0.25.2 numpy: 1.14.1 scipy: 0.19.0 pyarrow: None xarray: None IPython: 6.2.1 sphinx: 1.5.6 patsy: 0.5.0 dateutil: 2.7.0 pytz: 2017.2 blosc: None bottleneck: 1.2.0dev 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.3.0 xlsxwriter: 0.9.6 lxml: 4.1.1 bs4: 4.5.3 html5lib: 0.999999999 sqlalchemy: 1.0.15 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: 0.2.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
toobazcommented, May 14, 2018

@KalyanGokhale no official protocol (we have so many bugs we don’t typically fight over them 😉 ), and might depend on how urgent the issue is, but I guess a rule of thumb could be “always feel free to ask if the person who signed up to work on an issue is actually/still working on it, and if s/he isn’t, or doesn’t reply within a week, then feel free to steal it”. (and in general, when you start working on an issue, always mention it)

0reactions
KalyanGokhalecommented, May 13, 2018

@misupova sincere apologies once again.

@toobaz may be I was quick to jump on this one - but wanted to understand for future if there is any protocol you guys follow (e.g. if x # of days have passed and the person who signed up to work on an issue has not provided any update, then its fair play for someone else to pick it up etc?) - is there a middle ground between the time an issue is open and someone who has a (ready/near ready) solution can also post it…may be there is another collaborative way which I have not considered…just thinking out loud. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas unable to reset index because name exist
I use reset_index but its giving me error that the name already exist. I dont want to use reset_index(drop=True) because i want to...
Read more >
pandas.Series.reset_index — pandas 1.5.2 documentation
Just reset the index, without inserting it as a column in the new DataFrame. nameobject, optional. The name to use for the column...
Read more >
Pandas reset index - How to reset the index and convert the ...
On applying the .reset_index() function, the index gets shifted to the dataframe as a separate column. It is named as index ...
Read more >
Reset index in pandas DataFrame - PYnative
1. We can create a DataFrame from a CSV file or dict. 2. When we manipulate the DataFrame like drop duplicates or sort...
Read more >
pandas: Reset index of DataFrame, Series with reset_index()
It is also used to delete the current index or return to the data column when using the row name (string) as an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found