(
expr: Expr,
parent: Expr,
dependents: dict[str, Collection[weakref.ref[BaseExpr]]],
additional_columns: list | None = None,
)
| 3851 | |
| 3852 | |
| 3853 | def determine_column_projection( |
| 3854 | expr: Expr, |
| 3855 | parent: Expr, |
| 3856 | dependents: dict[str, Collection[weakref.ref[BaseExpr]]], |
| 3857 | additional_columns: list | None = None, |
| 3858 | ) -> object: |
| 3859 | if isinstance(parent, Index): |
| 3860 | column_union = [] |
| 3861 | else: |
| 3862 | column_union = parent.columns.copy() |
| 3863 | parents: list[Expr] |
| 3864 | parents = [inst for x in dependents[expr._name] if isinstance((inst := x()), Expr)] |
| 3865 | |
| 3866 | seen = set() |
| 3867 | for p in parents: |
| 3868 | if p._name in seen: |
| 3869 | continue |
| 3870 | seen.add(p._name) |
| 3871 | column_union.extend(p._projection_columns) |
| 3872 | |
| 3873 | if additional_columns is not None: |
| 3874 | column_union.extend(flatten(additional_columns, container=list)) |
| 3875 | |
| 3876 | # We can end up with MultiIndex columns from groupby ops, needs to be |
| 3877 | # accounted for in the sort |
| 3878 | flattened_columns = set(column_union) |
| 3879 | try: |
| 3880 | column_union = sorted(flattened_columns) |
| 3881 | except TypeError: |
| 3882 | # mixed type columns |
| 3883 | column_union = _sort_mixed(pd.Index(list(flattened_columns))).tolist() |
| 3884 | if ( |
| 3885 | len(column_union) == 1 |
| 3886 | and parent.ndim == 1 |
| 3887 | and all(p.ndim == 1 for p in parents) |
| 3888 | ): |
| 3889 | return column_union[0] |
| 3890 | return column_union |
| 3891 | |
| 3892 | |
| 3893 | def _sort_mixed(values): |
no test coverage detected