#資料集3的特徵為 取 線上資料中領券和用券日期大於3月15日和小於6月30日的
feature3 = off_train[
((off_train.date>=
'20160315')&
(off_train.date<=
'20160630'))
|((off_train.date==
'null')&
(off_train.date_received>=
'20160315')&
(off_train.date_received<=
'20160630'))
]feature3.head(
)#獲取最大日期,最小日期
t2['max_date_received'
]= t2.date_received.
(lambda s:
max(
[int
(d)for d in s.split(
':')])
)t2[
'min_date_received'
]= t2.date_received.
(lambda s:
min(
[int
(d)for d in s.split(
':')])
)t2 = t2[
['user_id'
,'coupon_id'
,'max_date_received'
,'min_date_received'
]]
#顯示時間是第幾周
dataset3[
'day_of_week'
]= dataset3[
'date_received'
].astype(
'str').
(lambda x:date(
int(x[0:
4]),
int(x[4:
6]),
int(x[6:
8]))
.weekday()+
1)#顯示時間是幾月
dataset3[
'day_of_month'
]= dataset3[
'date_received'
].astype(
'str').
(lambda x:
int(x[6:
8]))
#顯示時期和截止日之間的 天數 時間差
dataset3[
'days_distance'
]= dataset3[
'date_received'
].astype(
'str').
(lambda x:
(date(
int(x[0:
4]),
int(x[4:
6]),
int(x[6:
8]))
-date(
2016,6
,30))
.days)
defget_day_gap_before
(s):
date_received,dates = s.split(
'-')
dates = dates.split(
':')
gaps =
for d in dates:
#根據日期求取時間差,天數
this_gap =
(date(
int(date_received[0:
4]),
int(date_received[4:
6]),
int(date_received[6:
8]))
-date(
int(d[0:
4]),
int(d[4:
6]),
int(d[6:
8]))
).days
if this_gap>0:
iflen
(gaps)==0
:return-1
else
:return
min(gaps)
defget_day_gap_after
(s):
date_received,dates = s.split(
'-')
dates = dates.split(
':')
gaps =
for d in dates:
#根據日期求取時間差,天數
this_gap =
(date(
int(d[0:
4]),
int(d[4:
6]),
int(d[6:
8]))
-date(
int(date_received[0:
4]),
int(date_received[4:
6]),
int(date_received[6:
8]))
).days
if this_gap>0:
iflen
(gaps)==0
:return-1
else
:return
min(gaps)
t7 = dataset3[
['user_id'
,'coupon_id'
,'date_received']]
t7 = pd.merge(t7,t6,on=
['user_id'
,'coupon_id'
],how=
'left'
)#將兩個字段拼接,中間使用短橫
先拼接,再查詢
#將資料分組,同組日期使用冒號相連 join
t2 = t2.groupby(
['user_id'
,'coupon_id'])
['date_received'
].agg(
lambda x:
':'.join(x)
).reset_index(
)t2[
'receive_number'
]= t2.date_received.
(lambda s:
len(s.split(
':')))
t2 = t2[t2.receive_number>1]
#計算最大日期和最小日期
t2['max_date_received'
]= t2.date_received.
(lambda s:
max(
[int
(d)for d in s.split(
':')])
)t2[
'min_date_received'
]= t2.date_received.
(lambda s:
min(
[int
(d)for d in s.split(
':')])
)t2 = t2[
['user_id'
,'coupon_id'
,'max_date_received'
,'min_date_received'
]]
日期時間相關操作
當前日期時間的獲取 datetime datetime1 datetime.now 日期時間的運算 string str1 datetime1.addyears 1 tostring 加年份 string str1 datetime1.addmonths 1 tostring 加月份 string ...
Domino日期操作相關
1 將字串格式的日期轉為日期格式 texttotime 例 texttotime 1987 05 18 2 查詢日期 假如被搜尋文件中沒有日期域。用 created.searchstr searchstr webdoc.totime 0 3 公式中日期的比較 date today date adju...
javascript日期相關操作
時間戳轉時間格式 function getmydate str function getzf num return num 時間戳轉時間格式end 格式化日期 yyyy mm dd function formatdate strtime if day 9 return date.getfullyea...