Skip to content

Commit 718c019

Browse files
saimneblur
authored andcommitted
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 535e940 commit 718c019

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
@@ -2389,6 +2389,8 @@ astropy.io.ascii
23892389
astropy.io.fits
23902390
^^^^^^^^^^^^^^^
23912391

2392+
- Fix uint conversion in ``FITS_rec`` when slicing a table. [#8982]
2393+
23922394
astropy.io.misc
23932395
^^^^^^^^^^^^^^^
23942396

‎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)