-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Add support to names keyword in Index #28032
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,8 +176,10 @@ class Index(IndexOpsMixin, PandasObject): | |
Otherwise, an error will be raised. | ||
copy : bool | ||
Make a copy of input ndarray | ||
name : object | ||
name : object, optional | ||
Name to be stored in the index | ||
names : tuple of objects, optional | ||
Names to be stored in the index (only accepts tuple of length 1) | ||
tupleize_cols : bool (default: True) | ||
When True, attempt to create a MultiIndex if possible | ||
|
||
|
@@ -259,12 +261,22 @@ def __new__( | |
dtype=None, | ||
copy=False, | ||
name=None, | ||
names=None, | ||
fastpath=None, | ||
tupleize_cols=True, | ||
**kwargs | ||
): | ||
|
||
if name is None and hasattr(data, "name"): | ||
if names is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not super familiar with this part of the code but is it possible to share the length of checking of >>> pd.MultiIndex.from_tuples((('a', 'b'), ('c', 'd')), names=('a', 'b', 'c'))
ValueError: Length of names must match number of levels in MultiIndex. So ideally could share that machinery here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a 100% sure I understood what you're saying here, but I removed the check for weather the data is a list-like. Is that it? |
||
if name is not None: | ||
raise TypeError("Using name and names is unsupported") | ||
elif names is not None and not is_list_like(names): | ||
raise TypeError("names must be list-like") | ||
elif len(names) > 1: | ||
raise TypeError("names must be list-like of size 1") | ||
# infer name from names when MultiIndex cannot be created | ||
name = names[0] | ||
elif hasattr(data, "name") and name is None: | ||
name = data.name | ||
|
||
if fastpath is not None: | ||
|
@@ -492,9 +504,7 @@ def __new__( | |
# 10697 | ||
from .multi import MultiIndex | ||
|
||
return MultiIndex.from_tuples( | ||
data, names=name or kwargs.get("names") | ||
) | ||
return MultiIndex.from_tuples(data, names=names or name) | ||
# other iterable of some kind | ||
subarr = com.asarray_tuplesafe(data, dtype=object) | ||
return Index(subarr, dtype=dtype, copy=copy, name=name, **kwargs) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to the end (before kwargs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this!