Skip to content

Commit 10b2e14

Browse files
committed
Ensure FITS_rec can be indexed with a tuple even if it returns a scalar.
1 parent 66ba5d5 commit 10b2e14

3 files changed

Lines changed: 45 additions & 33 deletions

File tree

‎CHANGES.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ astropy.io.ascii
446446
astropy.io.fits
447447
^^^^^^^^^^^^^^^
448448

449+
- Fix a minor bug where ``FITS_rec`` instances can not be indexed with tuples
450+
and other sequences that end up with a scalar. [#6955]
451+
449452
astropy.io.misc
450453
^^^^^^^^^^^^^^^
451454

‎astropy/io/fits/fitsrec.py‎

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -484,40 +484,37 @@ def __getitem__(self, key):
484484

485485
if isinstance(key, str):
486486
return self.field(key)
487-
elif isinstance(key, (slice, np.ndarray, tuple, list)):
488-
# Have to view as a recarray then back as a FITS_rec, otherwise the
489-
# circular reference fix/hack in FITS_rec.field() won't preserve
490-
# the slice
491-
subtype = type(self)
492-
out = self.view(np.recarray).__getitem__(key).view(subtype)
493-
out._coldefs = ColDefs(self._coldefs)
494-
arrays = []
495-
out._converted = {}
496-
for idx, name in enumerate(self._coldefs.names):
497-
#
498-
# Store the new arrays for the _coldefs object
499-
#
500-
arrays.append(self._coldefs._arrays[idx][key])
501-
502-
# Ensure that the sliced FITS_rec will view the same scaled
503-
# columns as the original; this is one of the few cases where
504-
# it is not necessary to use _cache_field()
505-
if name in self._converted:
506-
dummy = self._converted[name]
507-
field = np.ndarray.__getitem__(dummy, key)
508-
out._converted[name] = field
509-
510-
out._coldefs._arrays = arrays
511-
return out
512-
513-
# if not a slice, do this because Record has no __getstate__.
514-
# also more efficient.
515-
else:
516-
if isinstance(key, int) and key >= len(self):
517-
raise IndexError("Index out of bounds")
518487

519-
newrecord = self._record_type(self, key)
520-
return newrecord
488+
# Have to view as a recarray then back as a FITS_rec, otherwise the
489+
# circular reference fix/hack in FITS_rec.field() won't preserve
490+
# the slice.
491+
out = self.view(np.recarray)[key]
492+
if type(out) is np.record:
493+
# Oops, we got a single element rather than a view. In that case,
494+
# return a Record, which has no __getstate__ and is more efficient.
495+
return self._record_type(self, key)
496+
497+
# We got a view; change it back to our class, and add stuff
498+
out = out.view(type(self))
499+
out._coldefs = ColDefs(self._coldefs)
500+
arrays = []
501+
out._converted = {}
502+
for idx, name in enumerate(self._coldefs.names):
503+
#
504+
# Store the new arrays for the _coldefs object
505+
#
506+
arrays.append(self._coldefs._arrays[idx][key])
507+
508+
# Ensure that the sliced FITS_rec will view the same scaled
509+
# columns as the original; this is one of the few cases where
510+
# it is not necessary to use _cache_field()
511+
if name in self._converted:
512+
dummy = self._converted[name]
513+
field = np.ndarray.__getitem__(dummy, key)
514+
out._converted[name] = field
515+
516+
out._coldefs._arrays = arrays
517+
return out
521518

522519
def __setitem__(self, key, value):
523520
if self._coldefs is None:

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,3 +3062,15 @@ def test_table_to_hdu():
30623062

30633063
assert hdu.header['FOO'] == 'bar'
30643064
assert hdu.header['TEST'] == 1
3065+
3066+
3067+
def test_regression_scalar_indexing():
3068+
# Indexing a FITS_rec with a tuple that returns a scalar record
3069+
# should work
3070+
x = np.array([(1.0, 2), (3.0, 4)],
3071+
dtype=[('x', float), ('y', int)]).view(fits.FITS_rec)
3072+
x1a = x[1]
3073+
# this should succeed.
3074+
x1b = x[(1,)]
3075+
# FITS_record does not define __eq__; so test elements.
3076+
assert all(a == b for a, b in zip(x1a, x1b))

0 commit comments

Comments
 (0)