简体   繁体   English

熊猫ween_time布尔值

[英]Pandas between_time boolean

I'm trying to create a column that will assign true if row value falls between the times 09:00 and 17:00. 我正在尝试创建一个列,如果行值介于时间09:00和17:00之间,它将分配true。

I can select those times easily enough using between_time but can't assign a new column a True , False . 我可以使用between_time轻松地选择那些时间,但是不能为新列分配TrueFalse

df = df.between_time('9:00', '17:00', include_start=True, include_end=True)

This statement selects the appropriate rows, but I can't assign a True-False value to them. 该语句选择适当的行,但是我无法为它们分配True-False值。

I've been trying to use np.where . 我一直在尝试使用np.where

df['day'] = np.where(df.between_time('9:00', '17:00', include_start=True, include_end=True),'True','False')

Except that np.where only works on boolean expressions. 除了np.where仅适用于布尔表达式。

I've used it like this: 我这样使用它:

df['day'] = np.where((df['dayTime'] < '09:00') & (df['dayTime'] > '17:00'),'True','False')

But only gotten False returns , because a value can't be greater than 17 and less than 9 at the same time. 但是只有False returns ,因为一个值不能同时大于17和小于9。

I'm missing a relatively easy step and can't figure it out. 我缺少一个相对容易的步骤,无法解决。 Help. 救命。

You could do something like the following: 您可以执行以下操作:

df.index.isin(df.between_time('9:00', '17:00', include_start=True, include_end=True).index)

Because the between method returns a dataframe, rather than a boolean array we can call .index on it as we could with any dataframe, this will return a series of times. 因为between方法返回的是数据.index ,而不是布尔数组,所以我们可以像对待任何数据.index对它调用.index ,这将返回一系列时间。 We can then check wether or not our original time indices are in our time indices resulting from the between method. 然后,我们可以检查原始时间索引是否在between方法产生的时间索引中。

This will give you a boolean array of values that are within your time frame. 这将为您提供时间范围内的布尔值数组。 I can't think of another way to do this, but that doesn't mean there isn't one 我想不出另一种方式来做到这一点,但这并不意味着没有一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM