Skip to content

BUG: User-facing AssertionError with add column to SparseDataFrame #25503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__'):
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down