Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the min value of a date in Pandas

I have this df

Start_Time  
0   18/08/2022 00:04:44 
1   18/08/2022 00:09:54 
2   18/08/2022 00:13:00 
3   18/08/2022 00:16:09 
4   18/08/2022 00:18:10 

and I’m trying to get the minimum Start_time value, it can be YYYY/MM/DD or DD/MM/YYYY

this is the piece of code I’m using :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df_test['Start_time'] = pd.to_datetime(df_test['Start_time'])

and then trying to get the min value

df_test['MIN_Start_time'] = df_test['Start_time'].dt.strftime('%Y/%m/%d').min() 

that’s the output of my code, but it should give me as MIN_start_time the value 2022/08/18 or 18/08/2022

Start_Time  MIN_Start_time
0   18/08/2022 00:04:44 2022/01/09  
1   18/08/2022 00:09:54 2022/01/09  
2   18/08/2022 00:13:00 2022/01/09  
3   18/08/2022 00:16:09 2022/01/09  
4   18/08/2022 00:18:10 2022/01/09  

Any ides what I’m doing wrong?

>Solution :

df['Start_Time']=pd.to_datetime(df['Start_Time'], dayfirst=True)

df['min_start_date']=df['Start_Time'].min()
df
Start_Time  min_start_date
0   2022-08-18 00:04:44     2022-08-18 00:04:44
1   2022-08-18 00:09:54     2022-08-18 00:04:44
2   2022-08-18 00:13:00     2022-08-18 00:04:44
3   2022-08-18 00:16:09     2022-08-18 00:04:44
4   2022-08-18 00:18:10     2022-08-18 00:04:44

to get just the date part

df['min_start_date']=df['Start_Time'].dt.strftime('%F').min()
df

or

df['min_start_date']=df['Start_Time'].dt.date.min()
df
    Start_Time  min_start_date
0   2022-08-18 00:04:44     2022-08-18
1   2022-08-18 00:09:54     2022-08-18
2   2022-08-18 00:13:00     2022-08-18
3   2022-08-18 00:16:09     2022-08-18
4   2022-08-18 00:18:10     2022-08-18
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading