Skip to content

ENH: pd.concat with keys and ignore_index=True should raise #59398

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 9 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Other enhancements
- :meth:`DataFrame.pivot_table` and :func:`pivot_table` now allow the passing of keyword arguments to ``aggfunc`` through ``**kwargs`` (:issue:`57884`)
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)
- Restore support for reading Stata 104-format and enable reading 103-format dta files (:issue:`58554`)
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ def concat(
0 1 2
1 3 4
"""
if ignore_index and keys is not None:
raise ValueError(
f"Cannot set {ignore_index=} and specify keys. Either should be used."
)

if copy is not lib.no_default:
warnings.warn(
"The copy keyword is deprecated and will be removed in a future "
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,15 @@ def test_concat_with_series_and_frame_returns_rangeindex_columns():
result = concat([ser, df])
expected = DataFrame([0, 1, 2], index=[0, 0, 1])
tm.assert_frame_equal(result, expected, check_column_type=True)


def test_concat_with_moot_ignore_index_and_keys):
df1 = DataFrame([[0]])
df2 = DataFrame([[42]])

msg = (
"Setting ignore_index to true and providing key values are "
"counterproductive. Either should be used."
)
with pytest.raises(ValueError, match=msg):
concat([df1, df2], keys=["df1", "df2"], ignore_index=True)
Loading