-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
30 lines (25 loc) · 1.09 KB
/
utils.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
import datetime
start_date = datetime.datetime.strptime('2010-01-01', '%Y-%m-%d')
def parse_dt(x):
if not isinstance(x, str):
return start_date
elif len(x) == len('2010-01-01'):
return datetime.datetime.strptime(x, '%Y-%m-%d')
elif len(x) == len('2010-01-01 10:10:10'):
return datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
else:
return start_date
def transform_datetime_features(df):
datetime_columns = [
col_name
for col_name in df.columns
if col_name.startswith('datetime')
]
for col_name in datetime_columns:
df[col_name] = df[col_name].apply(lambda x: parse_dt(x))
df['number_year_{}'.format(col_name)] = df[col_name].apply(lambda x: x.year)
df['number_weekday_{}'.format(col_name)] = df[col_name].apply(lambda x: x.weekday())
df['number_month_{}'.format(col_name)] = df[col_name].apply(lambda x: x.month)
df['number_day_{}'.format(col_name)] = df[col_name].apply(lambda x: x.day)
df['number_hour_{}'.format(col_name)] = df[col_name].apply(lambda x: x.hour)
return df