(
expr: Expr,
parent: Expr,
dependents: dict[str, Collection[weakref.ref[BaseExpr]]],
additional_columns: list | None = None,
)
| 3895 | |
| 3896 | |
| 3897 | def determine_column_projection( |
| 3898 | expr: Expr, |
| 3899 | parent: Expr, |
| 3900 | dependents: dict[str, Collection[weakref.ref[BaseExpr]]], |
| 3901 | additional_columns: list | None = None, |
| 3902 | ) -> object: |
| 3903 | if isinstance(parent, Index): |
| 3904 | column_union = [] |
| 3905 | else: |
| 3906 | column_union = parent.columns.copy() |
| 3907 | parents: list[Expr] |
| 3908 | parents = [inst for x in dependents[expr._name] if isinstance((inst := x()), Expr)] |
| 3909 | |
| 3910 | seen = set() |
| 3911 | for p in parents: |
| 3912 | if p._name in seen: |
| 3913 | continue |
| 3914 | seen.add(p._name) |
| 3915 | column_union.extend(p._projection_columns) |
| 3916 | |
| 3917 | if additional_columns is not None: |
| 3918 | column_union.extend(flatten(additional_columns, container=list)) |
| 3919 | |
| 3920 | # We can end up with MultiIndex columns from groupby ops, needs to be |
| 3921 | # accounted for in the sort |
| 3922 | flattened_columns = set(column_union) |
| 3923 | try: |
| 3924 | column_union = sorted(flattened_columns) |
| 3925 | except TypeError: |
| 3926 | # mixed type columns |
| 3927 | column_union = _sort_mixed(pd.Index(list(flattened_columns))).tolist() |
| 3928 | if ( |
| 3929 | len(column_union) == 1 |
| 3930 | and parent.ndim == 1 |
| 3931 | and all(p.ndim == 1 for p in parents) |
| 3932 | ): |
| 3933 | return column_union[0] |
| 3934 | return column_union |
| 3935 | |
| 3936 | |
| 3937 | def _sort_mixed(values): |
no test coverage detected