I want to create a scatter plot for every 10 minutes in the time period specified by t_list
. I get the error TypeError: cannot compare a dtyped [datetime64[ns]] array with a scalar of type [bool]
in the line df_t = df[(df['datetime']>=t & df['datetime']<t_end)]
but the type for t
and t_end
are both datetime
. non of the variables are type bool
.
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
df_data = pd.read_csv('C:\SCADA.csv')#import data
#format Timestamp as datetime
df_data['datetime'] = pd.to_datetime(df_data['TimeStamp'] )
#create df of time period
df = df_data[(df_data['datetime']>= datetime(2017, 12, 23, 06,00, 00)) &
(df_data['datetime']< datetime(2017, 12, 23, 07, 00, 00))]
#time period I want to create 10 min plots for
t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 07, 00, 00)]
for t in t_list:
t_end = t + timedelta(minutes = 10)
#breaks here with
TypeError: cannot compare a dtyped [datetime64[ns]] array with a
scalar of type [bool]
df_t = df[(df['datetime']>=t & df['datetime']<t_end)]
#code continues with plotting scatter plots within the loop
When boolean indexing with multiple conditions, you should wrap each single condition in brackets.
From the docs:
Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses, since by default Python will evaluate an expression such as df.A > 2 & df.B < 3 as df.A > (2 & df.B) < 3, while the desired evaluation order is (df.A > 2) & (df.B < 3).
Thus, adding the brackets to your last line should work:
df_t = df[(df['datetime']>=t) & (df['datetime']<t_end)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.