MCPcopy Create free account
hub / github.com/dask/dask / __getitem__

Method __getitem__

dask/array/core.py:1982–2043  ·  view source on GitHub ↗
(self, index)

Source from the content-addressed store, hash-verified

1980 self._chunks = y.chunks
1981
1982 def __getitem__(self, index):
1983 # Field access, e.g. x['a'] or x[['a', 'b']]
1984 if isinstance(index, str) or (
1985 isinstance(index, list) and index and all(isinstance(i, str) for i in index)
1986 ):
1987 if isinstance(index, str):
1988 dt = self.dtype[index]
1989 else:
1990 dt = np.dtype(
1991 {
1992 "names": index,
1993 "formats": [self.dtype.fields[name][0] for name in index],
1994 "offsets": [self.dtype.fields[name][1] for name in index],
1995 "itemsize": self.dtype.itemsize,
1996 }
1997 )
1998
1999 if dt.shape:
2000 new_axis = list(range(self.ndim, self.ndim + len(dt.shape)))
2001 chunks = self.chunks + tuple((i,) for i in dt.shape)
2002 return self.map_blocks(
2003 getitem, index, dtype=dt.base, chunks=chunks, new_axis=new_axis
2004 )
2005 else:
2006 return self.map_blocks(getitem, index, dtype=dt)
2007
2008 if not isinstance(index, tuple):
2009 index = (index,)
2010
2011 from dask.array.slicing import (
2012 normalize_index,
2013 slice_with_bool_dask_array,
2014 slice_with_int_dask_array,
2015 )
2016
2017 index2 = normalize_index(index, self.shape)
2018 dependencies = {self.name}
2019 for i in index2:
2020 if isinstance(i, Array):
2021 dependencies.add(i.name)
2022
2023 if any(isinstance(i, Array) and i.dtype.kind in "iu" for i in index2):
2024 self, index2 = slice_with_int_dask_array(self, index2)
2025 if any(isinstance(i, Array) and i.dtype == bool for i in index2):
2026 self, index2 = slice_with_bool_dask_array(self, index2)
2027
2028 if all(isinstance(i, slice) and i == slice(None) for i in index2):
2029 return self
2030
2031 out = "getitem-" + tokenize(self, index2)
2032 try:
2033 dsk, chunks = slice_array(out, self.name, self.chunks, index2)
2034 except SlicingNoop:
2035 return self
2036
2037 graph = HighLevelGraph.from_collections(out, dsk, dependencies=[self])
2038
2039 meta = meta_from_array(self._meta, ndim=len(chunks))

Callers

nothing calls this directly

Calls 13

map_blocksMethod · 0.95
allFunction · 0.90
normalize_indexFunction · 0.90
anyFunction · 0.90
slice_arrayFunction · 0.90
meta_from_arrayFunction · 0.90
from_collectionsMethod · 0.80
ArrayClass · 0.70
tokenizeFunction · 0.50
dtypeMethod · 0.45

Tested by

no test coverage detected