Skip to content

CLN: Make non-empty **kwargs in Index.__new__ fail instead of silently dropped #29625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
ABCDatetimeArray,
ABCDatetimeIndex,
ABCIndexClass,
ABCIntervalIndex,
ABCMultiIndex,
ABCPandasArray,
ABCPeriodIndex,
Expand Down Expand Up @@ -450,7 +451,9 @@ def __new__(
return PeriodIndex(subarr, name=name, **kwargs)
except IncompatibleFrequency:
pass
return cls._simple_new(subarr, name)
if kwargs:
raise TypeError(f"Unexpected keyword arguments {set(kwargs)!r}")
return cls._simple_new(subarr, name, **kwargs)

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

new_index = self._shallow_copy_with_infer(new_labels, freq=None)
new_index = self._shallow_copy_with_infer(new_labels)
return new_index, indexer, new_indexer

# --------------------------------------------------------------------
Expand Down Expand Up @@ -4254,7 +4257,13 @@ def _concat_same_dtype(self, to_concat, name):
Concatenate to_concat which has the same class.
"""
# must be overridden in specific classes
klasses = (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ExtensionArray)
klasses = (
ABCDatetimeIndex,
ABCTimedeltaIndex,
ABCPeriodIndex,
ExtensionArray,
ABCIntervalIndex,
)
to_concat = [
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
]
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/multi/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,11 @@ def test_create_index_existing_name(idx):
("qux", "two"),
],
dtype="object",
),
names=["foo", "bar"],
)
)
tm.assert_index_equal(result, expected)

result = pd.Index(index, names=["A", "B"])
result = pd.Index(index, name="A")
Copy link
Contributor Author

@topper-123 topper-123 Nov 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both result and expected are normal Index instances and instantiating with a names parameter should fail.

expected = Index(
Index(
[
Expand All @@ -627,7 +626,7 @@ def test_create_index_existing_name(idx):
],
dtype="object",
),
names=["A", "B"],
name="A",
)
tm.assert_index_equal(result, expected)

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexes/multi/test_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def test_identical(idx):
assert mi.identical(mi2)

mi3 = Index(mi.tolist(), names=mi.names)
mi4 = Index(mi.tolist(), names=mi.names, tupleize_cols=False)
msg = r"Unexpected keyword arguments {'names'}"
with pytest.raises(TypeError, match=msg):
Index(mi.tolist(), names=mi.names, tupleize_cols=False)
mi4 = Index(mi.tolist(), tupleize_cols=False)
assert mi.identical(mi3)
assert not mi.identical(mi4)
assert mi.equals(mi4)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ def test_constructor_simple_new(self, vals, dtype):
result = index._simple_new(index.values, dtype)
tm.assert_index_equal(result, index)

def test_constructor_wrong_kwargs(self):
# GH #19348
with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"):
Index([], foo="bar")

@pytest.mark.parametrize(
"vals",
[
Expand Down
26 changes: 12 additions & 14 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,22 +479,20 @@ def test_insert_index_period(self, insert, coerced_val, coerced_dtype):
obj = pd.PeriodIndex(["2011-01", "2011-02", "2011-03", "2011-04"], freq="M")
assert obj.dtype == "period[M]"

data = [
pd.Period("2011-01", freq="M"),
coerced_val,
pd.Period("2011-02", freq="M"),
pd.Period("2011-03", freq="M"),
pd.Period("2011-04", freq="M"),
]
if isinstance(insert, pd.Period):
index_type = pd.PeriodIndex
exp = pd.PeriodIndex(data, freq="M")
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
else:
index_type = pd.Index

exp = index_type(
[
pd.Period("2011-01", freq="M"),
coerced_val,
pd.Period("2011-02", freq="M"),
pd.Period("2011-03", freq="M"),
pd.Period("2011-04", freq="M"),
],
freq="M",
)
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
msg = r"Unexpected keyword arguments {'freq'}"
with pytest.raises(TypeError, match=msg):
pd.Index(data, freq="M")

def test_insert_index_complex128(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ def test_excel_passes_na_filter(self, read_ext, na_filter):
def test_unexpected_kwargs_raises(self, read_ext, arg):
# gh-17964
kwarg = {arg: "Sheet1"}
msg = "unexpected keyword argument `{}`".format(arg)
msg = r"unexpected keyword argument `{}`".format(arg)

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