0
我是熊貓新手,並且對此有問題。我有一個簡單的表:將熊貓數據框中的日期時間添加到基於列的現有行
date period_number
0 2016-10-26 1
1 2016-10-26 3
2 2016-10-26 5
3 2016-10-27 1
我想創建一個開始和結束時間3大小相等的週期,就像這樣:
date period_number start end
0 2016-10-26 1 2016-10-26 00:00 2016-10-26 08:00
1 2016-10-26 3 2016-10-26 08:00 2016-10-26 16:00
2 2016-10-26 5 2016-10-26 16:00 2016-10-27 00:00
3 2016-10-27 1 2016-10-27 00:00 2016-10-27 08:00
我試圖做這樣的事情:
for (period, group) in df.groupby('period'):
if period == 1:
group['start'] = group['date']
group['end'] = group['date'] + timedelta(hours=8)
if period == 3:
group['start'] = group['date'] + timedelta(hours=8)
group['end'] = group['date'] + timedelta(hours=16)
if period == 5:
group['start'] = group['date'] + timedelta(hours=16)
group['end'] = group['date'] + timedelta(days=1)
但我收到錯誤:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
也試過,但顯然是錯誤的,並得到同樣的錯誤:
df[df['period'] == 1]['end'] = df['date'] + timedelta(hours=8)
謝謝,我也意識到,數據始終是爲了讓我能其實只是創建帶freq ='8H'的date_range並將其添加到數據框 – eggbert
使事情變得更容易;) – Skirrebattie