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

Class Projection

dask/dataframe/dask_expr/_expr.py:2153–2225  ·  view source on GitHub ↗

Column Selection

Source from the content-addressed store, hash-verified

2151
2152
2153class Projection(Elemwise):
2154 """Column Selection"""
2155
2156 _parameters = ["frame", "columns"]
2157 operation = operator.getitem
2158
2159 @functools.cached_property
2160 def unique_partition_mapping_columns_from_shuffle(self):
2161 col_op = self.operand("columns")
2162 columns = set(col_op) if isinstance(col_op, list) else {col_op}
2163 return {
2164 c
2165 for c in self.frame.unique_partition_mapping_columns_from_shuffle
2166 if c in columns or isinstance(c, tuple) and set(c).issubset(columns)
2167 }
2168
2169 @property
2170 def columns(self):
2171 cols = self.operand("columns")
2172 if isinstance(cols, list):
2173 return cols
2174 elif isinstance(cols, pd.Index):
2175 return list(cols)
2176 else:
2177 return [cols]
2178
2179 @functools.cached_property
2180 def _meta(self):
2181 if is_dataframe_like(self.frame._meta):
2182 return super()._meta
2183 # if we are not a DataFrame and have a scalar, we reduce to a scalar
2184 if not isinstance(self.operand("columns"), (list, slice)) and not hasattr(
2185 self.operand("columns"), "dtype"
2186 ):
2187 return meta_nonempty(self.frame._meta).iloc[0]
2188 # Avoid column selection for Series/Index
2189 return self.frame._meta
2190
2191 def _node_label_args(self):
2192 return [self.frame, self.operand("columns")]
2193
2194 def __str__(self):
2195 base = str(self.frame)
2196 if " " in base:
2197 base = "(" + base + ")"
2198 return f"{base}[{repr(self.operand('columns'))}]"
2199
2200 def _divisions(self):
2201 if self.ndim == 0:
2202 return (None, None)
2203 return super()._divisions()
2204
2205 def _simplify_down(self):
2206 if (
2207 str(self.frame.columns) == str(self.columns)
2208 and self._meta.ndim == self.frame._meta.ndim
2209 ):
2210 # TODO: we should get more precise around Expr.columns types

Callers 12

_lowerMethod · 0.90
__getitem__Method · 0.90
_locMethod · 0.90
_loc_seriesMethod · 0.90
_lowerMethod · 0.90
_lowerMethod · 0.90
_simplify_downMethod · 0.90
__getitem__Method · 0.85
_simplify_upMethod · 0.85
_simplify_downMethod · 0.85
_simplify_downMethod · 0.85
_simplify_upMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected