Repository: pandas-dev/pandas
Pull request: TST: new test for incorrect series assignment
- [x] closes #14525
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Problem statement:
Inconsistent error raising in indexing
#### A small, complete example of the issue

``` python
x = np.random.randn(2, 2)
y = pd.Series(range(2))
y.loc[range(2)] = x # succeeds
y.loc[:] = x # fails
```
#### Expected Output

Since we're assigning a 2D array to a 1D vector that is smaller than the raveled array, I would expect both assignments to fail.

Hints from issue discussion:
sure if you want to jump into the deep end on indexing. yes this is prob not being checked.

Looks like both the previously successful assignment now fails. Could use a test.

```
In [118]: y.loc[range(2)] = x

ValueError: shape mismatch: value array of shape (2,2) could not be broadcast to indexing result of shape (2,)

In [119]: pd.__version__
Out[119]: '0.26.0.dev0+652.g30362ed82'
```

Commit message: change test name

Patch to review:
diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py
index 4673dabca811b..8067b40a925e6 100644
--- a/pandas/tests/series/indexing/test_indexing.py
+++ b/pandas/tests/series/indexing/test_indexing.py
@@ -391,6 +391,22 @@ def test_setslice(datetime_series):
     assert sl.index.is_unique is True
 
 
+def test_2d_to_1d_assignment_error_raised():
+    x = np.random.randn(2, 2)
+    y = pd.Series(range(2))
+
+    msg = (
+        r"shape mismatch: value array of shape \(2,2\) could not be"
+        r" broadcast to indexing result of shape \(2,\)"
+    )
+    with pytest.raises(ValueError, match=msg):
+        y.loc[range(2)] = x
+
+    msg = r"could not broadcast input array from shape \(2,2\) into shape \(2\)"
+    with pytest.raises(ValueError, match=msg):
+        y.loc[:] = x
+
+
 # FutureWarning from NumPy about [slice(None, 5).
 @pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning")
 def test_basic_getitem_setitem_corner(datetime_series):


Code review comments:
diff_hunk: @@ -391,6 +391,22 @@ def test_setslice(datetime_series):
     assert sl.index.is_unique is True
 
 
+def test_2d_to_1d_assignment_error_raised():
path: pandas/tests/series/indexing/test_indexing.py
text: @user1:
```suggestion
def test_2d_to_1d_assignment_raises():
```

@author:
Thanks @user1 - good to know