8000 Restore np.recarray and deprecate np.chararray · numpy/numpy@bbf20e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbf20e2

Browse files
committed
Restore np.recarray and deprecate np.chararray
1 parent 59eb21b commit bbf20e2

27 files changed

+131
-125
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
* ``np.compare_chararrays`` has been removed from the main namespace.
22
Use ``np.char.compare_chararrays`` instead.
33

4-
* ``np.chararray`` has been removed from the main namespace.
4+
* ``np.chararray`` has been deprecated and it will be removed in the future.
55
Use ``np.char.chararray`` instead.
66

77
* ``np.format_parser`` has been removed from the main namespace.
88
Use ``np.rec.format_parser`` instead.
9-
10-
* ``np.recarray`` has been removed from the main namespace.
11-
Use ``np.rec.recarray`` instead.

doc/source/glossary.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ Glossary
442442
record array
443443
A :term:`structured array` with allowing access in an attribute style
444444
(``a.field``) in addition to ``a['field']``. For details, see
445-
:doc:`numpy.rec.recarray. <reference/generated/numpy.rec.recarray>`
445+
:doc:`numpy.recarray. <reference/generated/numpy.recarray>`
446446

447447

448448
row-major

doc/source/reference/arrays.classes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ scalar data type object :class:`record`.
526526
.. autosummary::
527527
:toctree: generated/
528528

529-
rec.recarray
529+
recarray
530530
record
531531

532532
.. note::

doc/source/user/basics.io.genfromtxt.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ Validating names
323323
----------------
324324

325325
NumPy arrays with a structured dtype can also be viewed as
326-
:class:`~numpy.rec.recarray`, where a field can be accessed as if it were an
326+
:class:`~numpy.recarray`, where a field can be accessed as if it were an
327327
attribute. For that reason, we may need to make sure that the field name
328328
doesn't contain any space or invalid character, or that it does not
329329
correspond to the name of a standard attribute (like ``size`` or

doc/source/user/basics.rec.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ Record Arrays
630630
=============
631631

632632
As an optional convenience numpy provides an ndarray subclass,
633-
:class:`numpy.rec.recarray` that allows access to fields of structured arrays
633+
:class:`numpy.recarray` that allows access to fields of structured arrays
634634
by attribute instead of only by index.
635635
Record arrays use a special datatype, :class:`numpy.record`, that allows
636636
field access by attribute on the structured scalars obtained from the array.
@@ -673,13 +673,13 @@ appropriate `view <numpy-ndarray-view>`_::
673673
>>> arr = np.array([(1, 2., 'Hello'), (2, 3., "World")],
674674
... dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'a10')])
675675
>>> recordarr = arr.view(dtype=np.dtype((np.record, arr.dtype)),
676-
... type=np.rec.recarray)
676+
... type=np.recarray)
677677

678-
For convenience, viewing an ndarray as type :class:`numpy.rec.recarray` will
678+
For convenience, viewing an ndarray as type :class:`numpy.recarray` will
679679
automatically convert to :class:`numpy.record` datatype, so the dtype can be left
680680
out of the view::
681681

682-
>>> recordarr = arr.view(np.rec.recarray)
682+
>>> recordarr = arr.view(np.recarray)
683683
>>> recordarr.dtype
684684
dtype((numpy.record, [('foo', '<i4'), ('bar', '<f4'), ('baz', 'S10')]))
685685

@@ -697,7 +697,7 @@ array if the field has a structured type but as a plain ndarray otherwise. ::
697697
>>> type(recordarr.foo)
698698
<class 'numpy.ndarray'>
699699
>>> type(recordarr.bar)
700-
<class 'numpy.rec.recarray'>
700+
<class 'numpy.recarray'>
701701

702702
Note that if a field has the same name as an ndarray attribute, the ndarray
703703
attribute takes precedence. Such fields will be inaccessible by attribute but

numpy/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120

121121
from . import core
122122
from .core import (
123-
_no_nep50_warning, memmap, iinfo, finfo,
123+
_no_nep50_warning, memmap, iinfo, finfo, recarray,
124124
False_, ScalarType, True_, abs, absolute, add, all, allclose, alltrue,
125125
amax, amin, any, arange, arccos, arccosh, arcsin, arcsinh, arctan,
126126
arctan2, arctanh, argmax, argmin, argpartition, argsort, argwhere,
@@ -390,6 +390,14 @@ def __getattr__(attr):
390390
f"{__expired_attributes__[attr]}"
391391
)
392392

