MCPcopy
hub / github.com/dask/dask / __getitem__

Method __getitem__

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

Source from the content-addressed store, hash-verified

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