Skip to content

Commit 2c518fc

Browse files
jswhit2jswhit2
authored andcommitted
Fix DeprecationWarning for assigning to numpy.ndarray.shape (issue #1468)
1 parent 0710bf2 commit 2c518fc

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

Changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
=================================
33
* Change default encoding for stringtochar/chartostring functions from 'utf-8' to 'utf-8'/'ascii' for dtype.kind='U'/'S'
44
(issue #1464).
5+
* Fix DeprecationWarning for assigning to numpy.ndarray.shape for numpy >= 2.5.0 (issue #1468).
56

67
version 1.7.4 (tag v1.7.4rel)
78
================================

src/netCDF4/_netCDF4.pyx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5630,7 +5630,7 @@ cannot be safely cast to variable data type""" % attname
56305630
# create a view so shape in caller is not modified (issue 90)
56315631
try: # if extra singleton dims, just reshape
56325632
data = data.view()
5633-
data.shape = tuple(datashape)
5633+
data = data.reshape(tuple(datashape))
56345634
except ValueError: # otherwise broadcast
56355635
data = numpy.broadcast_to(data, datashape)
56365636

@@ -6821,8 +6821,7 @@ and shape `a.shape + (N,)`, where N is the length of each string in a."""
68216821
if encoding in ['none','None','bytes']:
68226822
b = numpy.array(tuple(a.tobytes()),'S1')
68236823
elif encoding == 'ascii':
6824-
b = numpy.array(tuple(a.tobytes().decode(encoding)),dtype+'1')
6825-
b.shape = a.shape + (n_strlen,)
6824+
b = (numpy.array(tuple(a.tobytes().decode(encoding)),dtype+'1')).reshape(a.shape + (n_strlen,))
68266825
else:
68276826
if not a.ndim:
68286827
a = numpy.array([a])
@@ -6862,8 +6861,7 @@ returns a numpy string array with datatype `'UN'` (or `'SN'`) and shape
68626861
a = numpy.array([bs[n1:n1+slen] for n1 in range(0,len(bs),slen)],'S'+repr(slen))
68636862
else:
68646863
a = numpy.array([bs[n1:n1+slen].decode(encoding) for n1 in range(0,len(bs),slen)],'U'+repr(slen))
6865-
a.shape = b.shape[:-1]
6866-
return a
6864+
return a.reshape(b.shape[:-1])
68676865

68686866
class MFDataset(Dataset):
68696867
"""

0 commit comments

Comments
 (0)