Given a dataframe, how to groupby Item with max value of Updated Date (as datetime instead of date string) while keeping date string format in result dataframe ?
df = pd.DataFrame([['A', 10, 'Jun 12, 2019 06:16 PM'],
['A', 20, 'Jul 26, 2019 10:56 AM'],
['B', 30, 'May 20, 2019 05:54 PM'],
['B', 40, 'Apr 28, 2019 06:42 PM']],
columns=['Item', 'Quantity', 'Updated Date'])
>>> df
Item Quantity Updated Date
0 A 10 Jun 12, 2019 06:16 PM
1 A 20 Jul 26, 2019 10:56 AM
2 B 30 May 20, 2019 05:54 PM
3 B 40 Apr 28, 2019 06:42 PM
Expected Output
Item Quantity Updated Date
0 A 30 Jul 26, 2019 10:56 AM
1 B 70 May 20, 2019 05:54 PM
What I have tried
If I put 'Updated Date': max in the agg(), it would simply return the max value in alphabetical order
>>> df.groupby(['Item'], as_index=False).agg({'Quantity': sum, 'Updated Date': max})
Item Quantity Updated Date
0 A 30 Jun 12, 2019 06:16 PM # expected to be Jul 26
1 B 70 May 20, 2019 05:54 PM
If I apply pd.to_datetime() it gives me a closer result but the date string format is distorted
df['Updated Date'] = pd.to_datetime(df['Updated Date'])
df.groupby(['Item'], as_index=False).agg({'Quantity': sum, 'Updated Date': max})
Item Quantity Updated Date
0 A 30 2019-07-26 10:56:00
1 B 70 2019-05-20 17:54:00
Is it possible to apply pd.to_datetime() only during groupby ? The challenge here is datetime format is not guaranteed to be '%b %d, %Y %I:%M %p' while I want to keep the date string as is in the result.