Skip to content
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
3 changes: 3 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
while retaining complex decoding on reopen (issue #1404, PR #1525).
* Speed up index setup in _StartCountStride (used when indexing variables) by
skipping numpy conversion for plain ints and slices and classifying each index once (PR #1524).
* Raise IndexError when assigning to a scalar variable using integer indexing
(consistent with numpy 0-d arrays). Use v[...] = val, v[()] = val or
v.assignValue(val) instead (issue #1531).

version 1.7.5.1.post1 (tag v1.7.5.1.post1rel)
=============================================
Expand Down
21 changes: 20 additions & 1 deletion src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ shape of fancy temp slice = (3, 3, 36, 71)

***Special note for scalar variables***: To extract data from a scalar variable
`v` with no associated dimensions, use `numpy.asarray(v)` or `v[...]`.
The result will be a numpy scalar array.
The result will be a numpy scalar array. To write data to a scalar variable,
use `v[...] = val`, `v[()] = val` or `v.assignValue(val)`. As with numpy
scalar (0-d) arrays, integer indexing (e.g. `v[0] = val`) is not allowed
for scalar variables and will raise an `IndexError`.

By default, netcdf4-python returns numpy masked arrays with values equal to the
`missing_value` or `_FillValue` variable attributes masked for primitive and
Expand Down Expand Up @@ -5540,6 +5543,22 @@ cannot be safely cast to variable data type""" % attname
# arguments to the nc_put_var() function, and is much more easy
# to use.

# scalar variables cannot be assigned to with integer indices
# (consistent with numpy 0-d arrays, issue #1531). Use
# v[...] = val, v[()] = val or v.assignValue(val) instead.
if not self.dimensions:
elems = elem if isinstance(elem, tuple) else (elem,)
for e in elems:
if isinstance(e, (bool, numpy.bool_)):
continue
if isinstance(e, (int, numpy.integer)) or \
(isinstance(e, (list, numpy.ndarray)) and
numpy.asarray(e).dtype.kind in 'iu'):
msg = ('cannot use integer indexing to assign to a scalar '
'variable, use v[...] = val, v[()] = val or '
'v.assignValue(val) instead')
raise IndexError(msg)

# if _Encoding is specified for a character variable, convert
# numpy array of strings to a numpy array of characters with one more
# dimension.
Expand Down
15 changes: 15 additions & 0 deletions test/test_scalarvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from numpy.testing import assert_almost_equal
import netCDF4
import math
import numpy as np

VAR_NAME='temp'
VAR_TYPE='f4'
Expand Down Expand Up @@ -52,6 +53,20 @@ def runTest(self):
# check result of getValue and slice
assert_almost_equal(vg.getValue(), VAR_VAL, decimal=6)
assert_almost_equal(vg[:], VAR_VAL, decimal=6)
# issue #1531: integer indexing not allowed for assignment
# to scalar variables (consistent with numpy 0-d arrays).
for key in (-2000, -2, -1, 0, 1, 2, 2000, np.int64(0), (0,), [0],
np.array([0])):
with self.assertRaises(IndexError):
v[key] = 9
assert_almost_equal(v[...], VAR_VAL, decimal=6)
# allowed methods of assignment
v[...] = 1.0
assert_almost_equal(v[...], 1.0, decimal=6)
v[()] = 2.0
assert_almost_equal(v[...], 2.0, decimal=6)
v.assignValue(3.0)
assert_almost_equal(v[...], 3.0, decimal=6)
f.close()

if __name__ == '__main__':
Expand Down
Loading