[英]how to convert multiple if else loop to list comprehension in python
我正在使用以下if循环来创建大约一百万个桶,大约需要100万次观察,这需要花费很多时间。 以下是我的if循环
def half_hourly_buckets(dataframe,time_column):
dataframe[time_column] = pd.to_datetime(dataframe[time_column],format = '%H:%M:%S').dt.time
for j in range(len(dataframe)):
x = dataframe.loc[j,time_column]
if (x >= datetime.time(0,0,1)) & (x <= datetime.time(0,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "00:00:01 - 00:30:00"
elif (x >= datetime.time(0,30,1)) & (x <= datetime.time(1,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "00:30:01 - 01:00:00"
elif (x >= datetime.time(1,0,1)) & (x <= datetime.time(1,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "01:00:01 - 01:30:00"
elif (x >= datetime.time(1,30,1)) & (x <= datetime.time(2,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "01:30:01 - 02:00:00"
elif (x >= datetime.time(2,0,1)) & (x <= datetime.time(2,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "02:00:01 - 02:30:00"
elif (x >= datetime.time(2,30,1)) & (x <= datetime.time(3,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "02:30:01 - 03:00:00"
elif (x >= datetime.time(3,0,1)) & (x <= datetime.time(3,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "03:00:01 - 03:30:00"
elif (x >= datetime.time(3,30,1)) & (x <= datetime.time(4,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "03:30:01 - 04:00:00"
elif (x >= datetime.time(4,0,1)) & (x <= datetime.time(4,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "04:00:01 - 04:30:00"
elif (x >= datetime.time(4,30,1)) & (x <= datetime.time(5,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "04:30:01 - 05:00:00"
elif (x >= datetime.time(5,0,1)) & (x <= datetime.time(5,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "05:00:01 - 05:30:00"
elif (x >= datetime.time(5,30,1)) & (x <= datetime.time(6,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "05:30:01 - 06:00:00"
elif (x >= datetime.time(6,0,1)) & (x <= datetime.time(6,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "06:00:01 - 06:30:00"
elif (x >= datetime.time(6,30,1)) & (x <= datetime.time(7,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "06:30:01 - 07:00:00"
elif (x >= datetime.time(7,0,1)) & (x <= datetime.time(7,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "07:00:01 - 07:30:00"
elif (x >= datetime.time(7,30,1)) & (x <= datetime.time(8,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "07:30:01 - 08:00:00"
elif (x >= datetime.time(8,0,1)) & (x <= datetime.time(8,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "08:00:01 - 08:30:00"
elif (x >= datetime.time(8,30,1)) & (x <= datetime.time(9,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "08:30:01 - 09:00:00"
elif (x >= datetime.time(9,0,1)) & (x <= datetime.time(9,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "09:00:01 - 09:30:00"
elif (x >= datetime.time(9,30,1)) & (x <= datetime.time(10,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "09:30:01 - 10:00:00"
elif (x >= datetime.time(10,0,1)) & (x <= datetime.time(10,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "10:00:01 - 10:30:00"
elif (x >= datetime.time(10,30,1)) & (x <= datetime.time(11,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "10:30:01 - 11:00:00"
elif (x >= datetime.time(11,0,1)) & (x <= datetime.time(11,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "11:00:01 - 11:30:00"
elif (x >= datetime.time(11,30,1)) & (x <= datetime.time(12,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "11:30:01 - 12:00:00"
elif (x >= datetime.time(12,0,1)) & (x <= datetime.time(12,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "12:00:01 - 12:30:00"
elif (x >= datetime.time(12,30,1)) & (x <= datetime.time(13,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "12:30:01 - 13:00:00"
elif (x >= datetime.time(13,0,1)) & (x <= datetime.time(13,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "13:00:01 - 13:30:00"
elif (x >= datetime.time(13,30,1)) & (x <= datetime.time(14,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "13:30:01 - 14:00:00"
elif (x >= datetime.time(14,0,1)) & (x <= datetime.time(14,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "14:00:01 - 14:30:00"
elif (x >= datetime.time(14,30,1)) & (x <= datetime.time(15,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "14:30:01 - 15:00:00"
elif (x >= datetime.time(15,0,1)) & (x <= datetime.time(15,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "15:00:01 - 15:30:00"
elif (x >= datetime.time(15,30,1)) & (x <= datetime.time(16,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "15:30:01 - 16:00:00"
elif (x >= datetime.time(16,0,1)) & (x <= datetime.time(16,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "16:00:01 - 16:30:00"
elif (x >= datetime.time(16,30,1)) & (x <= datetime.time(17,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "16:30:01 - 17:00:00"
elif (x >= datetime.time(17,0,1)) & (x <= datetime.time(17,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "17:00:01 - 17:30:00"
elif (x >= datetime.time(17,30,1)) & (x <= datetime.time(18,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "17:30:01 - 18:00:00"
elif (x >= datetime.time(18,0,1)) & (x <= datetime.time(18,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "18:00:01 - 18:30:00"
elif (x >= datetime.time(18,30,1)) & (x <= datetime.time(19,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "18:30:01 - 19:00:00"
elif (x >= datetime.time(19,0,1)) & (x <= datetime.time(19,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "19:00:01 - 19:30:00"
elif (x >= datetime.time(19,30,1)) & (x <= datetime.time(20,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "19:30:01 - 20:00:00"
elif (x >= datetime.time(20,0,1)) & (x <= datetime.time(20,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "20:00:01 - 20:30:00"
elif (x >= datetime.time(20,30,1)) & (x <= datetime.time(21,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "20:30:01 - 21:00:00"
elif (x >= datetime.time(21,0,1)) & (x <= datetime.time(21,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "21:00:01 - 21:30:00"
elif (x >= datetime.time(21,30,1)) & (x <= datetime.time(22,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "21:30:01 - 22:00:00"
elif (x >= datetime.time(22,0,1)) & (x <= datetime.time(22,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "22:00:01 - 22:30:00"
elif (x >= datetime.time(22,30,1)) & (x <= datetime.time(23,0,0)):
dataframe.loc[j,'half_hourly_bucket'] = "22:30:01 - 23:00:00"
elif (x >= datetime.time(23,0,1)) & (x <= datetime.time(23,30,0)):
dataframe.loc[j,'half_hourly_bucket'] = "23:00:01 - 23:30:00"
else:
dataframe.loc[j,'half_hourly_bucket'] = "23:30:01 - 00:00:00"
return dataframe
有没有办法避免这种循环并提高处理速度?
首先,您进行此方法所需的比较次数约为两倍。 如果你没有通过第一次测试,你就已经知道了
x >= datetime.time(0,30,1))
所以你不必在下一个elif
二次测试这个。
其次,由于您正在使用常规桶,您可以通过获取秒数并使用除以30分钟的结果的整数部分来计算出您需要的桶。 假设x
是时间对象,你可以这样做:
bucket_number = int((datetime.datetime.combine(datetime.date.min, x) -
datetime.datetime.combine(datetime.date.min, datetime.time(0))
).total_seconds() / (30 * 60))
bucket_start = datetime.datetime.combine(datetime.date.min, datetime.time(0)) + \
datetime.timedelta(seconds = bucket_number * 30 * 60)
bucket_end = datetime.datetime.combine(datetime.date.min, datetime.time(0)) + \
datetime.timedelta(seconds = (bucket_number + 1) * 30 * 60)
dataframe.loc[j,'half_hourly_bucket'] = "{} - {}".format(bucket_start.strftime('%H:%M:%S'),
bucket_end.strftime('%H:%M:%S'))
这将消除任何测试的需要。
注意:这里的许多艰苦工作是因为很难处理time
对象。 如果你可以使用datetime
对象,那么这将更容易。
您可以采用不同的方法并使用timedelta
来定义存储桶,这会严重简化此代码:
from datetime import datetime, timedelta
def ceil_dt(dt, delta):
return dt + (datetime.min - dt) % delta
def floor_dt(dt, delta):
return dt - (dt - datetime.min) % delta
now = datetime.now()
print(now)
print(
floor_dt(now, timedelta(minutes=30)), ceil_dt(now, timedelta(minutes=30))
)
https://repl.it/@ryanpcmcquen/TwinHelplessLifecycles-1
floor_dt
为您提供了存储桶的开头, ceil_dt
为您提供了结束。
你根本不需要案件。
x = dataframe.loc[j,time_column]
if x > datetime.time(23,30,0):
dataframe.loc[j,'half_hourly_bucket'] = "23:30:01 - 00:00:00"
else:
past = x.minute >= 30
min_hour = str(x.hour).zfill(2)
max_hour = str(x.hour+1 if past else x.hour).zfill(2)
min_time = ':30:01' if past else ':00:01'
max_time = ':00:00' if past else ':30:00'
dataframe.loc[j,'half_hourly_bucket'] = min_hour+min_time+' - '+max_hour+max_time
首先,将所有时间桶的排序列表作为元组:
>>> times=[('00:00:01', '00:30:00'), ('00:30:01', '01:00:00'), ('01:00:01', '01:30:00'), ('01:30:01', '02:00:00'), ('02:00:01', '02:30:00'), ('02:30:01', '03:00:00'), ('03:00:01', '03:30:00'), ('03:30:01', '04:00:00'), ('04:00:01', '04:30:00'), ('04:30:01', '05:00:00'), ('05:00:01', '05:30:00'), ('05:30:01', '06:00:00'), ('06:00:01', '06:30:00'), ('06:30:01', '07:00:00'), ('07:00:01', '07:30:00'), ('07:30:01', '08:00:00'), ('08:00:01', '08:30:00'), ('08:30:01', '09:00:00'), ('09:00:01', '09:30:00'), ('09:30:01', '10:00:00'), ('10:00:01', '10:30:00'), ('10:30:01', '11:00:00'), ('11:00:01', '11:30:00'), ('11:30:01', '12:00:00'), ('12:00:01', '12:30:00'), ('12:30:01', '13:00:00'), ('13:00:01', '13:30:00'), ('13:30:01', '14:00:00'), ('14:00:01', '14:30:00'), ('14:30:01', '15:00:00'), ('15:00:01', '15:30:00'), ('15:30:01', '16:00:00'), ('16:00:01', '16:30:00'), ('16:30:01', '17:00:00'), ('17:00:01', '17:30:00'), ('17:30:01', '18:00:00'), ('18:00:01', '18:30:00'), ('18:30:01', '19:00:00'), ('19:00:01', '19:30:00'), ('19:30:01', '20:00:00'), ('20:00:01', '20:30:00'), ('20:30:01', '21:00:00'), ('21:00:01', '21:30:00'), ('21:30:01', '22:00:00'), ('22:00:01', '22:30:00'), ('22:30:01', '23:00:00'), ('23:00:01', '23:30:00'), ('23:30:01', '00:00:00')]
然后是基于右手值的索引:
>>> idx=[t[1] for t in times]
然后,您可以使用bisect模块选择正确的存储桶:
>>> times[bisect.bisect_left(idx,'00:31:00')]
('00:30:01', '01:00:00')
鉴于00:00:00
值在最后一个桶中(如您编写的示例),您将单独测试:
>>> bucket = ('23:30:01', '00:00:00') if ts=='00:00:00' else times[bisect.bisect_left(idx, ts)]
bisect模块非常快速有效。 也许比其他原生Python方法快几百倍来做类似的事情。
你可以使用pandas cut将数据分成半小时的间隔
dates = pd.date_range(end = '09/18/2018', periods = 10000, freq='S')
df = pd.DataFrame({'datetime': np.random.choice(dates, 10000)})
bins = pd.date_range(df.datetime.dt.floor('30T').min(), df.datetime.dt.ceil('30T').max(), freq = '30T')
pd.cut(df.datetime, bins = bins)
你得到
0 (2018-09-17 22:30:00, 2018-09-17 23:00:00]
1 (2018-09-17 22:00:00, 2018-09-17 22:30:00]
2 (2018-09-17 21:00:00, 2018-09-17 21:30:00]
3 (2018-09-17 21:30:00, 2018-09-17 22:00:00]
4 (2018-09-17 22:00:00, 2018-09-17 22:30:00]
5 (2018-09-17 23:30:00, 2018-09-18]
6 (2018-09-17 22:00:00, 2018-09-17 22:30:00]
7 (2018-09-17 22:00:00, 2018-09-17 22:30:00]
可以轻松地将分组分组以进行聚合
dates = pd.date_range(end = '09/18/2018', periods = 10000, freq='S')
df = pd.DataFrame({'id' : np.random.randint(10, 1000, 10000),'datetime': np.random.choice(dates, 10000)})
bins = pd.date_range(df.datetime.dt.floor('30T').min(), df.datetime.dt.ceil('30T').max(), freq = '30T')
df.groupby(['id', pd.cut(df.datetime, bins = bins)]).size()
id datetime
10 (2018-09-17 21:00:00, 2018-09-17 21:30:00] 2
(2018-09-17 21:30:00, 2018-09-17 22:00:00] 1
(2018-09-17 22:00:00, 2018-09-17 22:30:00] 1
(2018-09-17 22:30:00, 2018-09-17 23:00:00] 3
(2018-09-17 23:00:00, 2018-09-17 23:30:00] 4
11 (2018-09-17 21:30:00, 2018-09-17 22:00:00] 1
(2018-09-17 22:00:00, 2018-09-17 22:30:00] 1
(2018-09-17 22:30:00, 2018-09-17 23:00:00] 1
(2018-09-17 23:30:00, 2018-09-18] 1
您可以使用Pandas中的矢量化操作快速完成此操作。 唯一的技巧是将datetime.time
值转换为Pandas可以使用的时间戳。 这里有一些代码可以在大约4秒内完成整个工作:
import datetime, random
import pandas as pd, numpy as np
# create random times
df = pd.DataFrame({'time': [
datetime.time(int(24 * h), int(60 * m), int(60 * s))
for h, m, s in np.random.rand(500000, 3)
]})
def half_hourly_buckets(dataframe, time_column):
# convert time values to datetimes on arbitrary date
base_date = datetime.datetime(2000, 1, 1)
dt = dataframe[time_column].apply(
lambda t: datetime.datetime.combine(base_date, t)
)
# assign corresponding bins
one_second = pd.Timedelta(seconds=1)
thirty_minutes = pd.Timedelta(minutes=30)
bin = (dt - one_second).dt.floor('30T')
dataframe['half_hourly_bucket'] = (
(bin + one_second).dt.strftime("%H:%M:%S")
+ ' - '
+ (bin + thirty_minutes).dt.strftime("%H:%M:%S")
)
half_hourly_buckets(df, 'time')
df
# time half_hourly_bucket
# 0 07:00:49 07:00:01 - 07:30:00
# 1 06:16:19 06:00:01 - 06:30:00
# 2 06:17:06 06:00:01 - 06:30:00
# 3 17:28:31 17:00:01 - 17:30:00
# ... ... ...
# 739 18:00:01 18:00:01 - 18:30:00
# ... ... ...
# 4259 00:00:00 23:30:01 - 00:00:00
# ... ... ...
# 4520 17:30:00 17:00:01 - 17:30:00
# ... ... ...
您可以通过创建更有效的辅助函数来计算.apply
,然后使用Pandas .apply
函数来调用它,从而非常快速地完成此操作。 这是一个在大约2秒内运行的示例。
请注意,与其他一些答案不同,这会将:00:00和:30:00次移动到前半个小时的bin中,如代码所示。
import datetime, random
import pandas as pd, numpy as np
# create random times
df = pd.DataFrame({'time': [
datetime.time(int(24 * h), int(60 * m), int(60 * s))
for h, m, s in np.random.rand(500000, 3)
]})
def make_bin(t):
h, m, s = t.hour, t.minute, t.second
# move the first second of each half-hour back into the prior bin
if s == 0 and m in {0, 30}:
if m == 0:
h = (h - 1) % 24
m = 59
else:
m = 29
# is this in the first half of the hour?
first_half = m < 30
# calculate bin start and end
start = datetime.time(h, 0 if first_half else 30, 1)
end = datetime.time(
h if first_half else (h + 1) % 24,
30 if first_half else 0,
0
)
return '{} - {}'.format(start, end)
def half_hourly_buckets(dataframe, time_column):
dataframe['half_hourly_bucket'] = dataframe[time_column].apply(make_bin)
half_hourly_buckets(df, 'time')
df
# time half_hourly_bucket
# 0 02:07:13 02:00:01 - 02:30:00
# 1 21:52:35 21:30:01 - 22:00:00
# 2 07:41:36 07:30:01 - 08:00:00
# 3 20:41:36 20:30:01 - 21:00:00
# ...
# 761 17:00:00 16:30:01 - 17:00:00
# 1460 17:30:00 17:00:01 - 17:30:00
# ...
# 219253 00:00:00 23:30:01 - 00:00:00
# ...
为了给你的问题提供最直接的答案:你可以将bin计算代码移动到辅助函数中,然后使用Pandas的.apply()
方法将它应用于一系列的所有元素。 这比你的循环运行得快得多,主要是因为你当前使用的增量分配非常慢。 根据您的原始代码,有一个如何执行此操作的示例。
此代码在我的计算机上大约5秒内处理500,000行。 您可以使用更有效的方法来计算垃圾箱,从而进一步加快速度; 我在另一个答案中举了一个例子,大约需要2秒才能运行。
df['half_hourly_bucket'] = df['time'].apply(make_bin)
其中make_bin()
是通过定义的
def make_bin(x):
if (x >= datetime.time(0,0,1)) & (x <= datetime.time(0,30,0)):
bin = "00:00:01 - 00:30:00"
elif (x >= datetime.time(0,30,1)) & (x <= datetime.time(1,0,0)):
bin = "00:30:01 - 01:00:00"
elif (x >= datetime.time(1,0,1)) & (x <= datetime.time(1,30,0)):
bin = "01:00:01 - 01:30:00"
elif (x >= datetime.time(1,30,1)) & (x <= datetime.time(2,0,0)):
bin = "01:30:01 - 02:00:00"
elif (x >= datetime.time(2,0,1)) & (x <= datetime.time(2,30,0)):
bin = "02:00:01 - 02:30:00"
elif (x >= datetime.time(2,30,1)) & (x <= datetime.time(3,0,0)):
bin = "02:30:01 - 03:00:00"
elif (x >= datetime.time(3,0,1)) & (x <= datetime.time(3,30,0)):
bin = "03:00:01 - 03:30:00"
elif (x >= datetime.time(3,30,1)) & (x <= datetime.time(4,0,0)):
bin = "03:30:01 - 04:00:00"
elif (x >= datetime.time(4,0,1)) & (x <= datetime.time(4,30,0)):
bin = "04:00:01 - 04:30:00"
elif (x >= datetime.time(4,30,1)) & (x <= datetime.time(5,0,0)):
bin = "04:30:01 - 05:00:00"
elif (x >= datetime.time(5,0,1)) & (x <= datetime.time(5,30,0)):
bin = "05:00:01 - 05:30:00"
elif (x >= datetime.time(5,30,1)) & (x <= datetime.time(6,0,0)):
bin = "05:30:01 - 06:00:00"
elif (x >= datetime.time(6,0,1)) & (x <= datetime.time(6,30,0)):
bin = "06:00:01 - 06:30:00"
elif (x >= datetime.time(6,30,1)) & (x <= datetime.time(7,0,0)):
bin = "06:30:01 - 07:00:00"
elif (x >= datetime.time(7,0,1)) & (x <= datetime.time(7,30,0)):
bin = "07:00:01 - 07:30:00"
elif (x >= datetime.time(7,30,1)) & (x <= datetime.time(8,0,0)):
bin = "07:30:01 - 08:00:00"
elif (x >= datetime.time(8,0,1)) & (x <= datetime.time(8,30,0)):
bin = "08:00:01 - 08:30:00"
elif (x >= datetime.time(8,30,1)) & (x <= datetime.time(9,0,0)):
bin = "08:30:01 - 09:00:00"
elif (x >= datetime.time(9,0,1)) & (x <= datetime.time(9,30,0)):
bin = "09:00:01 - 09:30:00"
elif (x >= datetime.time(9,30,1)) & (x <= datetime.time(10,0,0)):
bin = "09:30:01 - 10:00:00"
elif (x >= datetime.time(10,0,1)) & (x <= datetime.time(10,30,0)):
bin = "10:00:01 - 10:30:00"
elif (x >= datetime.time(10,30,1)) & (x <= datetime.time(11,0,0)):
bin = "10:30:01 - 11:00:00"
elif (x >= datetime.time(11,0,1)) & (x <= datetime.time(11,30,0)):
bin = "11:00:01 - 11:30:00"
elif (x >= datetime.time(11,30,1)) & (x <= datetime.time(12,0,0)):
bin = "11:30:01 - 12:00:00"
elif (x >= datetime.time(12,0,1)) & (x <= datetime.time(12,30,0)):
bin = "12:00:01 - 12:30:00"
elif (x >= datetime.time(12,30,1)) & (x <= datetime.time(13,0,0)):
bin = "12:30:01 - 13:00:00"
elif (x >= datetime.time(13,0,1)) & (x <= datetime.time(13,30,0)):
bin = "13:00:01 - 13:30:00"
elif (x >= datetime.time(13,30,1)) & (x <= datetime.time(14,0,0)):
bin = "13:30:01 - 14:00:00"
elif (x >= datetime.time(14,0,1)) & (x <= datetime.time(14,30,0)):
bin = "14:00:01 - 14:30:00"
elif (x >= datetime.time(14,30,1)) & (x <= datetime.time(15,0,0)):
bin = "14:30:01 - 15:00:00"
elif (x >= datetime.time(15,0,1)) & (x <= datetime.time(15,30,0)):
bin = "15:00:01 - 15:30:00"
elif (x >= datetime.time(15,30,1)) & (x <= datetime.time(16,0,0)):
bin = "15:30:01 - 16:00:00"
elif (x >= datetime.time(16,0,1)) & (x <= datetime.time(16,30,0)):
bin = "16:00:01 - 16:30:00"
elif (x >= datetime.time(16,30,1)) & (x <= datetime.time(17,0,0)):
bin = "16:30:01 - 17:00:00"
elif (x >= datetime.time(17,0,1)) & (x <= datetime.time(17,30,0)):
bin = "17:00:01 - 17:30:00"
elif (x >= datetime.time(17,30,1)) & (x <= datetime.time(18,0,0)):
bin = "17:30:01 - 18:00:00"
elif (x >= datetime.time(18,0,1)) & (x <= datetime.time(18,30,0)):
bin = "18:00:01 - 18:30:00"
elif (x >= datetime.time(18,30,1)) & (x <= datetime.time(19,0,0)):
bin = "18:30:01 - 19:00:00"
elif (x >= datetime.time(19,0,1)) & (x <= datetime.time(19,30,0)):
bin = "19:00:01 - 19:30:00"
elif (x >= datetime.time(19,30,1)) & (x <= datetime.time(20,0,0)):
bin = "19:30:01 - 20:00:00"
elif (x >= datetime.time(20,0,1)) & (x <= datetime.time(20,30,0)):
bin = "20:00:01 - 20:30:00"
elif (x >= datetime.time(20,30,1)) & (x <= datetime.time(21,0,0)):
bin = "20:30:01 - 21:00:00"
elif (x >= datetime.time(21,0,1)) & (x <= datetime.time(21,30,0)):
bin = "21:00:01 - 21:30:00"
elif (x >= datetime.time(21,30,1)) & (x <= datetime.time(22,0,0)):
bin = "21:30:01 - 22:00:00"
elif (x >= datetime.time(22,0,1)) & (x <= datetime.time(22,30,0)):
bin = "22:00:01 - 22:30:00"
elif (x >= datetime.time(22,30,1)) & (x <= datetime.time(23,0,0)):
bin = "22:30:01 - 23:00:00"
elif (x >= datetime.time(23,0,1)) & (x <= datetime.time(23,30,0)):
bin = "23:00:01 - 23:30:00"
else:
bin = "23:30:01 - 00:00:00"
return bin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.