Repository: pandas-dev/pandas
Pull request: ENH: pd.concat with keys and ignore_index=True should raise 
- [x] closes #59274 (Replace xxxx with the GitHub issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Problem statement:
ENH: pd.concat with keys and ignore_index=True should raise
### Feature Type

- [ ] Adding new functionality to pandas

- [X] Changing existing functionality in pandas

- [ ] Removing existing functionality in pandas


### Problem Description

You can happily pass both of these arguments together, but the ignore_index=True call drops the keys, so it ends up being a moot point:

```python
In [1]: import pandas as pd

In [2]: df1 = pd.DataFrame([[0]])

In [3]: df2 = pd.DataFrame([[42]])

In [4]: pd.concat([df1, df2])
Out[4]: 
    0
0   0
0  42

In [5]: pd.concat([df1, df2], keys=["df1", "df2"])
Out[5]: 
        0
df1 0   0
df2 0  42

In [6]: pd.concat([df1, df2], keys=["df1", "df2"], ignore_index=True)
Out[6]: 
    0
0   0
1  42
```

### Feature Description

pandas can raise that this combination of values does not make sense

### Alternative Solutions

status quo

### Additional Context

_No response_

Hints from issue discussion:
take
@WillAyd so kinda warning should be raised to warn this as counterproductive action
@Prabhat-Thapa45 rather than warning, I think the suggestion is that an informative error be raised
take

Commit message: Add test

Patch to review:
diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index e3c4e69db7cbd..5bd00e608077f 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -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 the combination of given arguments don't make sense (:issue:`59274`)
 - 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`)
 - Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py
index 6836ba3f65691..33fd9a6c8bc40 100644
--- a/pandas/core/reshape/concat.py
+++ b/pandas/core/reshape/concat.py
@@ -379,6 +379,12 @@ def concat(
     0   1   2
     1   3   4
     """
+    if ignore_index and keys is not None:
+        raise ValueError(
+            "Setting ignore_index to true and providing key values are "
+            "counterproductive. 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 "
diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py
index b2caa1fadd1a5..c3d28bab60dcc 100644
--- a/pandas/tests/reshape/concat/test_concat.py
+++ b/pandas/tests/reshape/concat/test_concat.py
@@ -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_combination_of_arguments():
+    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)


Code review comments:
diff_hunk: @@ -379,6 +379,12 @@ def concat(
     0   1   2
     1   3   4
     """
+    if ignore_index and keys is not None:
+        raise ValueError(
+            "Setting ignore_index to true and providing key values are "
+            "counterproductive. Either should be used."
path: pandas/core/reshape/concat.py
text: @user1:
```suggestion
            f"Cannot set {ignore_index=} and specify keys. Either should be used."
```

diff_hunk: @@ -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 the combination of given arguments don't make sense (:issue:`59274`)
path: doc/source/whatsnew/v3.0.0.rst
text: @user1:
```suggestion
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
```