393+
if attr == "chararray":
394+
warnings.warn(
395+
"`np.chararray` is deprecated and will be removed from "
396+
"the main namespace in the future. Use `np.char.chararray` "
397+
"instead.", DeprecationWarning, stacklevel=2)
398+
import numpy.char as char
399+
return char.chararray
400+
393401
raise AttributeError("module {!r} has no attribute "
394402
"{!r}".format(__name__, attr))
395403

numpy/__init__.pyi

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ from numpy import (
210210
char as char,
211211
)
212212

213-
from numpy.core import defchararray, records
214-
char = defchararray
215-
rec = records
213+
from numpy.core.records import (
214+
record as record,
215+
recarray as recarray,
216+
)
217+
218+
from numpy.core.defchararray import (
219+
chararray as chararray,
220+
)
216221

217222
from numpy.core.function_base import (
218223
linspace as linspace,
@@ -3379,15 +3384,6 @@ class iinfo(Generic[_IntType]):
33793384
@overload
33803385
def __new__(cls, dtype: str) -> iinfo[Any]: ...
33813386

3382-
class record(void):
3383-
def __getattribute__(self, attr: str) -> Any: ...
3384-
def __setattr__(self, attr: str, val: ArrayLike) -> None: ...
3385-
def pprint(self) -> str: ...
3386-
@overload
3387-
def __getitem__(self, key: str | SupportsIndex) -> Any: ...
3388-
@overload
3389-
def __getitem__(self, key: list[str]) -> record: ...
3390-
33913387
_NDIterFlagsKind = L[
33923388
"buffered",
33933389
"c_index",

numpy/_expired_attrs_2_0.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,5 @@
7171
"byte_bounds": "Now it's available under `np.lib.array_utils.byte_bounds`",
7272
"compare_chararrays":
7373
"It's still available as `np.char.compare_chararrays`.",
74-
"chararray": "It's still available as `np.char.chararray`.",
7574
"format_parser": "It's still available as `np.rec.format_parser`.",
76-
"recarray": "It's still available as `np.rec.recarray`.",
7775
}

numpy/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
from .numeric import *
7777
from . import fromnumeric
7878
from .fromnumeric import *
79-
from .records import record
79+
from .records import record, recarray
8080
# Note: module name memmap is overwritten by a class with same name
8181
from .memmap import *
8282
from . import function_base
@@ -102,7 +102,7 @@
102102
from . import _dtype
103103
from . import _methods
104104

105-
__all__ = ['memmap', 'record', 'abs']
105+
__all__ = ['memmap', 'record', 'recarray', 'abs']
106106
__all__ += numeric.__all__
107107
__all__ += function_base.__all__
108108
__all__ += getlimits.__all__

numpy/core/_add_newdocs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,9 @@
978978
979979
Contrary to `asanyarray`, ndarray subclasses are not passed through:
980980
981-
>>> issubclass(np.rec.recarray, np.ndarray)
981+
>>> issubclass(np.recarray, np.ndarray)
982982
True
983-
>>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.rec.recarray)
983+
>>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray)
984984
>>> np.asarray(a) is a
985985
False
986986
>>> np.asanyarray(a) is a
@@ -1044,7 +1044,7 @@
10441044
10451045
Instances of `ndarray` subclasses are passed through as-is:
10461046
1047-
>>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.rec.recarray)
1047+
>>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray)
10481048
>>> np.asanyarray(a) is a
10491049
True
10501050
@@ -4645,7 +4645,7 @@
46454645
46464646
Using a view to convert an array to a recarray:
46474647
4648-
>>> z = x.view(np.rec.recarray)
4648+
>>> z = x.view(np.recarray)
46494649
>>> z.a
46504650
array([1, 3], dtype=int8)
46514651

numpy/core/records.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def pprint(self):
307307
# the fields (and any subfields)
308308

309309

