Index DateTimeColumn
5517 2015-04-20 15:27:30
5674 2015-04-20 15:37:30
5675 2015-04-20 15:37:30
5917 2015-04-20 15:49:30
5919 2015-04-20 15:49:30
6619 2015-04-20 16:18:12
6652 2015-04-20 16:36:30
6696 2015-04-20 16:21:42
我希望从15:27:30到20分钟产生一些时间范围。或者找到第二天..有人可以建议我执行此任务的代码段吗?
答案 0 :(得分:1)
我认为您可以Day
使用Minute
或print df['DateTimeColumn'] + pd.offsets.Minute(20)
0 2015-04-20 15:47:30
1 2015-04-20 15:57:30
2 2015-04-20 15:57:30
3 2015-04-20 16:09:30
4 2015-04-20 16:09:30
5 2015-04-20 16:38:12
6 2015-04-20 16:56:30
7 2015-04-20 16:41:42
Name: DateTimeColumn, dtype: datetime64[ns]
print df['DateTimeColumn'] + pd.offsets.Day()
0 2015-04-21 15:27:30
1 2015-04-21 15:37:30
2 2015-04-21 15:37:30
3 2015-04-21 15:49:30
4 2015-04-21 15:49:30
5 2015-04-21 16:18:12
6 2015-04-21 16:36:30
7 2015-04-21 16:21:42
Name: DateTimeColumn, dtype: datetime64[ns]
:
columns
您可以创建新的df['Minutes20'] = df['DateTimeColumn'] + pd.offsets.Minute(20)
df['NextDay'] = df['DateTimeColumn'] + pd.offsets.Day()
print df
Index DateTimeColumn Minutes20 NextDay
0 5517 2015-04-20 15:27:30 2015-04-20 15:47:30 2015-04-21 15:27:30
1 5674 2015-04-20 15:37:30 2015-04-20 15:57:30 2015-04-21 15:37:30
2 5675 2015-04-20 15:37:30 2015-04-20 15:57:30 2015-04-21 15:37:30
3 5917 2015-04-20 15:49:30 2015-04-20 16:09:30 2015-04-21 15:49:30
4 5919 2015-04-20 15:49:30 2015-04-20 16:09:30 2015-04-21 15:49:30
5 6619 2015-04-20 16:18:12 2015-04-20 16:38:12 2015-04-21 16:18:12
6 6652 2015-04-20 16:36:30 2015-04-20 16:56:30 2015-04-21 16:36:30
7 6696 2015-04-20 16:21:42 2015-04-20 16:41:42 2015-04-21 16:21:42
:
Day
您可以通过pd.offsets找到下一个#df['Minutes20'] = df['DateTimeColumn'] + pd.offsets.Minute(20)
df['NextDay'] = df['DateTimeColumn'] + pd.offsets.Day()
df['NextDayNorm'] = df['NextDay'].dt.normalize()
df['NextDayDiff'] = df['NextDay'].dt.normalize() - df['DateTimeColumn']
print df
Index DateTimeColumn NextDay NextDayNorm NextDayDiff
0 5517 2015-04-20 15:27:30 2015-04-21 15:27:30 2015-04-21 08:32:30
1 5674 2015-04-20 15:37:30 2015-04-21 15:37:30 2015-04-21 08:22:30
2 5675 2015-04-20 15:37:30 2015-04-21 15:37:30 2015-04-21 08:22:30
3 5917 2015-04-20 15:49:30 2015-04-21 15:49:30 2015-04-21 08:10:30
4 5919 2015-04-20 15:49:30 2015-04-21 15:49:30 2015-04-21 08:10:30
5 6619 2015-04-20 16:18:12 2015-04-21 16:18:12 2015-04-21 07:41:48
6 6652 2015-04-20 16:36:30 2015-04-21 16:36:30 2015-04-21 07:23:30
7 6696 2015-04-20 16:21:42 2015-04-21 16:21:42 2015-04-21 07:38:18
的差异:
DateTimeColumn
如果您想按时间范围进行选择,请先将列print df
Index DateTimeColumn
0 5517 2015-04-20 15:27:30
1 5674 2015-04-20 15:37:30
2 5675 2015-04-20 15:37:30
3 5917 2015-04-20 15:49:30
4 5675 2015-04-21 15:37:30
5 5917 2015-04-22 15:49:30
6 5919 2015-04-23 15:49:30
7 6619 2015-04-20 16:18:12
8 6652 2015-04-20 16:36:30
9 6696 2015-04-20 16:21:42
start = pd.to_datetime('15:27:30').time()
end = pd.to_datetime('15:47:30').time()
print start
15:27:30
print end
15:47:30
df.index = df['DateTimeColumn']
print df.ix[df.index.indexer_between_time(start, end)]
Index DateTimeColumn
DateTimeColumn
2015-04-20 15:27:30 5517 2015-04-20 15:27:30
2015-04-20 15:37:30 5674 2015-04-20 15:37:30
2015-04-20 15:37:30 5675 2015-04-20 15:37:30
2015-04-21 15:37:30 5675 2015-04-21 15:37:30
设为索引,然后设置normalize
:
start
您可以按end
创建offset
和startdate = pd.to_datetime('15:27:30')
enddate = startdate + pd.offsets.Minute(20)
start = startdate.time()
end = enddate.time()
print start
15:27:30
print end
15:47:30
时间:
usps 75078