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 :
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?
CodePudding user response:
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