I have a pandas dataframe which reads from a csv file and has a timestamp
column. I am trying to get the rows that were in the last 3 hours. So far I have:
df_trade = pd.read_csv("log.csv")
df_ma.set_index('timestamp')
start = format_time()
end = start - pd.Timedelta(hours=3)
df_ma = df_ma[end:]
This is my helper function:
def format_time():
t = datetime.now()
s = t.strftime('%Y-%m-%d %H:%M:%S')
return pd.to_datetime(s)
However, I get this error when I try to do slicing:
TypeError: cannot do slice indexing on RangeIndex with these indexers [2021-06-01 07:07:53] of type Timestamp
How can I resolve this issue
You have to change your last line:
df_ma = df_ma[end:]
become
df_ma = df_ma.loc[df_ma.index > 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.