Skip to content

Assignment 2d numpy array to 3d variable with first dimension of length 1 has changed behavior #919

@cpaulik

Description

@cpaulik

In version 1.5.1 the following code errors:

We are assigning a shape (100, 100) array to a (1, 100, 100) variable.
Not sure if this is supposed to work but it did in the past.

import netCDF4
import numpy as np

with netCDF4.Dataset('test.nc','w') as f:
    f.createDimension('time',1)
    f.createDimension('lat',100)
    f.createDimension('lon',100)

    f.createVariable('v1',np.float,('time', 'lon','lat',))
    arr = np.arange(100*100).reshape((100, 100))
    f['v1'][:] = arr
  File "01-160948.py", line 11, in <module>
    f['v1'][:] = arr
  File "netCDF4/_netCDF4.pyx", line 4796, in netCDF4._netCDF4.Variable.__setitem__
  File "/home/cpa/.pyenv/versions/miniconda2-4.3.30/envs/libsat/lib/python3.6/site-packages/netCDF4/utils.py", line 352, in _StartCountStride
    datashapenew = datashapenew + (datashape[i],)
IndexError: tuple index out of range

It works up to version 1.5.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions