Skip to content

Commit af7b800

Browse files
committed
make non-empty kwargs fail in Index.__new__
1 parent 71b7868 commit af7b800

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

pandas/core/indexes/base.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
ABCDatetimeArray,
5555
ABCDatetimeIndex,
5656
ABCIndexClass,
57+
ABCIntervalIndex,
5758
ABCMultiIndex,
5859
ABCPandasArray,
5960
ABCPeriodIndex,
@@ -450,7 +451,13 @@ def __new__(
450451
return PeriodIndex(subarr, name=name, **kwargs)
451452
except IncompatibleFrequency:
452453
pass
453-
return cls._simple_new(subarr, name)
454+
if kwargs:
455+
if len(kwargs) == 1:
456+
msg = f"Unexpected keyword argument {list(kwargs)[0]!r}"
457+
else:
458+
msg = f"Unexpected keyword arguments {set(kwargs)!r}"
459+
raise TypeError(msg)
460+
return cls._simple_new(subarr, name, **kwargs)
454461

455462
elif hasattr(data, "__array__"):
456463
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)
@@ -3391,7 +3398,7 @@ def _reindex_non_unique(self, target):
33913398
new_indexer = np.arange(len(self.take(indexer)))
33923399
new_indexer[~check] = -1
33933400

3394-
new_index = self._shallow_copy_with_infer(new_labels, freq=None)
3401+
new_index = self._shallow_copy_with_infer(new_labels)
33953402
return new_index, indexer, new_indexer
33963403

33973404
# --------------------------------------------------------------------
@@ -4254,7 +4261,13 @@ def _concat_same_dtype(self, to_concat, name):
42544261
Concatenate to_concat which has the same class.
42554262
"""
42564263
# must be overridden in specific classes
4257-
klasses = (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ExtensionArray)
4264+
klasses = (
4265+
ABCDatetimeIndex,
4266+
ABCTimedeltaIndex,
4267+
ABCPeriodIndex,
4268+
ExtensionArray,
4269+
ABCIntervalIndex,
4270+
)
42584271
to_concat = [
42594272
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
42604273
]

pandas/tests/indexes/multi/test_constructor.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,11 @@ def test_create_index_existing_name(idx):
609609
("qux", "two"),
610610
],
611611
dtype="object",
612-
),
613-
names=["foo", "bar"],
612+
)
614613
)
615614
tm.assert_index_equal(result, expected)
616615

617-
result = pd.Index(index, names=["A", "B"])
616+
result = pd.Index(index, name="A")
618617
expected = Index(
619618
Index(
620619
[
@@ -627,7 +626,7 @@ def test_create_index_existing_name(idx):
627626
],
628627
dtype="object",
629628
),
630-
names=["A", "B"],
629+
name="A",
631630
)
632631
tm.assert_index_equal(result, expected)
633632

pandas/tests/indexes/multi/test_equivalence.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ def test_identical(idx):
146146
assert mi.identical(mi2)
147147

148148
mi3 = Index(mi.tolist(), names=mi.names)
149-
mi4 = Index(mi.tolist(), names=mi.names, tupleize_cols=False)
149+
msg = r"Unexpected keyword argument 'names'"
150+
with pytest.raises(TypeError, match=msg):
151+
Index(mi.tolist(), names=mi.names, tupleize_cols=False)
152+
mi4 = Index(mi.tolist(), tupleize_cols=False)
150153
assert mi.identical(mi3)
151154
assert not mi.identical(mi4)
152155
assert mi.equals(mi4)

pandas/tests/indexes/test_base.py

+5
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ def test_constructor_simple_new(self, vals, dtype):
354354
result = index._simple_new(index.values, dtype)
355355
tm.assert_index_equal(result, index)
356356

357+
def test_constructor_wrong_kwargs(self):
358+
# GH #19348
359+
with pytest.raises(TypeError, match="Unexpected keyword argument 'foo'"):
360+
Index([], foo="bar")
361+
357362
@pytest.mark.parametrize(
358363
"vals",
359364
[

pandas/tests/indexing/test_coercion.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -479,22 +479,20 @@ def test_insert_index_period(self, insert, coerced_val, coerced_dtype):
479479
obj = pd.PeriodIndex(["2011-01", "2011-02", "2011-03", "2011-04"], freq="M")
480480
assert obj.dtype == "period[M]"
481481

482+
data = [
483+
pd.Period("2011-01", freq="M"),
484+
coerced_val,
485+
pd.Period("2011-02", freq="M"),
486+
pd.Period("2011-03", freq="M"),
487+
pd.Period("2011-04", freq="M"),
488+
]
482489
if isinstance(insert, pd.Period):
483-
index_type = pd.PeriodIndex
490+
exp = pd.PeriodIndex(data, freq="M")
491+
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
484492
else:
485-
index_type = pd.Index
486-
487-
exp = index_type(
488-
[
489-
pd.Period("2011-01", freq="M"),
490-
coerced_val,
491-
pd.Period("2011-02", freq="M"),
492-
pd.Period("2011-03", freq="M"),
493-
pd.Period("2011-04", freq="M"),
494-
],
495-
freq="M",
496-
)
497-
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
493+
msg = r"Unexpected keyword argument 'freq'"
494+
with pytest.raises(TypeError, match=msg):
495+
pd.Index(data, freq="M")
498496

499497
def test_insert_index_complex128(self):
500498
pass

0 commit comments

Comments
 (0)