85 def __getitem__(self, key):
86 """Return the record at index key if key is an integer,
87 return a column if `key` is a string field name or Key,
88 or return a subset of the catalog if key is a slice
89 or boolean NumPy array.
91 if type(key)
is slice:
92 (start, stop, step) = (key.start, key.stop, key.step)
99 return self.subset(start, stop, step)
100 elif isinstance(key, np.ndarray):
101 if key.dtype == bool:
102 return self.subset(key)
103 raise RuntimeError(
"Unsupported array type for indexing a Catalog, "
104 f
"only boolean arrays are supported: {key.dtype}")
105 elif isinstance(key, str):
106 key = self.schema.find(key).key
109 elif isinstance(key, Key):
113 return self._getitem_(key)
184 def extend(self, iterable, deep=False, mapper=None):
185 """Append all records in the given iterable to the catalog.
190 Any Python iterable containing records.
191 deep : `bool`, optional
192 If `True`, the records will be deep-copied; ignored if
193 mapper is not `None` (that always implies `True`).
194 mapper : `lsst.afw.table.schemaMapper.SchemaMapper`, optional
195 Used to translate records.
200 if type(deep).__name__ ==
"SchemaMapper":
203 if isinstance(iterable, type(self)):
204 if mapper
is not None:
205 self._extend(iterable, mapper)
207 self._extend(iterable, deep)
209 for record
in iterable:
210 if mapper
is not None:
211 self._append(self.
table.copyRecord(record, mapper))
213 self._append(self.
table.copyRecord(record))
221 def asAstropy(self, cls=None, copy=False, unviewable="copy"):
222 """Return an astropy.table.Table (or subclass thereof) view into this catalog.
227 Table subclass to use; `None` implies `astropy.table.Table`
228 itself. Use `astropy.table.QTable` to get Quantity columns.
229 copy : bool, optional
230 If `True`, copy data from the LSST catalog to the astropy
231 table. Not copying is usually faster, but can keep memory
232 from being freed if columns are later removed from the
234 unviewable : `str`, optional
235 One of the following options (which is ignored if
236 copy=`True` ), indicating how to handle field types (`str`
237 and `Flag`) for which views cannot be constructed:
239 - 'copy' (default): copy only the unviewable fields.
240 - 'raise': raise ValueError if unviewable fields are present.
241 - 'skip': do not include unviewable fields in the Astropy Table.
245 cls : `astropy.table.Table`
246 Astropy view into the catalog.
251 Raised if the `unviewable` option is not a known value, or
252 if the option is 'raise' and an uncopyable field is found.
257 cls = astropy.table.Table
258 if unviewable
not in (
"copy",
"raise",
"skip"):
260 f
"'unviewable'={unviewable!r} must be one of 'copy', 'raise', or 'skip'")
261 ps = self.getMetadata()
262 meta = ps.toOrderedDict()
if ps
is not None else None
264 items = self.schema.
extract(
"*", ordered=
True)
265 for name, item
in items.items():
267 unit = item.field.getUnits()
or None
268 if key.getTypeString() ==
"String":
270 if unviewable ==
"raise":
271 raise ValueError(
"Cannot extract string "
272 "unless copy=True or unviewable='copy' or 'skip'.")
273 elif unviewable ==
"skip":
276 len(self), dtype=np.dtype((str, key.getSize())))
277 for i, record
in enumerate(self):
278 data[i] = record.get(key)
279 elif key.getTypeString() ==
"Flag":
281 if unviewable ==
"raise":
282 raise ValueError(
"Cannot extract packed bit columns "
283 "unless copy=True or unviewable='copy' or 'skip'.")
284 elif unviewable ==
"skip":
287 elif key.getTypeString() ==
"Angle":
290 elif "Array" in key.getTypeString()
and key.isVariableLength():
292 if unviewable ==
"raise":
293 raise ValueError(
"Cannot extract variable-length array fields unless unviewable='skip'.")
294 elif unviewable ==
"skip" or unviewable ==
"copy":
299 astropy.table.Column(
303 description=item.field.getDoc()
306 return cls(columns, meta=meta, copy=copy)