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

Method __getitem__

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

Source from the content-addressed store, hash-verified

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