Home > database >  Return specific amount of days from list of dates in DataFrame
Return specific amount of days from list of dates in DataFrame

Time:02-02

I am trying to only get the last 7 days of data but still keep it in DataFrame format.

I am receiving some data in the DataFrame format. And i'm trying to use the pandas.Timedelta.days to filter so I only get the last 7 days of data. But when I run the code I get the following error: AttributeError: 'DatetimeProperties' object has no attribute 'Timedelta' This I don't understand since timedelta should be able with datetime import.

How can I make it to only return the last 7 days of my data?

My code looks like the following:

from xxx.input import get_automation_metrics

def graph_visual_data():
   data = get_automation_metrics('podcast')
   data = data[data.name.isin(['podcast_processed', 'n_unknown_errors', 'n_known_errors'])][['date','value','name']]
   data.date = data.date.dt.Timedelta.days(7)
   data.value = data.value.astype(int)

The data I receive looks like the following: enter image description here

CodePudding user response:

That's because you misunderstand how pandas.Timedelta works.

I wrote sample code using pandas.Timedelta. So try this one to understand how to use it.

import datetime
import pandas as pd

# Create a sample dataframe (2022-01-01 ~ 2022-02-01)
df = pd.DataFrame(pd.date_range(start='2022-01-01', end='2022-02-01'), columns=['date'])
df['value'] = 0
df['name'] = 'abc'

# timedelta
td = pd.Timedelta(days=-7)

# current timestamp
now = datetime.datetime.now()

# from 7 days ago till now
condition = (now   td < df.date) & (df.date <= now)

# output dataframe
df_last_7days = df[condition]
  •  Tags:  
  • Related