-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path10_miscellaneous_decompose_date.py
71 lines (65 loc) · 3.07 KB
/
10_miscellaneous_decompose_date.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def decompose_date(df, analyse_col):
print(analyse_col)
import pandas as pd
clean_col = analyse_col.replace(" ", "_")
from collections import OrderedDict
weekdays = OrderedDict( {0: "lundi", 1: "mardi", 2: "mercredi",
3: "jeudi", 4: "vendredi", 5: "samedi", 6: "dimanche"})
mois = OrderedDict({1: "janvier" , 2: "février" , 3: "mars", 4: "avril", 5: "mai",
6: "juin" , 7: "juillet" , 8: "août",
9: "septembre", 10: "octobre", 11: "novembre", 12: "décembre"})
filtre = ~df[analyse_col].isnull()
#
new_col = "%s_jour_semaine" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: x.weekday()).map(weekdays)
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=weekdays.values(), ordered=True)
#
new_col = "%s_jour_semaine_int" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[analyse_col].map(lambda x: x.weekday())
#
new_col = "%s_jour_mois" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: x.day)
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=range(32), ordered=True)
#
new_col = "%s_mois" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: x.month).map(mois)
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=mois.values(), ordered=True)
#
new_col = "%s_nth_mois" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: x.month)
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=range(1, 13), ordered=True)
#
new_col = "%s_nth_semestre" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: 1 if x.month<7 else 2)
#
new_col = "%s_nth_quarter" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: int(1 + (x.month-1)//3 ))
#
new_col = "%s_week" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: int(x.week))
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=range(53), ordered=True)
#
new_col = "%s_year" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: int(x.year))
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=sorted(list(df[filtre][new_col].unique())), ordered=True)
#
new_col = "%s_nth_day" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][analyse_col].map(lambda x: x.timetuple().tm_yday)
df.loc[filtre, new_col] = pd.Categorical(df[filtre][new_col], categories=range(366), ordered=True)
#
year = "%s_year" % clean_col
nth_day = "%s_nth_day" % clean_col
new_col = "%s_year_plus_day" % clean_col
print(new_col)
df.loc[filtre, new_col] = df[filtre][year] + (df[filtre][nth_day] / 365)
print("fini")