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

Method __getitem__

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

Source from the content-addressed store, hash-verified

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