Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing datetime format which includes weekday?

Currently I have datetime column in this format

        Datime
        Thu Jun 18 23:04:19 +0000 2020
        Thu Jun 18 23:04:18 +0000 2020
        Thu Jun 18 23:04:14 +0000 2020
        Thu Jun 18 23:04:13 +0000 2020

I want to change it to:

   Datetime
 2020-06-18 23:04:19
 2020-06-18 23:04:18
 2020-06-18 23:04:14
 2020-06-18 23:04:13
like image 758
user13174343 Avatar asked Sep 17 '26 10:09

user13174343


1 Answers

Assuming you have loaded your pandas dataframe, you can convert Datetime column to specified format using this function. You can rename this function.

import datetime

def modify_datetime(dtime):
    my_time = datetime.datetime.strptime(dtime, '%a %b %d %H:%M:%S %z %Y')
    return my_time.strftime('%Y-%m-%d %H:%M:%S')

First argument to strptime function is date string and second argument is format.

Directive, Description

%a         Weekday abbreviated
%b         Month abbreviated name
%d         Day of the month
%H         Hour (24-hour format)
%M         Minute with zero padding
%S         Second with zero padding
%z         UTC offset
%Y         Full year

Once you converted string date to datetime objects you can convert it back to string with specified format using strftime function. You can read more about formats here.

Finally, just modify the Datetime column

df['Datetime'] = df['Datetime'].apply(modify_datetime)
like image 175
Sharmiko Avatar answered Sep 18 '26 23:09

Sharmiko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!