-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
37 lines (30 loc) · 989 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 17 15:30:56 2020
@author: tfletcher
"""
# import numpy as np
# import pandas as pd
# import matplotlib.pyplot as plt
import itertools
from typing import Iterable, List
def iterative_column_selector( col_names: Iterable[str] ) -> List[List[str]]:
n = len( col_names )
it = itertools.chain.from_iterable( [ itertools.combinations( col_names, i ) for i in range(1, n+1) ] )
return [ list( v ) for v in it ]
def if_column_exists( column_names ):
"""
Helper for `ColumnTransformer` to only specify columns that exist in a dataframe `X`.
Parameters
----------
column_names : list
The dataframe columns to select.
Returns
-------
callable
A function that returns a filtered list of column names given `X`.
"""
def selector( X ):
cols = [ name for name in X.columns if name in column_names ]
return cols
return selector