我有一些数据,我想在不同日期的不同时间之间子集,特别是我希望列 df ['event']
具有 1
,如果是时间是在上午9点至上午11点之间,如果是 2
,则时间是在下午3点至下午4点之间, 0
否则。因此,我尝试使用以下行:
df['event'] = np.where( (df['datetime'] >= datetime.strptime(f'{date} 09:00:00', '%Y-%m-%d %H:%M:%S')) & (df['datetime'] < datetime.strptime(f'{date} 11:00:00', '%Y-%m-%d %H:%M:%S')) , 1, np.where((df['datetime'] >= datetime.strptime(f'{date} 15:00:00', '%Y-%m-%d %H:%M:%S')) & (df['timestamp'] < datetime.strptime(f'{date} 16:00:00', '%Y-%m-%d %H:%M:%S')) , 2, 0))
但是,这似乎不起作用。我认为有一种方法可以通过索引时间来做到这一点,但这似乎很棘手,因为这条代码在函数内部,当我索引时,似乎会遇到很多错误。
有更好的方法吗?
I have some data where I would like to subset between different times of day for different dates, specifically I want the column df['event']
to have a 1
if the time is between 9am to 11am, a 2
if it is between 3pm to 4pm, and 0
otherwise. As such, I tried to use the following line:
df['event'] = np.where( (df['datetime'] >= datetime.strptime(f'{date} 09:00:00', '%Y-%m-%d %H:%M:%S')) & (df['datetime'] < datetime.strptime(f'{date} 11:00:00', '%Y-%m-%d %H:%M:%S')) , 1, np.where((df['datetime'] >= datetime.strptime(f'{date} 15:00:00', '%Y-%m-%d %H:%M:%S')) & (df['timestamp'] < datetime.strptime(f'{date} 16:00:00', '%Y-%m-%d %H:%M:%S')) , 2, 0))
This however, doesn't seem to work. I figured that there's a way to do it by indexing on times, but this seems to be quite tricky since this line of code is inside a function, and when I index on times, it seems to chuck up a bunch of errors.
Is there a better way of doing this?
发布评论
评论(2)
有3个值可供选择,因此请使用
np.Select
There are 3 values to choose from, so use
np.select
instead如果需要的话,这里仅与此处合作是解决 dateTimeIndex.indexer_between_time :
或with:
如果不需要
dateTimeIndex
最终将其分配给助手df1
dataframe:If need working only with times here is solution with
DatetimeIndex.indexer_between_time
:Or with:
If dont need
DatetimeIndex
finally assign it to helperdf1
DataFrame: