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