From 835d6f0420674ce2d2cf2fa2f2b98007e5a80a7b Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 12 Apr 2019 15:12:03 +0200 Subject: [PATCH] BUG: User-facing AssertionError with add column to SparseDataFrame (#25503) --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/core/sparse/frame.py | 8 ++++---- pandas/tests/sparse/frame/test_frame.py | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index e2ae7cfc0cc34..7b87f6a7f8d3c 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -402,7 +402,7 @@ Sparse - Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`) - Bug in :class:`SparseFrame` constructor where passing ``None`` as the data would cause ``default_fill_value`` to be ignored (:issue:`16807`) -- +- Bug in `SparseDataFrame` when adding a column in which the length of values does not match length of index, ``AssertionError`` is raised instead of raising ``ValueError`` (:issue:`25484`) Other diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py index d7fe6d837f04d..08729442e701f 100644 --- 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 diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py index 954dd85f16c28..45df08ccfeb48 100644 --- 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