Skip to content

Commit 6ca3bd7

Browse files
authored
full_like: error on non-scalar fill_value (#3979)
* Avoid multiplication DeprecationWarning in rasterio backend * full_like: error on non-scalar fill_value Fixes #3977 * Added test * Updated what's new * core.utils.is_scalar instead of numpy.is_scalar * More informative error message * raises_regex for error test
1 parent 37551da commit 6ca3bd7

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ New Features
5858

5959
Bug fixes
6060
~~~~~~~~~
61+
- ``ValueError`` is raised when ``fill_value`` is not a scalar in :py:meth:`full_like`. (:issue`3977`)
62+
By `Huite Bootsma <https://github.com/huite>`_.
6163
- Fix wrong order in converting a ``pd.Series`` with a MultiIndex to ``DataArray``. (:issue:`3951`)
6264
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
6365
- Fix renaming of coords when one or more stacked coords is not in

xarray/core/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .options import OPTIONS, _get_keep_attrs
2626
from .pycompat import dask_array_type
2727
from .rolling_exp import RollingExp
28-
from .utils import Frozen, either_dict_or_kwargs
28+
from .utils import Frozen, either_dict_or_kwargs, is_scalar
2929

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

1400+
if not is_scalar(fill_value):
1401+
raise ValueError(f"fill_value must be scalar. Received {fill_value} instead.")
1402+
14001403
if isinstance(other, Dataset):
14011404
data_vars = {
14021405
k: _full_like_variable(v, fill_value, dtype)

xarray/tests/test_variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,10 @@ def test_full_like(self):
22132213
assert expect.dtype == bool
22142214
assert_identical(expect, full_like(orig, True, dtype=bool))
22152215

2216+
# raise error on non-scalar fill_value
2217+
with raises_regex(ValueError, "must be scalar"):
2218+
full_like(orig, [1.0, 2.0])
2219+
22162220
@requires_dask
22172221
def test_full_like_dask(self):
22182222
orig = Variable(

0 commit comments

Comments
 (0)