Skip to content

Commit 797732a

Browse files
authored
CLN: Make non-empty **kwargs in Index.__new__ fail instead of silently dropped (#29625)
1 parent ab5731f commit 797732a

File tree

6 files changed

+37
-23
lines changed

6 files changed

+37
-23
lines changed

pandas/core/indexes/base.py

+12-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,9 @@ 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+
raise TypeError(f"Unexpected keyword arguments {set(kwargs)!r}")
456+
return cls._simple_new(subarr, name, **kwargs)
454457

455458
elif hasattr(data, "__array__"):
456459
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)
@@ -3391,7 +3394,7 @@ def _reindex_non_unique(self, target):
33913394
new_indexer = np.arange(len(self.take(indexer)))
33923395
new_indexer[~check] = -1
33933396

3394-
new_index = self._shallow_copy_with_infer(new_labels, freq=None)
3397+
new_index = self._shallow_copy_with_infer(new_labels)
33953398
return new_index, indexer, new_indexer
33963399

33973400
# --------------------------------------------------------------------
@@ -4254,7 +4257,13 @@ def _concat_same_dtype(self, to_concat, name):
42544257
Concatenate to_concat which has the same class.
42554258
"""
42564259
# must be overridden in specific classes
4257-
klasses = (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ExtensionArray)
4260+
klasses = (
4261+
ABCDatetimeIndex,
4262+
ABCTimedeltaIndex,
4263+
ABCPeriodIndex,
4264+
ExtensionArray,
4265+
ABCIntervalIndex,
4266+
)
42584267
to_concat = [
42594268
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
42604269
]

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 arguments {'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
@@ -350,6 +350,11 @@ def test_constructor_simple_new(self, vals, dtype):
350350
result = index._simple_new(index.values, dtype)
351351
tm.assert_index_equal(result, index)
352352

353+
def test_constructor_wrong_kwargs(self):
354+
# GH #19348
355+
with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"):
356+
Index([], foo="bar")
357+
353358
@pytest.mark.parametrize(
354359
"vals",
355360
[

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 arguments {'freq'}"
494+
with pytest.raises(TypeError, match=msg):
495+
pd.Index(data, freq="M")
498496

499497
def test_insert_index_complex128(self):
500498
pass

pandas/tests/io/excel/test_readers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ def test_excel_passes_na_filter(self, read_ext, na_filter):
893893
def test_unexpected_kwargs_raises(self, read_ext, arg):
894894
# gh-17964
895895
kwarg = {arg: "Sheet1"}
896-
msg = "unexpected keyword argument `{}`".format(arg)
896+
msg = r"unexpected keyword argument `{}`".format(arg)
897897

898898
with pd.ExcelFile("test1" + read_ext) as excel:
899899
with pytest.raises(TypeError, match=msg):

0 commit comments

Comments
 (0)