Code Monkey home page Code Monkey logo

Comments (6)

rhshadrach avatar rhshadrach commented on July 20, 2024 1

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.

cc @jorisvandenbossche

from pandas.

Aloqeely avatar Aloqeely commented on July 20, 2024

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.

rhshadrach avatar rhshadrach commented on July 20, 2024

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.

asishm avatar asishm commented on July 20, 2024
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.

alexandermorgan avatar alexandermorgan commented on July 20, 2024

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.

gboeker avatar gboeker commented on July 20, 2024

take

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.