diff options
author | Erik M. Bray <embray@stsci.edu> | 2015-05-27 11:06:17 -0400 |
---|---|---|
committer | Erik M. Bray <embray@stsci.edu> | 2015-06-01 12:51:15 -0400 |
commit | 776c3c32b759a62542939fa2abd729f9ad77ae9b (patch) | |
tree | afedfb26df99413c16f7135ad0431f5fe2fd3966 /numpy/lib/tests/test_recfunctions.py | |
parent | 9e7a0b224b4a7859efc1bc80b7ccc5c86c83d3df (diff) | |
download | numpy-776c3c32b759a62542939fa2abd729f9ad77ae9b.tar.gz |
BUG: Further fixes to record and recarray getitem/getattr
This is a followup to PR #5505, which didn't go quite far
enough. This fixes two issues in particular:
1) The record class also needs an updated `__getitem__` that works
analogously to its `__getattribute__` so that a nested record is
returned as a `record` object and not a plain `np.void`. In other
words the old behavior is:
```python
>>> rec = np.rec.array([('abc ', (1,1), 1), ('abc', (2,3), 1)],
... dtype=[('foo', 'S4'), ('bar', [('A', int), ('B', int)]),
('baz', int)])
>>> rec[0].bar
(1, 1)
>>> type(rec[0].bar)
<class 'numpy.record'>
>>> type(rec[0]['bar'])
<type 'numpy.void'>
```
demonstrated inconsistency between `.bar` and `['bar']` on the record
object. The new behavior is:
```python
>>> type(rec[0]['bar'])
<class 'numpy.record'>
```
2) The second issue is more subtle. The fix to #5505 used the
`obj.dtype.descr` attribute to create a new dtype of type `record`.
However, this does not recreate the correct type if the fields are
not aligned. To demonstrate:
```python
>>> dt = np.dtype({'C': ('S5', 0), 'D': ('S5', 6)})
>>> dt.fields
dict_proxy({'C': (dtype('S5'), 0), 'D': (dtype('S5'), 6)})
>>> dt.descr
[('C', '|S5'), ('', '|V1'), ('D', '|S5')]
>>> new_dt = np.dtype((np.record, dt.descr))
>>> new_dt
dtype((numpy.record, [('C', 'S5'), ('f1', 'V1'), ('D', 'S5')]))
>>> new_dt.fields
dict_proxy({'f1': (dtype('V1'), 5), 'C': (dtype('S5'), 0), 'D':
(dtype('S5'), 6)})
```
Using the `fields` dict to construct the new type reconstructs the
correct type with the correct offsets:
```python
>>> new_dt2 = np.dtype((np.record, dt.fields))
>>> new_dt2.fields
dict_proxy({'C': (dtype('S5'), 0), 'D': (dtype('S5'), 6)})
```
(Note: This is based on #5920 for convenience, but I could decouple
the changes if that's preferable.)
Diffstat (limited to 'numpy/lib/tests/test_recfunctions.py')
0 files changed, 0 insertions, 0 deletions