-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: DataFrame.reset_index names param can't be a tuple as docs state #57994
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
The method that raised the ValueError is |
@alexandermorgan - can you supply a reproducible example. |
pd.DataFrame({'a': [1]}, index=pd.MultiIndex.from_tuples([(1,2)])).reset_index(names=('a', 'b')) raises with File ~/asishm/pandas/core/frame.py:6144, in DataFrame.reset_index(self, level, drop, inplace, col_level, col_fill, allow_duplicates, names)
6141 to_insert: Iterable[tuple[Any, Any | None]]
6143 default = "index" if "index" not in self else "level_0"
-> 6144 names = self.index._get_default_index_names(names, default)
6146 if isinstance(self.index, MultiIndex):
6147 to_insert = zip(self.index.levels, self.index.codes)
File ~/asishm/pandas/core/indexes/base.py:1727, in Index._get_default_index_names(self, names, default)
1724 names = [names]
1726 if not isinstance(names, list) and names is not None:
-> 1727 raise ValueError("Index names must be str or 1-dimensional list")
1729 if not names:
1730 if isinstance(self, MultiIndex):
ValueError: Index names must be str or 1-dimensional list |
If we go with @Aloqeely's suggestion of changing Index._get_default_index_names, then instead of raising an error if |
Thanks @asishm and @alexandermorgan. From the original issue:
and the corresponding PR, it seems clear to me the original intention was to require a list. I think we should update the docs here. |
take |
Pandas version checks
main
hereLocation of the documentation
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html
Documentation problem
In the description of the
names
parameter, the declared type in bold is correct: "int, str or 1-dimensional list, default None"But the expanded description that immediately follows incorrectly states that
names
can be a tuple: "If the DataFrame has a MultiIndex, this has to be a list or tuple with length equal to the number of levels."This is incorrect because passing a tuple as the
names
parameter raises this error: 'raise ValueError("Index names must be str or 1-dimensional list")'Suggested fix for documentation
Remove " or tuple" from the expanded documentation description. This will cause it to match the bold-face type description, and more importantly accurately reflect the behavior. I can submit a PR for this if that's preferred.
The text was updated successfully, but these errors were encountered: