BUG: reset_index() Fails on a Series/DataFrame Object that has "False" (bool type) as the Index Name #38147
Labels
Index
Related to the Index class or subclasses
Needs Tests
Unit test(s) needed to prevent regressions
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
[] (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Stacktrace:
~\Miniconda3\envs\vectorbt\lib\site-packages\pandas\core\series.py in reset_index(self, level, drop, name, inplace)
1340 else:
1341 df = self.to_frame(name)
-> 1342 return df.reset_index(level=level, drop=drop)
1343
1344 # ----------------------------------------------------------------------
~\Miniconda3\envs\vectorbt\lib\site-packages\pandas\core\frame.py in reset_index(self, level, drop, inplace, col_level, col_fill)
4602 # to ndarray and maybe infer different dtype
4603 level_values = _maybe_casted_values(lev, lab)
-> 4604 new_obj.insert(0, name, level_values)
4605
4606 new_obj.index = new_index
~\Miniconda3\envs\vectorbt\lib\site-packages\pandas\core\frame.py in insert(self, loc, column, value, allow_duplicates)
3494 self._ensure_valid_index(value)
3495 value = self._sanitize_column(column, value, broadcast=False)
-> 3496 self._data.insert(loc, column, value, allow_duplicates=allow_duplicates)
3497
3498 def assign(self, **kwargs) -> "DataFrame":
~\Miniconda3\envs\vectorbt\lib\site-packages\pandas\core\internals\managers.py in insert(self, loc, item, value, allow_duplicates)
1171 if not allow_duplicates and item in self.items:
1172 # Should this be a different kind of error??
-> 1173 raise ValueError(f"cannot insert {item}, already exists")
1174
1175 if not isinstance(loc, int):
ValueError: cannot insert False, already exists
Problem description
Pandas index names support multiple object types as opposed to just
str
. I presume that the intention was that an index name is never anything other than a string, but I can only speculate. Interestingly enough, I did not encounter this issue on a DataFrame. This leads me to believe that the issue has to do withI originally encountered this bug when loading data from a csv that had a header of
False
. Perhaps a clean enough work-around is to convert headers to strings when loading from any file type?Expected Output
The index reset without the ValueError. It should match
df1
's output.Output of
pd.show_versions()
pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0.post20200712
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.3.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.50.1
The text was updated successfully, but these errors were encountered: