Comments (6)
Thanks @asishm and @alexandermorgan. From the original issue:
For multi-index, you should provide a list.
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.
from pandas.
The method that raised the ValueError is Index._get_default_index_names
and it states that it only raises "if names not str or list-like", and a tuple is definitely list-like, I think it makes more sense to fix the behavior of _get_default_index_names
to accept tuples, rather than change the documentation
from pandas.
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."
@alexandermorgan - can you supply a reproducible example.
from pandas.
pd.DataFrame({'a': [1]}, index=pd.MultiIndex.from_tuples([(1,2)])).reset_index(names=('a', 'b'))
raises with ValueError: Index names must be str or 1-dimensional list
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
from pandas.
If we go with @Aloqeely's suggestion of changing Index._get_default_index_names, then instead of raising an error if names
is not a list, we could change the condition of the error block to be if not is_list_like(names)
. Here is where is_list_like seems to be defined. It's a little surprising that the 7 examples given in the docstring of is_list_like's behavior, neither tuples nor dictionaries are included, though the function returns True for both types.
from pandas.
take
from pandas.
Related Issues (20)
- BUG: next_workday observance wrongly adds BDay to holidays on weekdays HOT 2
- BUG: Unexpected behaviour when attempting to read Stata 104 format dta files
- BUG: RuntimeWarning: coroutine 'AsyncCursor.execute' was never awaited HOT 3
- BUG: pd.to_datetime() produces NaN when first row is a time in the current month HOT 4
- DOC: Enforce Numpy Docstring Validation | pandas.api.types HOT 1
- Default string dtype should not raise fallback performance warnings HOT 2
- BUG: `pd.Arrays.SparseArray` warning inconsistencies when contained in a `pd.DataFrame` (potential false positive)
- DOC: The conditions under which slicing with labels worked as expected were wrong. HOT 2
- BUG: pandas.errors.EmptyDataError
- BUG: Forcing an int dtype on DataFrame construction raises an odd error HOT 4
- BUG: passing non-trivial expressions to function within eval raises an error HOT 2
- ADMIN: Upgrade to mailman 3
- ENH: Allow `pd.set_option` to control the uuid4 seed used in `pd.io.formats.style.Renderer`
- BUG: Passing an empty Index to pd.DataFrame.from_records() produces a ValueError
- BUG: Adding or multiplying a pandas nullable dtype Series with a pyarrow dtype Series raises TypeError HOT 3
- BUG: Regression of Index.join() when return_indexers=True HOT 2
- BUG: Slicing operator has a regression on Python 3.12 for a dataframe with categorical MultiIndex HOT 4
- BUG: groupby.first() with None values fills None value with next row HOT 5
- BUG: ./ci/code_checks.sh fails in local dev environment HOT 1
- Default string dtype (PDEP-14): naming convention to distinguish the dtype variants HOT 27
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.