I have the following table in pandas:
>>>TIMESTAMP date Hour patient1 patient2 patient3 patient4 ....
0 2019-06-13 12:00:00 2019-06-13 12:00:00 -0.456 -0.124 -0.451 -2.257
1 2019-06-13 12:03:00 2019-06-13 12:03:00 -0.456 -0.134 -0.781 -2.357
2 2019-06-13 12:06:00 2019-06-13 12:06:00 -0.876 -0.238 -0.983 -1.824
...
2019-07-04 22:03:00 2019-07-04 12:03:00 -0.568 -0.532 -0.451 0.789
n 2019-07-04 22:06:00 2019-07-04 12:06:00 -0.021 -0.981 -0.293 1.824
I have timestamp column which is datetime and I have the column date and hour which I split from the TIMESTAMP column as following:
# #two cOlumns for hour and for date
df['Date'] = [d.date() for d in df['Timestamp']]
df['Hour'] = [d.time() for d in df['Timestamp']]
The problem is that I want to select from my database only rows that are between 2019-06-20 to 2019-07-02 and that the hour will be between 07:00 to 17:00.That doesn't work due to problems with the datetime format.
I was trying to do it using between:
df=df[(df['date'].between('2019-06-20','2019-07-02'))&(df['Hour'].between('07:00','17:00'))]
but then I have gotten this error:
TypeError: '>=' not supported between instances of 'datetime.date' and 'str'
so I checked the dtypes and found that date and hour are object type, so I tried to change their type like this:
df['date'] = pd.to_datetime(df['date']).dt.date
df['Hour'] = pd.to_datetime(df['Hour']).dt.time
but got error:
TypeError: <class 'datetime.time'> is not convertible to datetime
My end goal is to be able to select only rows that are inside the date and hour ranges that I want (between 2019-06-20 to 2019-07-02 and that the hour will be between 07:00 to 17:00).