Home > database >  Get last three dates from the most recent date in the dataframe
Get last three dates from the most recent date in the dataframe

Time:09-14

I have a dataframe df:

      Reference Number        Date             Time                  
36566             5221  2022-09-12  16:26:11.359962  
36565             5220  2022-09-12  16:24:30.494203  
34773             5218  2022-09-11  14:44:41.524015  
34781             5218  2022-09-11  14:44:41.524015  
34780             5218  2022-09-11  14:44:41.524015  
...                ...         ...              ...           
94                5001  2022-05-12  12:14:20.100192    
95                5001  2022-05-12  12:14:20.100192    
96                5001  2022-05-12  12:14:20.100192    
97                5001  2022-05-12  12:14:20.100192    
4                 5000  2022-05-12  12:08:11.946828    

How can I retrieve rows with the last three days from the most recent date ?

for example in this scenario I want the objects from 2022-09-12 to 2022-09-10 The object type is <class 'datetime.date'>

I tried print("last day",df['Date'].iloc[-1]-datetime.timedelta(days=3)) but it says

type object 'datetime.datetime' has no attribute 'timedelta'

CodePudding user response:

df = pd.DataFrame({"Reference": [36566, 36565,34773],"Number": [5221, 5220,5218],"date": ["2019-3-18", "2019-10-24", "2019-1-1"]})
df["date"] = pd.to_datetime(df["date"])
df = df.sort_values(by="date")
print(df[-2:])

CodePudding user response:

Use DataFrame.last working with DatetimeIndex:

df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date').sort_index().last('3D')

Your solution should be changed:

df = df[df['Date'] > (df['Date'].max()-pd.Timedelta(days=3))]
  • Related