Skip to content

Commit 92deb43

Browse files
committed
BUG: Creating Index with the names argument
1 parent 78147e9 commit 92deb43

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

pandas/core/indexes/base.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,18 @@ class Index(IndexOpsMixin, PandasObject):
112112
dtype : NumPy dtype (default: object)
113113
copy : bool
114114
Make a copy of input ndarray
115-
name : object
115+
name : object, optional
116116
Name to be stored in the index
117+
names : sequence of objects, optional
118+
Names for the index levels
117119
tupleize_cols : bool (default: True)
118120
When True, attempt to create a MultiIndex if possible
119121
120122
Notes
121123
-----
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.
123127
124128
Examples
125129
--------
@@ -177,10 +181,21 @@ class Index(IndexOpsMixin, PandasObject):
177181
str = accessor.AccessorProperty(strings.StringMethods)
178182

179183
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):
186+
187+
if names is not None and name is not None:
188+
raise TypeError("Can provide only one of names and name arguments")
181189

182-
if name is None and hasattr(data, 'name'):
183-
name = data.name
190+
if names is not None and not is_list_like(names):
191+
raise TypeError("names must be list-like")
192+
193+
if name is None:
194+
if hasattr(data, 'name'):
195+
name = data.name
196+
# extract `name` from `names` in case MultiIndex cannot be created
197+
elif names:
198+
name = names[0]
184199

185200
if fastpath:
186201
return cls._simple_new(data, name)
@@ -359,8 +374,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
359374
# 10697
360375
if all(isinstance(e, tuple) for e in data):
361376
from .multi import MultiIndex
362-
return MultiIndex.from_tuples(
363-
data, names=name or kwargs.get('names'))
377+
return MultiIndex.from_tuples(data, names=names or name)
364378
# other iterable of some kind
365379
subarr = _asarray_tuplesafe(data, dtype=object)
366380
return Index(subarr, dtype=dtype, copy=copy, name=name, **kwargs)

pandas/tests/indexes/test_base.py

+24
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,30 @@ def test_constructor_simple_new(self):
305305
result = idx._simple_new(idx, 'obj')
306306
tm.assert_index_equal(result, idx)
307307

308+
def test_constructor_names(self):
309+
# test both `name` and `names` provided
310+
with pytest.raises(TypeError, match='only one of names and name'):
311+
idx = Index([1, 2, 3], name='a', names=('a',))
312+
313+
# test non-list-like `names`
314+
with pytest.raises(TypeError, match='names must be iterable'):
315+
idx = Index([1, 2, 3], names='a')
316+
317+
# test using `name` for a flat `Index`
318+
idx = Index([1, 2, 3], name='a')
319+
assert idx.name == 'a'
320+
assert idx.names == ('a',)
321+
322+
# test using `names` for a flat `Index`
323+
idx = Index([1, 2, 3], names=('a',))
324+
assert idx.name == 'a'
325+
assert idx.names == ('a',)
326+
327+
# test using `names` for a `MultiIndex`
328+
idx = Index([('A', 1), ('A', 2)], names=('a', 'b'))
329+
assert idx.name is None
330+
assert idx.names == ('a', 'b')
331+
308332
def test_constructor_dtypes(self):
309333

310334
for idx in [Index(np.array([1, 2, 3], dtype=int)),

0 commit comments

Comments
 (0)