Get a row or a range of rows from the table. If key argument is an integer, the corresponding table row is returned as a record of the current flavor. If key is a slice, the range of rows determined by it is returned as a structured array of the current flavor.
(
self, key: int | slice | list[int] | list[bool] | np.ndarray
)
| 2179 | return self.read(field=name) |
| 2180 | |
| 2181 | def __getitem__( |
| 2182 | self, key: int | slice | list[int] | list[bool] | np.ndarray |
| 2183 | ) -> np.ndarray: |
| 2184 | """Get a row or a range of rows from the table. |
| 2185 | |
| 2186 | If key argument is an integer, the corresponding table row is returned |
| 2187 | as a record of the current flavor. If key is a slice, the range of rows |
| 2188 | determined by it is returned as a structured array of the current |
| 2189 | flavor. |
| 2190 | |
| 2191 | In addition, NumPy-style point selections are supported. In |
| 2192 | particular, if key is a list of row coordinates, the set of rows |
| 2193 | determined by it is returned. Furthermore, if key is an array of |
| 2194 | boolean values, only the coordinates where key is True are returned. |
| 2195 | Note that for the latter to work it is necessary that key list would |
| 2196 | contain exactly as many rows as the table has. |
| 2197 | |
| 2198 | Examples |
| 2199 | -------- |
| 2200 | :: |
| 2201 | |
| 2202 | record = table[4] |
| 2203 | recarray = table[4:1000:2] |
| 2204 | recarray = table[[4,1000]] # only retrieves rows 4 and 1000 |
| 2205 | recarray = table[[True, False, ..., True]] |
| 2206 | |
| 2207 | Those statements are equivalent to:: |
| 2208 | |
| 2209 | record = table.read(start=4)[0] |
| 2210 | recarray = table.read(start=4, stop=1000, step=2) |
| 2211 | recarray = table.read_coordinates([4,1000]) |
| 2212 | recarray = table.read_coordinates([True, False, ..., True]) |
| 2213 | |
| 2214 | Here, you can see how indexing can be used as a shorthand for the |
| 2215 | :meth:`Table.read` and :meth:`Table.read_coordinates` methods. |
| 2216 | |
| 2217 | """ |
| 2218 | self._g_check_open() |
| 2219 | |
| 2220 | if is_idx(key): |
| 2221 | key = operator.index(key) |
| 2222 | |
| 2223 | # Index out of range protection |
| 2224 | if key >= self.nrows: |
| 2225 | raise IndexError("Index out of range") |
| 2226 | if key < 0: |
| 2227 | # To support negative values |
| 2228 | key += self.nrows |
| 2229 | start, stop, step = self._process_range(key, key + 1, 1) |
| 2230 | return self.read(start, stop, step)[0] |
| 2231 | elif isinstance(key, slice): |
| 2232 | start, stop, step = self._process_range( |
| 2233 | key.start, key.stop, key.step |
| 2234 | ) |
| 2235 | return self.read(start, stop, step) |
| 2236 | # Try with a boolean or point selection |
| 2237 | elif type(key) in (list, tuple) or isinstance(key, np.ndarray): |
| 2238 | return self._read_coordinates(key, None) |
nothing calls this directly
no test coverage detected