Get the last x minutes from a dataframe with a timestamp column

Hi everyone,
This is something that I have had the need to include in a dashboard and I thought I would share the method I used to extract the last x minutes from a dataframe. Maybe it can be useful to others.

For a dataframe called today and the timestamp column called dateTime.sec

# Get the last timestamp from the dataframe 
last_timestamp = df_today['dateTime.sec'].iloc[-1]

# Use last_timestamp to go back in time in the dataframe
# In this case we are going back 30 minutes 
first_timestamp = last_timestamp - pd.Timedelta(30, 'minutes')
# Create new dataframe with only timestamps bigger or equal to the first_timestamp
# In this case the dataframe will have all records from 30 minutes ago 
df_today_filtered = df_today[df_today["dateTime.sec"] >= first_timestamp]

How can you upgrade on this?

By creating an input field or a dropdown menu where the user can define the time interval. In that case just use:

pd.Timedelta(user_input, 'minutes') in the callback of your app.

Hope this can help someone!