@@ -113,14 +113,18 @@ class Index(IndexOpsMixin, PandasObject):
113
113
dtype : NumPy dtype (default: object)
114
114
copy : bool
115
115
Make a copy of input ndarray
116
- name : object
116
+ name : object, optional
117
117
Name to be stored in the index
118
+ names : sequence of objects, optional
119
+ Names for the index levels
118
120
tupleize_cols : bool (default: True)
119
121
When True, attempt to create a MultiIndex if possible
120
122
121
123
Notes
122
124
-----
123
- An Index instance can **only** contain hashable objects
125
+ An Index instance can **only** contain hashable objects.
126
+
127
+ Only one of `name` and `names` can be specified at the same time.
124
128
125
129
Examples
126
130
--------
@@ -176,10 +180,21 @@ class Index(IndexOpsMixin, PandasObject):
176
180
str = CachedAccessor ("str" , StringMethods )
177
181
178
182
def __new__ (cls , data = None , dtype = None , copy = False , name = None ,
179
- fastpath = False , tupleize_cols = True , ** kwargs ):
183
+ fastpath = False , tupleize_cols = True , names = None ,
184
+ ** kwargs ):
185
+
186
+ if names is not None and name is not None :
187
+ raise TypeError ("Can provide only one of names and name arguments" )
180
188
181
- if name is None and hasattr (data , 'name' ):
182
- name = data .name
189
+ if names is not None and not is_list_like (names ):
190
+ raise TypeError ("names must be list-like" )
191
+
192
+ if name is None :
193
+ if hasattr (data , 'name' ):
194
+ name = data .name
195
+ # extract `name` from `names` in case MultiIndex cannot be created
196
+ elif names :
197
+ name = names [0 ]
183
198
184
199
if fastpath :
185
200
return cls ._simple_new (data , name )
@@ -358,8 +373,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
358
373
# 10697
359
374
if all (isinstance (e , tuple ) for e in data ):
360
375
from .multi import MultiIndex
361
- return MultiIndex .from_tuples (
362
- data , names = name or kwargs .get ('names' ))
376
+ return MultiIndex .from_tuples (data , names = names or name )
363
377
# other iterable of some kind
364
378
subarr = _asarray_tuplesafe (data , dtype = object )
365
379
return Index (subarr , dtype = dtype , copy = copy , name = name , ** kwargs )
0 commit comments