8000 Merge pull request #17182 from seberg/doc-metadata · numpy/numpy@f7cb42d · GitHub
[go: up one dir, main page]

Skip to content

Commit f7cb42d

Browse files
authored
Merge pull request #17182 from seberg/doc-metadata
DOC: Document `dtype.metadata`
2 parents 9e8c5d6 + 815f3a2 commit f7cb42d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

doc/source/reference/arrays.dtypes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ Attributes providing additional information:
537537
dtype.alignment
538538
dtype.base
539539

540+
Metadata attached by the user:
541+
542+
.. autosummary::
543+
:toctree: generated/
544+
545+
dtype.metadata
546+
540547

541548
Methods
542549
-------

numpy/core/_add_newdocs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5541,6 +5541,45 @@
55415541
55425542
"""))
55435543

5544+
add_newdoc('numpy.core.multiarray', 'dtype', ('metadata',
5545+
"""
5546+
Either ``None`` or a readonly dictionary of metadata (mappingproxy).
5547+
5548+
The metadata field can be set using any dictionary at data-type
5549+
creation. NumPy currently has no uniform approach to propagating
5550+
metadata; although some array operations preserve it, there is no
5551+
guarantee that others will.
5552+
5553+
.. warning::
5554+
5555+
Although used in certain projects, this feature was long undocumented
5556+
and is not well supported. Some aspects of metadata propagation
5557+
are expected to change in the future.
5558+
5559+
Examples
5560+
--------
5561+
5562+
>>> dt = np.dtype(float, metadata={"key": "value"})
5563+
>>> dt.metadata["key"]
5564+
'value'
5565+
>>> arr = np.array([1, 2, 3], dtype=dt)
5566+
>>> arr.dtype.metadata
5567+
mappingproxy({'key': 'value'})
5568+
5569+
Adding arrays with identical datatypes currently preserves the metadata:
5570+
5571+
>>> (arr + arr).dtype.metadata
5572+
mappingproxy({'key': 'value'})
5573+
5574+
But if the arrays have different dtype metadata, the metadata may be
5575+
dropped:
5576+
5577+
>>> dt2 = np.dtype(float, metadata={"key2": "value2"})
5578+
>>> arr2 = np.array([3, 2, 1], dtype=dt2)
5579+
>>> (arr + arr2).dtype.metadata is None
5580+
True # The metadata field is cleared so None is returned
5581+
"""))
5582+
55445583
add_newdoc('numpy.core.multiarray', 'dtype', ('name',
55455584
"""
55465585
A bit-width name for this data-type.

0 commit comments

Comments
 (0)
0