310-
@set_module("numpy.rec")
310+
@set_module("numpy")
311311
class recarray(ndarray):
312312
"""Construct an ndarray that allows field access using attributes.
313313
@@ -378,7 +378,7 @@ class recarray(ndarray):
378378
use one of the following methods:
379379
380380
1. Create a standard ndarray and convert it to a record array,
381-
using ``arr.view(np.rec.recarray)``
381+
using ``arr.view(np.recarray)``
382382
2. Use the `buf` keyword.
383383
3. Use `np.rec.fromrecords`.
384384
@@ -395,7 +395,7 @@ class recarray(ndarray):
395395
396396
View the array as a record array:
397397
398-
>>> x = x.view(np.rec.recarray)
398+
>>> x = x.view(np.recarray)
399399
400400
>>> x.x
401401
array([1., 3.])
@@ -405,7 +405,7 @@ class recarray(ndarray):
405405
406406
Create a new, empty record array:
407407
408-
>>> np.rec.recarray((2,),
408+
>>> np.recarray((2,),
409409
... dtype=[('x', int), ('y', float), ('z', int)]) #doctest: +SKIP
410410
rec.array([(-1073741821, 1.2249118382103472e-301, 24547520),
411411
(3471280, 1.2134086255804012e-316, 0)],
@@ -538,7 +538,7 @@ def __repr__(self):
538538
# This should only happen if the user is playing
539539
# strange games with dtypes.
540540
prefix = "array("
541-
fmt = 'array(%s,%sdtype=%s).view(numpy.rec.recarray)'
541+
fmt = 'array(%s,%sdtype=%s).view(numpy.recarray)'
542542

543543
# get data/shape string. logic taken from numeric.array_repr
544544
if self.size > 0 or self.shape == (0,):
@@ -605,7 +605,7 @@ def fromarrays(arrayList, dtype=None, shape=None, formats=None,
605605
606606
Returns
607607
-------
608-
np.rec.recarray
608+
np.recarray
609609
Record array consisting of given arrayList columns.
610610
611611
Examples
@@ -703,7 +703,7 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None,
703703
704704
Returns
705705
-------
706-
np.rec.recarray
706+
np.recarray
707707
record array consisting of given recList rows.
708708
709709
Examples
@@ -789,7 +789,7 @@ def fromstring(datastring, dtype=None, shape=None, offset=0, formats=None,
789789
790790
Returns
791791
-------
792-
np.rec.recarray
792+
np.recarray
793793
Record array view into the data in datastring. This will be readonly
794794
if `datastring` is readonly.
795795
@@ -871,7 +871,7 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
871871
872872
Returns
873873
-------
874-
np.rec.recarray
874+
np.recarray
875875
record array consisting of data enclosed in file.
876876
877877
Examples
@@ -983,16 +983,16 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None,
983983
984984
Returns
985985
-------
986-
np.rec.recarray
986+
np.recarray
987987
Record array created from the specified object.
988988
989989
Notes
990990
-----
991-
If `obj` is ``None``, then call the `~numpy.rec.recarray` constructor. If
991+
If `obj` is ``None``, then call the `~numpy.recarray` constructor. If
992992
`obj` is a string, then call the `fromstring` constructor. If `obj` is a
993993
list or a tuple, then if the first object is an `~numpy.ndarray`, call
994994
`fromarrays`, otherwise call `fromrecords`. If `obj` is a
995-
`~numpy.rec.recarray`, then make a copy of the data in the recarray
995+
`~numpy.recarray`, then make a copy of the data in the recarray
996996
(if ``copy=True``) and use the new formats, names, and titles. If `obj`
997997
is a file, then call `fromfile`. Finally, if obj is an `ndarray`, then
998998
return ``obj.view(recarray)``, making a copy of the data if ``copy=True``.

numpy/core/records.pyi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ from typing import (
1010
)
1111

1212
from numpy import (
13-
record as record,
1413
ndarray,
1514
dtype,
1615
generic,
@@ -41,6 +40,15 @@ class _SupportsReadInto(Protocol):
4140
def tell(self, /) -> int: ...
4241
def readinto(self, buffer: memoryview, /) -> int: ...
4342

43+
class record(void):
44+
def __getattribute__(self, attr: str) -> Any: ...
45+
def __setattr__(self, attr: str, val: ArrayLike) -> None: ...
46+
def pprint(self) -> str: ...
47+
@overload
48+
def __getitem__(self, key: str | SupportsIndex) -> Any: ...
49+
@overload
50+
def __getitem__(self, key: list[str]) -> record: ...
51+
4452
class recarray(ndarray[_ShapeType, _DType_co]):
4553
# NOTE: While not strictly mandatory, we're demanding here that arguments
4654
# for the `format_parser`- and `dtype`-based dtype constructors are

numpy/core/tests/test_arraymethod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_invalid_arguments(self, args, error):
6666

6767
@pytest.mark.parametrize(
6868
"cls", [
69-
np.ndarray, np.rec.recarray, np.char.chararray, np.matrix, np.memmap
69+
np.ndarray, np.recarray, np.char.chararray, np.matrix, np.memmap
7070
]
7171
)
7272
class TestClassGetItem:

numpy/core/tests/test_dtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def test_fieldless_views(self):
569569
def test_nonstructured_with_object(self):
570570
# See gh-23277, the dtype here thinks it contain objects, if the
571571
# assert about that fails, the test becomes meaningless (which is OK)
572-
arr = np.rec.recarray((0,), dtype="O")
572+
arr = np.recarray((0,), dtype="O")
573573
assert arr.dtype.names is None # no fields
574574
assert arr.dtype.hasobject # but claims to contain objects
575575
del arr # the deletion failed previously.

0 commit comments

Comments
 (0)
0