@@ -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 :
0 commit comments