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

full_like: error on non-scalar fill_value #3979

Merged
merged 6 commits into from
Apr 24, 2020
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: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ New Features

Bug fixes
~~~~~~~~~
- ``ValueError`` is raised when ``fill_value`` is not a scalar in :py:meth:`full_like`. (:issue`3977`)
By `Huite Bootsma <https://github.com/huite>`_.
- Fix wrong order in converting a ``pd.Series`` with a MultiIndex to ``DataArray``. (:issue:`3951`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Fix renaming of coords when one or more stacked coords is not in
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .options import OPTIONS, _get_keep_attrs
from .pycompat import dask_array_type
from .rolling_exp import RollingExp
from .utils import Frozen, either_dict_or_kwargs
from .utils import Frozen, either_dict_or_kwargs, is_scalar

# Used as a sentinel value to indicate a all dimensions
ALL_DIMS = ...
Expand Down Expand Up @@ -1397,6 +1397,9 @@ def full_like(other, fill_value, dtype: DTypeLike = None):
from .dataset import Dataset
from .variable import Variable

if not is_scalar(fill_value):
raise ValueError(f"fill_value must be scalar. Received {fill_value} instead.")

if isinstance(other, Dataset):
data_vars = {
k: _full_like_variable(v, fill_value, dtype)
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,10 @@ def test_full_like(self):
assert expect.dtype == bool
assert_identical(expect, full_like(orig, True, dtype=bool))

# raise error on non-scalar fill_value
with raises_regex(ValueError, "must be scalar"):
full_like(orig, [1.0, 2.0])

@requires_dask
def test_full_like_dask(self):
orig = Variable(
Expand Down