Skip to content

Commit d093fae

Browse files
jahn96mroeschke
andauthored
ENH: pd.concat with keys and ignore_index=True should raise (#59398)
* Add enhancement to v3.0.0 * Raise error when combination of given values don't make sense * Add test * Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/core/reshape/concat.py Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/tests/reshape/concat/test_concat.py Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/tests/reshape/concat/test_concat.py Co-authored-by: Matthew Roeschke <[email protected]> * Fix test --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent ecc451d commit d093fae

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Other enhancements
5050
- :meth:`DataFrame.pivot_table` and :func:`pivot_table` now allow the passing of keyword arguments to ``aggfunc`` through ``**kwargs`` (:issue:`57884`)
5151
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
5252
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
53+
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
5354
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)
5455
- Restore support for reading Stata 104-format and enable reading 103-format dta files (:issue:`58554`)
5556
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)

pandas/core/reshape/concat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ def concat(
379379
0 1 2
380380
1 3 4
381381
"""
382+
if ignore_index and keys is not None:
383+
raise ValueError(
384+
f"Cannot set {ignore_index=} and specify keys. Either should be used."
385+
)
386+
382387
if copy is not lib.no_default:
383388
warnings.warn(
384389
"The copy keyword is deprecated and will be removed in a future "

pandas/tests/reshape/concat/test_concat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,14 @@ def test_concat_with_series_and_frame_returns_rangeindex_columns():
939939
result = concat([ser, df])
940940
expected = DataFrame([0, 1, 2], index=[0, 0, 1])
941941
tm.assert_frame_equal(result, expected, check_column_type=True)
942+
943+
944+
def test_concat_with_moot_ignore_index_and_keys():
945+
df1 = DataFrame([[0]])
946+
df2 = DataFrame([[42]])
947+
948+
ignore_index = True
949+
keys = ["df1", "df2"]
950+
msg = f"Cannot set {ignore_index=} and specify keys. Either should be used."
951+
with pytest.raises(ValueError, match=msg):
952+
concat([df1, df2], keys=keys, ignore_index=ignore_index)

0 commit comments

Comments
 (0)