MCPcopy Create free account
hub / github.com/numpy/numpy / drop_metadata

Function drop_metadata

numpy/lib/utils.py:1151–1211  ·  view source on GitHub ↗

Returns the dtype unchanged if it contained no metadata or a copy of the dtype if it (or any of its structure dtypes) contained metadata. This utility is used by `np.save` and `np.savez` to drop metadata before saving. .. note:: Due to its limitation this function may

(dtype, /)

Source from the content-addressed store, hash-verified

1149
1150
1151def drop_metadata(dtype, /):
1152 """
1153 Returns the dtype unchanged if it contained no metadata or a copy of the
1154 dtype if it (or any of its structure dtypes) contained metadata.
1155
1156 This utility is used by `np.save` and `np.savez` to drop metadata before
1157 saving.
1158
1159 .. note::
1160
1161 Due to its limitation this function may move to a more appropriate
1162 home or change in the future and is considered semi-public API only.
1163
1164 .. warning::
1165
1166 This function does not preserve more strange things like record dtypes
1167 and user dtypes may simply return the wrong thing. If you need to be
1168 sure about the latter, check the result with:
1169 ``np.can_cast(new_dtype, dtype, casting="no")``.
1170
1171 """
1172 if dtype.fields is not None:
1173 found_metadata = dtype.metadata is not None
1174
1175 names = []
1176 formats = []
1177 offsets = []
1178 titles = []
1179 for name, field in dtype.fields.items():
1180 field_dt = drop_metadata(field[0])
1181 if field_dt is not field[0]:
1182 found_metadata = True
1183
1184 names.append(name)
1185 formats.append(field_dt)
1186 offsets.append(field[1])
1187 titles.append(None if len(field) < 3 else field[2])
1188
1189 if not found_metadata:
1190 return dtype
1191
1192 structure = dict(
1193 names=names, formats=formats, offsets=offsets, titles=titles,
1194 itemsize=dtype.itemsize)
1195
1196 # NOTE: Could pass (dtype.type, structure) to preserve record dtypes...
1197 return np.dtype(structure, align=dtype.isalignedstruct)
1198 elif dtype.subdtype is not None:
1199 # subarray dtype
1200 subdtype, shape = dtype.subdtype
1201 new_subdtype = drop_metadata(subdtype)
1202 if dtype.metadata is None and new_subdtype is subdtype:
1203 return dtype
1204
1205 return np.dtype((new_subdtype, shape))
1206 else:
1207 # Normal unstructured dtype
1208 if dtype.metadata is None:

Callers 2

dtype_to_descrFunction · 0.90
test_metadata_dtypeFunction · 0.90

Calls 1

dtypeMethod · 0.45

Tested by 1

test_metadata_dtypeFunction · 0.72