Skip to content

Commit a079db6

Browse files
committed
catch warnings
1 parent 1aa6a96 commit a079db6

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,24 @@ Set the following option to opt into the future behavior:
657657
658658
In [9]: pd.set_option("future.no_silent_downcasting", True)
659659
660+
Deprecated list-like-column-wise enlargement with partial-row-wise enlargement
661+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
662+
663+
Enlarging a dataframe with a partial row indexer and a list-like column indexer is deprecated.
664+
665+
Instead of
666+
667+
.. code-block:: python
668+
669+
df.loc[idx, ['A', 'B']] = (val_1, val_2)
670+
671+
please do
672+
673+
.. code-block:: python
674+
675+
df.loc[idx, 'A'] = val_1
676+
df.loc[idx, 'B'] = val_2
677+
660678
Other Deprecations
661679
^^^^^^^^^^^^^^^^^^
662680
- Changed :meth:`Timedelta.resolution_string` to return ``h``, ``min``, ``s``, ``ms``, ``us``, and ``ns`` instead of ``H``, ``T``, ``S``, ``L``, ``U``, and ``N``, for compatibility with respective deprecations in frequency aliases (:issue:`52536`)

pandas/core/indexing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,15 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:
879879
return
880880
if len(diff):
881881
warnings.warn(
882-
"Multi-column-wise row-wise expansion is deprecated and will be "
883-
"removed in a future version. ",
882+
(
883+
"list-like-column-wise enlargement with partial-row-wise "
884+
"enlargement is deprecated.\n"
885+
"Instead of\n"
886+
" df.loc[idx, ['A', 'B']] = (val_1, val_2)\n"
887+
"please do\n"
888+
" df.loc[idx, 'A'] = val_1\n"
889+
" df.loc[idx, 'B'] = val_2\n"
890+
),
884891
FutureWarning,
885892
stacklevel=find_stack_level(),
886893
)

pandas/tests/indexing/multiindex/test_loc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,15 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value):
377377
# GH#39147
378378
mi = MultiIndex.from_tuples([(1, 2), (3, 4)])
379379
df = DataFrame([[1, 2], [3, 4]], index=mi, columns=["a", "b"])
380-
df.loc[indexer, ["c", "d"]] = 1.0
380+
warn = FutureWarning if indexer != slice(None) else None
381+
with tm.assert_produces_warning(
382+
warn,
383+
match=(
384+
"list-like-column-wise enlargement with partial-row-wise enlargement "
385+
"is deprecated"
386+
),
387+
):
388+
df.loc[indexer, ["c", "d"]] = 1.0
381389
expected = DataFrame(
382390
[[1, 2, 1.0, 1.0], [3, 4, exp_value, exp_value]],
383391
index=mi,

pandas/tests/indexing/test_loc.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,14 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
940940
def test_loc_setitem_missing_columns(self, index, box, expected):
941941
# GH 29334
942942
df = DataFrame([[1, 2], [3, 4], [5, 6]], columns=["A", "B"])
943-
944-
df.loc[index] = box
943+
warn = FutureWarning if index[0] != slice(None, None, None) else None
944+
with tm.assert_produces_warning(
945+
warn,
946+
match=(
947+
"list-like-column-wise enlargement with partial-row-wise enlargement"
948+
),
949+
):
950+
df.loc[index] = box
945951
tm.assert_frame_equal(df, expected)
946952

947953
def test_loc_coercion(self):
@@ -3327,7 +3333,14 @@ def test_loc_setitem_dict_timedelta_multiple_set(self):
33273333
def test_loc_set_multiple_items_in_multiple_new_columns(self):
33283334
# GH 25594
33293335
df = DataFrame(index=[1, 2], columns=["a"])
3330-
df.loc[1, ["b", "c"]] = [6, 7]
3336+
with tm.assert_produces_warning(
3337+
FutureWarning,
3338+
match=(
3339+
"list-like-column-wise enlargement with partial-row-wise enlargement "
3340+
"is deprecated"
3341+
),
3342+
):
3343+
df.loc[1, ["b", "c"]] = [6, 7]
33313344

33323345
expected = DataFrame(
33333346
{

0 commit comments

Comments
 (0)