I have a date set for a couple of years and I am trying to extract data from a specific range and time, but I cannot get it to work with Pandas. I am sure there is something wrong with the date & time format, but oddly I can get hourly averages but cannot extract a date & time range.
This is the code, but it never return any data, I know it there is data in that time period. I think the date & time format is correct, but not sure why it does not work. Any help would be highly appreciated
Code
import pandas as pd
import numpy as np
df = pd.DataFrame([['2018-01-01 16:00:00 00:00', 2], ['2018-01-
01 17:00:00 00:00', 3], ['2018-01-01 18:00:00 00:00', 6]['2018-
01-01 19:00:00 00:00', 9]], columns=['time', 'data'])
print(df.head)
df['time'] = pd.to_datetime(df['time'])
df2018 = df.loc['2018-01-01 16:30:00':'2018-01-01 18:00:00']
print(df2018.head)
CodePudding user response:
Is the time column your index? if not, make it your index:
pd.set_index(df['time'])
Then:
df["2018-10-02":"2018-10-03"] # slicing a date period
df.loc[(df['time'] > start_date) & (df['time'] <= end_date )]