Skip to content
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

check early for non-scalar default_fill_value #27302

Merged
merged 3 commits into from
Jul 10, 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
5 changes: 4 additions & 1 deletion pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np

from pandas._libs.lib import is_scalar, item_from_zerodim
from pandas._libs.sparse import BlockIndex, get_blocks
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender
Expand Down Expand Up @@ -74,6 +75,8 @@ def __init__(
dtype=None,
copy=False,
):
if not is_scalar(default_fill_value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a dict acceptable here at all? Think we allow that for normal DataFrame fillna

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger are there cases where non-scalar is accepted? I was getting errors in another branch caused by zero-dim ndarray getting through here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe we allow this (nor should)

raise ValueError("'default_fill_value' must be a scalar")

warnings.warn(depr_msg, FutureWarning, stacklevel=2)
# pick up the defaults from the Sparse structures
Expand Down Expand Up @@ -666,7 +669,7 @@ def _get_op_result_fill_value(self, other, func):
fill_value = np.nan
else:
fill_value = func(np.float64(own_default), np.float64(other.fill_value))

fill_value = item_from_zerodim(fill_value)
else:
raise NotImplementedError(type(other))

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def test_constructor(self, float_frame, float_frame_int_kind, float_frame_fill0)

repr(float_frame)

def test_constructor_fill_value_not_scalar_raises(self):
d = {"b": [2, 3], "a": [0, 1]}
fill_value = np.array(np.nan)
with pytest.raises(ValueError, match="must be a scalar"):
SparseDataFrame(data=d, default_fill_value=fill_value)

def test_constructor_dict_order(self):
# GH19018
# initialization ordering: by insertion order if python>= 3.6, else
Expand Down