I have a list of strings date. Formatted in like
Fri Apr 23 12:38:07 +0000 2021
How can I change its format? I want to take only the hours. I checked other source before, but you need to change the date format, which obviously I'm struggling rn
As I know, you can write the code like
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%A %b %d %H:%M:%S %z %Y')
to change its format. But idk what +0000 means.
If you only want to take the hours from the date strings, you can use .dt.strftime() after the pd.to_datetime() call, as follows:
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
Note that your format string for pd.to_datetime() is not correct and need to replace %A by %a.
+0000 is the time zone, which you can parse with %z in the format string.
Demo
ds = pd.DataFrame({'tanggal': ['Fri Apr 23 12:38:07 +0000 2021', 'Thu Apr 22 11:28:17 +0000 2021']})
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
print(ds)
tanggal waktu
0 Fri Apr 23 12:38:07 +0000 2021 12:38:07
1 Thu Apr 22 11:28:17 +0000 2021 11:28:17
Related
I am trying to convert this format or any timezone format to YYYY-MM-DD HH:MM:SS.
format needed to be converted is - Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time).
can anyone help ?
from datetime import datetime
date_string = 'Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time)'.replace('(India Standard Time)', '').rstrip()
datetime_object = datetime.strptime(date_string, '%a %b %d %Y %H:%M:%S %Z%z').strftime("%Y-%m-%d %H:%M:%S")
print(datetime_object)
Gives output
2021-09-07 08:34:00
Hour is set to 24 hour clock
If you want 12 hour clock replace %H with %I in the above code
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
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)
You can use pandas.to_datetime and pandas.Series.dt.strftime appropriately:
>>> import pandas as pd
>>> from datetime import datetime
>>> datetime_strs = ["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"]
>>> d = {'Datetimes': datetime_strs}
>>> df = pd.DataFrame(data=d)
>>> df
Datetimes
0 Thu Jun 18 23:04:19 +0000 2020
1 Thu Jun 18 23:04:18 +0000 2020
2 Thu Jun 18 23:04:14 +0000 2020
3 Thu Jun 18 23:04:13 +0000 2020
>>> df['Datetimes'] = pd.to_datetime(df['Datetimes'], format='%a %b %d %H:%M:%S %z %Y')
>>> df
Datetimes
0 2020-06-18 23:04:19+00:00
1 2020-06-18 23:04:18+00:00
2 2020-06-18 23:04:14+00:00
3 2020-06-18 23:04:13+00:00
>>> df['Datetimes'] = df['Datetimes'].dt.strftime('%Y-%m-%d %H:%M:%S')
>>> df
Datetimes
0 2020-06-18 23:04:19
1 2020-06-18 23:04:18
2 2020-06-18 23:04:14
3 2020-06-18 23:04:13
Trying to parse a date like, "Thu, 22 Oct 2020 17:33:00 UT" with datetime.strptime(). I've tried:
datetime.strptime('Thu, 22 Oct 2020 17:33:00 UT', '%a, %d %b %Y %H:%M:%S %Z')
However, it appears 'UT' is not a recognized timezone. Is this possible without any string manipulation? Any help is much appreciated.
you could use dateutil's parser and supply a tzinfos mapping dict:
import dateutil
timezone_infos = {"UT": dateutil.tz.UTC}
dt = dateutil.parser.parse('Thu, 22 Oct 2020 17:33:00 UT', tzinfos=timezone_infos)
print(dt)
>>> 2020-10-22 17:33:00+00:00
I got a date in format like this: Wed, 15 Jul 2020 00:00:00 GMT i need to make it to %Y-%m-%d something like 2020-07-15
So, in test3 variable data is Wed, 15 Jul 2020 00:00:00 GMT
I try something like this
date_time = test3.strftime("%Y-%m-%d", test3)
writing `AttributeError: 'str' object has no attribute 'strftime'
I have imported
from datetime import datetime
So any ideas on how to convert it?
from datetime import datetime
test3 = "Wed, 15 Jul 2020 00:00:00 GMT"
date_time = datetime.strptime(test3, "%a, %d %b %Y %H:%M:%S %Z").strftime("%Y-%m-%d")
print(date_time)
use the code:
from datetime import datetime
time_str='Wed, 15 Jul 2020 00:00:00'
dt=datetime.strptime(time_str,'%a, %d %b %Y %H:%M:%S')
print(dt.strftime('%Y-%m-%d'))
I have a function in my backend and I receive the date from the front end in the formart Wed Dec 14 2016 14:39:16 GMT+0300 (AST)
date = request.body['date']
d = datetime.strptime(date, '%a %b %d %Y %X %Z%z')
I know strptime converts to the datetime object but I keep getting the following
'z' is a bad directive in format '%a %b %d %Y %X %Z%z'
What should my string format be?
You can do like this:
from dateutil import parser
parser.parse("Wed Dec 14 2016 14:39:16 GMT+0300 (AST)")