[英]Adding an hour to the dates that are in a specific period of time Pandas
I have a data frame with two columns, one column is "Date", the other is "Depth(m)" like that我有一个包含两列的数据框,一列是“日期”,另一列是“深度(米)”
Date Depth(m)
0 2015-07-09 13:00:00 2.624
1 2015-07-09 13:15:00 2.686
2 2015-07-09 13:30:00 2.747
3 2015-07-09 13:45:00 2.78
4 2015-07-09 14:00:00 2.826
5 2015-07-09 14:15:00 2.879
6 2015-07-09 14:30:00 2.938
7 2015-07-09 14:45:00 3.005
8 2015-07-09 15:00:00 3.056
9 2015-07-09 15:15:00 3.106
10 2015-07-09 15:30:00 3.173
11 2015-07-09 15:45:00 3.262
12 2015-07-09 16:00:00 3.332
13 2015-07-09 18:15:00 3.35
14 2015-07-09 18:30:00 3.324
15 2015-07-09 18:45:00 3.306
16 2015-07-09 19:00:00 3.299
I want to add an hour to days between 2015-07-09 14:00:00 and 2015-07-09 16:00:00我想在 2015-07-09 14:00:00 和 2015-07-09 16:00:00 之间增加一个小时
Output like that Output 这样的
Date Depth(m)
0 2015-07-09 13:00:00 2.624
1 2015-07-09 13:15:00 2.686
2 2015-07-09 13:30:00 2.747
3 2015-07-09 13:45:00 2.78
4 2015-07-09 15:00:00 2.826
5 2015-07-09 15:15:00 2.879
6 2015-07-09 15:30:00 2.938
7 2015-07-09 15:45:00 3.005
8 2015-07-09 16:00:00 3.056
9 2015-07-09 16:15:00 3.106
10 2015-07-09 16:30:00 3.173
11 2015-07-09 16:45:00 3.262
12 2015-07-09 17:00:00 3.332
13 2015-07-09 18:15:00 3.35
14 2015-07-09 18:30:00 3.324
15 2015-07-09 18:45:00 3.306
16 2015-07-09 19:00:00 3.299
Thank in advance.预先感谢。
Convert Date
column to pandas datetime
if it isn't already, and convert start and stop date values also to pandas datetime, then use np.where
and assign Date
incremented by an hour using pd.to_timedelta
for those which fall in the given range.将
Date
列转换为 pandas datetime
时间(如果尚未转换),并将开始和结束日期值也转换为 pandas 日期时间,然后使用np.where
并使用pd.to_timedelta
为那些落在给定范围内的Date
分配一个小时递增的日期。
df['Date'] = pd.to_datetime(df['Date'])
start = pd.to_datetime('2015-07-09 14:00:00')
stop = pd.to_datetime('2015-07-09 16:00:00')
df['Date'] = np.where((df['Date'].ge(start))&(df['Date'].le(stop)),
df['Date']+pd.to_timedelta(1, 'H'),
df['Date'])
OUTPUT: OUTPUT:
Date Depth(m)
0 2015-07-09 13:00:00 2.624
1 2015-07-09 13:15:00 2.686
2 2015-07-09 13:30:00 2.747
3 2015-07-09 13:45:00 2.780
4 2015-07-09 15:00:00 2.826
5 2015-07-09 15:15:00 2.879
6 2015-07-09 15:30:00 2.938
7 2015-07-09 15:45:00 3.005
8 2015-07-09 16:00:00 3.056
9 2015-07-09 16:15:00 3.106
10 2015-07-09 16:30:00 3.173
11 2015-07-09 16:45:00 3.262
12 2015-07-09 17:00:00 3.332
13 2015-07-09 18:15:00 3.350
14 2015-07-09 18:30:00 3.324
15 2015-07-09 18:45:00 3.306
16 2015-07-09 19:00:00 3.299
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.