-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_preprocess.py
53 lines (40 loc) · 1.48 KB
/
1_preprocess.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Sept 2020 by Chrianna Bharat
Adapted from code by: S Barbieri
'''
import pandas as pd
from sas7bdat import SAS7BDAT
from hyperparameters import Hyperparameters as hp
from pdb import set_trace as bp
def main():
# Load data
print(hp.priv_data_dir)
with SAS7BDAT(hp.priv_data_dir + hp.import_dat + '.sas7bdat') as file:
df = file.to_data_frame()
# Time to event and binary event column
df['TIME'] = df['txdays']
df['EVENT'] = 1-df['cens'] # observed/not-censored
df['presTenureYears'] = df['presTenureMonths']/12
# Check length and number of events before saving
print("Number of rows in 'df'")
print(df.shape)
print("Summary of event variable in 'df'")
print(df.EVENT.value_counts())
# count the number of nan values in each column
print("Print counts of variables with any missingness, and remove if needed...")
na_cnt = df.isnull().sum().to_frame('nulls')
print(na_cnt[na_cnt['nulls']>0])
# drop rows with missing values but save/review for reference
null_data = df[df.isnull().any(axis=1)]
df.dropna(inplace=True)
print("Number of rows in 'df'")
print(df.shape)
print("Summary of event variable in 'df'")
print(df.EVENT.value_counts())
# Save
df.reset_index(drop=True, inplace=True)
print(df.columns)
df.to_feather(hp.data_pp_dir + hp.import_dat + '_pp.feather')
if __name__ == '__main__':
main()