Skip to content

Commit 523ab8c

Browse files
authored
DEPR: concat with mismatched length keys/objs (#52337)
* DEPR: concat with mismatched len(keys) and len(objs) * Fix+test iterators
1 parent 5e07960 commit 523ab8c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Deprecations
110110
- Deprecated :meth:`DataFrame._data` and :meth:`Series._data`, use public APIs instead (:issue:`33333`)
111111
- Deprecated :meth:`.Groupby.all` and :meth:`.GroupBy.any` with datetime64 or :class:`PeriodDtype` values, matching the :class:`Series` and :class:`DataFrame` deprecations (:issue:`34479`)
112112
- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`)
113+
- Deprecated the behavior of :func:`concat` with both ``len(keys) != len(objs)``, in a future version this will raise instead of truncating to the shorter of the two sequences (:issue:`43485`)
113114
- Deprecated the default of ``observed=False`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby`; this will default to ``True`` in a future version (:issue:`43999`)
114115
- Deprecated explicit support for subclassing :class:`Index` (:issue:`45289`)
115116
- Deprecated :meth:`DataFrameGroupBy.dtypes`, check ``dtypes`` on the underlying object instead (:issue:`51045`)
@@ -130,6 +131,7 @@ Deprecations
130131
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
131132
-
132133

134+
133135
.. ---------------------------------------------------------------------------
134136
.. _whatsnew_210.performance:
135137

pandas/core/reshape/concat.py

+16
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
cast,
1515
overload,
1616
)
17+
import warnings
1718

1819
import numpy as np
1920

2021
from pandas._config import using_copy_on_write
2122

2223
from pandas.util._decorators import cache_readonly
24+
from pandas.util._exceptions import find_stack_level
2325

26+
from pandas.core.dtypes.common import is_iterator
2427
from pandas.core.dtypes.concat import concat_compat
2528
from pandas.core.dtypes.generic import (
2629
ABCDataFrame,
@@ -437,6 +440,19 @@ def __init__(
437440
# #1649
438441
clean_keys = []
439442
clean_objs = []
443+
if is_iterator(keys):
444+
keys = list(keys)
445+
if is_iterator(objs):
446+
objs = list(objs)
447+
if len(keys) != len(objs):
448+
# GH#43485
449+
warnings.warn(
450+
"The behavior of pd.concat with len(keys) != len(objs) is "
451+
"deprecated. In a future version this will raise instead of "
452+
"truncating to the smaller of the two sequences",
453+
FutureWarning,
454+
stacklevel=find_stack_level(),
455+
)
440456
for k, v in zip(keys, objs):
441457
if v is None:
442458
continue

pandas/tests/reshape/concat/test_concat.py

+17
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,20 @@ def test_concat_ignore_empty_from_reindex():
785785
result = concat([df1, df2.reindex(columns=df1.columns)], ignore_index=True)
786786
expected = df1 = DataFrame({"a": [1, 2], "b": [pd.Timestamp("2012-01-01"), pd.NaT]})
787787
tm.assert_frame_equal(result, expected)
788+
789+
790+
def test_concat_mismatched_keys_length():
791+
# GH#43485
792+
ser = Series(range(5))
793+
sers = [ser + n for n in range(4)]
794+
keys = ["A", "B", "C"]
795+
796+
msg = r"The behavior of pd.concat with len\(keys\) != len\(objs\) is deprecated"
797+
with tm.assert_produces_warning(FutureWarning, match=msg):
798+
concat(sers, keys=keys, axis=1)
799+
with tm.assert_produces_warning(FutureWarning, match=msg):
800+
concat(sers, keys=keys, axis=0)
801+
with tm.assert_produces_warning(FutureWarning, match=msg):
802+
concat((x for x in sers), keys=(y for y in keys), axis=1)
803+
with tm.assert_produces_warning(FutureWarning, match=msg):
804+
concat((x for x in sers), keys=(y for y in keys), axis=0)

0 commit comments

Comments
 (0)