Skip to content

Commit 7cbd822

Browse files
Anthony Byuraevrossbar
andauthored
DOC: Improve rec.array function documentation (#15853) (#16260)
Add complete docstring with parameters and examples to numpy.core.records.array (gh-15853). Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
1 parent 14d3173 commit 7cbd822

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

numpy/core/records.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,86 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
954954

955955
def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None,
956956
names=None, titles=None, aligned=False, byteorder=None, copy=True):
957-
"""Construct a record array from a wide-variety of objects.
957+
"""
958+
Construct a record array from a wide-variety of objects.
959+
960+
A general-purpose record array constructor that dispatches to the
961+
appropriate `recarray` creation function based on the inputs (see Notes).
962+
963+
Parameters
964+
----------
965+
obj: any
966+
Input object. See Notes for details on how various input types are
967+
treated.
968+
dtype: data-type, optional
969+
Valid dtype for array.
970+
shape: int or tuple of ints, optional
971+
Shape of each array.
972+
offset: int, optional
973+
Position in the file or buffer to start reading from.
974+
strides: tuple of ints, optional
975+
Buffer (`buf`) is interpreted according to these strides (strides
976+
define how many bytes each array element, row, column, etc.
977+
occupy in memory).
978+
formats, names, titles, aligned, byteorder :
979+
If `dtype` is ``None``, these arguments are passed to
980+
`numpy.format_parser` to construct a dtype. See that function for
981+
detailed documentation.
982+
copy: bool, optional
983+
Whether to copy the input object (True), or to use a reference instead.
984+
This option only applies when the input is an ndarray or recarray.
985+
Defaults to True.
986+
987+
Returns
988+
-------
989+
np.recarray
990+
Record array created from the specified object.
991+
992+
Notes
993+
-----
994+
If `obj` is ``None``, then call the `~numpy.recarray` constructor. If
995+
`obj` is a string, then call the `fromstring` constructor. If `obj` is a
996+
list or a tuple, then if the first object is an `~numpy.ndarray`, call
997+
`fromarrays`, otherwise call `fromrecords`. If `obj` is a
998+
`~numpy.recarray`, then make a copy of the data in the recarray
999+
(if ``copy=True``) and use the new formats, names, and titles. If `obj`
1000+
is a file, then call `fromfile`. Finally, if obj is an `ndarray`, then
1001+
return ``obj.view(recarray)``, making a copy of the data if ``copy=True``.
1002+
1003+
Examples
1004+
--------
1005+
>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
1006+
array([[1, 2, 3],
1007+
[4, 5, 6],
1008+
[7, 8, 9]])
1009+
1010+
>>> np.core.records.array(a)
1011+
rec.array([[1, 2, 3],
1012+
[4, 5, 6],
1013+
[7, 8, 9]],
1014+
dtype=int32)
1015+
1016+
>>> b = [(1, 1), (2, 4), (3, 9)]
1017+
>>> c = np.core.records.array(b, formats = ['i2', 'f2'], names = ('x', 'y'))
1018+
>>> c
1019+
rec.array([(1, 1.0), (2, 4.0), (3, 9.0)],
1020+
dtype=[('x', '<i2'), ('y', '<f2')])
1021+
1022+
>>> c.x
1023+
rec.array([1, 2, 3], dtype=int16)
1024+
1025+
>>> c.y
1026+
rec.array([ 1.0, 4.0, 9.0], dtype=float16)
1027+
1028+
>>> r = np.rec.array(['abc','def'], names=['col1','col2'])
1029+
>>> print(r.col1)
1030+
abc
1031+
1032+
>>> r.col1
1033+
array('abc', dtype='<U3')
1034+
1035+
>>> r.col2
1036+
array('def', dtype='<U3')
9581037
"""
9591038

9601039
if ((isinstance(obj, (type(None), str)) or isfileobj(obj)) and

0 commit comments

Comments
 (0)