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