Skip to content

Commit 173599f

Browse files
committed
Fix uint conversion after slicing
Fix astropy#5490. The _uint attribute was not transfered to the sliced array, so the data was later converted either correctly (if it was converted first before slicing) or as float in the other case.
1 parent c0b29d8 commit 173599f

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,8 @@ astropy.io.ascii
23862386
astropy.io.fits
23872387
^^^^^^^^^^^^^^^
23882388

2389+
- Fix uint conversion in ``FITS_rec`` when slicing a table. [#8982]
2390+
23892391
astropy.io.misc
23902392
^^^^^^^^^^^^^^^
23912393

‎astropy/io/fits/fitsrec.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ def __getitem__(self, key):
497497

498498
# We got a view; change it back to our class, and add stuff
499499
out = out.view(type(self))
500+
out._uint = self._uint
500501
out._coldefs = ColDefs(self._coldefs)
501502
arrays = []
502503
out._converted = {}

‎astropy/io/fits/tests/test_uint.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import numpy as np
7+
from numpy.testing import assert_array_equal
78

89
from astropy.io import fits
910
from . import FitsTestCase
@@ -112,3 +113,30 @@ def test_uint_columns(self, utype):
112113
table.data.base.base[utype]).all()
113114
assert (hdudata3[utype] == table.data[utype]).all()
114115
assert (hdudata3[utype] == u).all()
116+
117+
def test_uint_slice(self):
118+
"""
119+
Fix for https://github.com/astropy/astropy/issues/5490
120+
if data is sliced first, make sure the data is still converted as uint
121+
"""
122+
# create_data:
123+
dataref = np.arange(2**16, dtype=np.uint16)
124+
tbhdu = fits.BinTableHDU.from_columns([
125+
fits.Column(name='a', format='I',
126+
array=np.arange(2**16, dtype=np.int16)),
127+
fits.Column(name='b', format='I', bscale=1, bzero=2**15,
128+
array=dataref)
129+
])
130+
tbhdu.writeto(self.temp('test_scaled_slicing.fits'))
131+
132+
with fits.open(self.temp('test_scaled_slicing.fits')) as hdulist:
133+
data = hdulist[1].data
134+
assert_array_equal(data['b'], dataref)
135+
sel = data['a'] >= 0
136+
assert_array_equal(data[sel]['b'], dataref[sel])
137+
assert data[sel]['b'].dtype == dataref[sel].dtype
138+
139+
with fits.open(self.temp('test_scaled_slicing.fits')) as hdulist:
140+
data = hdulist[1].data
141+
assert_array_equal(data[sel]['b'], dataref[sel])
142+
assert data[sel]['b'].dtype == dataref[sel].dtype

0 commit comments

Comments
 (0)