Repository: pandas-dev/pandas

Issue:
BUG: User-facing AssertionError with add column to SparseDataFrame
see also #5289

#### Code Sample, a copy-pastable example if possible

```python
import numpy as np
import pandas as pd
data = {'A': [np.nan, np.nan, np.nan, 0, 1, 2, 3, 4, 5, 6],
        'B': [0, 1, 2, np.nan, np.nan, np.nan, 3, 4, 5, 6],
        'C': np.arange(10, dtype=np.float64),
        'D': [0, 1, 2, 3, 4, 5, np.nan, np.nan, np.nan, np.nan]}
dates = pd.bdate_range('1/1/2011', periods=10)
float_frame = pd.SparseDataFrame(data, index=dates, default_kind='block')
N = len(float_frame)
float_frame['foo'] = np.random.randn(N - 1)
```
#### Problem description

```python-traceback
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-0a4f051f0d7a> in <module>
      8 float_frame = pd.SparseDataFrame(data, index=dates, default_kind='block')
      9 N = len(float_frame)
---> 10 float_frame['foo'] = np.random.randn(N - 1)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   3117         else:
   3118             # set column
-> 3119             self._set_item(key, value)
   3120 
   3121     def _setitem_slice(self, key, value):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
   3192 
   3193         self._ensure_valid_index(value)
-> 3194         value = self._sanitize_column(key, value)
   3195         NDFrame._set_item(self, key, value)
   3196 

~\Anaconda3\lib\site-packages\pandas\core\sparse\frame.py in _sanitize_column(self, key, value, **kwargs)
    418             else:
    419                 if len(value) != len(self.index):
--> 420                     raise AssertionError('Length of values does not match '
    421                                          'length of index')
    422                 clean = sp_maker(value)

AssertionError: Length of values does not match length of index
```

#### Expected Output

```python-traceback
ValueError: Length of values does not match length of index
```
#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
That makes sense.  `SparseDataFrame` is getting deprecated, but in the mean time, let's keep it user-friendly until it gets removed.

Resolving patch:
diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py
--- a/pandas/core/sparse/frame.py
+++ b/pandas/core/sparse/frame.py
@@ -430,8 +430,8 @@ def sp_maker(x, index=None):
 
         elif isinstance(value, SparseArray):
             if len(value) != len(self.index):
-                raise AssertionError('Length of values does not match '
-                                     'length of index')
+                raise ValueError('Length of values does not match '
+                                 'length of index')
             clean = value
 
         elif hasattr(value, '__iter__'):
@@ -441,8 +441,8 @@ def sp_maker(x, index=None):
                     clean = sp_maker(clean)
             else:
                 if len(value) != len(self.index):
-                    raise AssertionError('Length of values does not match '
-                                         'length of index')
+                    raise ValueError('Length of values does not match '
+                                     'length of index')
                 clean = sp_maker(value)
 
         # Scalar


Test patch:
diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py
--- a/pandas/tests/sparse/frame/test_frame.py
+++ b/pandas/tests/sparse/frame/test_frame.py
@@ -568,8 +568,9 @@ def _check_frame(frame, orig):
             assert len(frame['I'].sp_values) == N // 2
 
             # insert ndarray wrong size
-            msg = "Length of values does not match length of index"
-            with pytest.raises(AssertionError, match=msg):
+            # GH 25484
+            msg = 'Length of values does not match length of index'
+            with pytest.raises(ValueError, match=msg):
                 frame['foo'] = np.random.randn(N - 1)
 
             # scalar value