This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 4 years ago.
I want to convert Wed, 14 Mar 2018 07:30:00 GMT to 2018/03/14 in python. I am getting unicode object has no attribute strftime error. Someone help me.
Use:
from datetime import datetime
date_given = 'Wed, 14 Mar 2018 07:30:00 GMT'
datetime.strptime(date_given, '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y/%m/%d')
You can read up below links for more details:
Python documentation for strptime: Python 2, Python 3
Python documentation for strftime: Python 2, Python 3
Related
This question already has answers here:
Parse date and change format from spanish
(2 answers)
Closed 1 year ago.
I want to convert this df from an object to a datetime object, "ABR" is the Spanish for "APR"
Initial date
final date
30 ABR 21
31 MAY 21
30 JUN 21
31 JUL 21
my code:
import locale
locale.setlocale(locale.LC_TIME, '')
a["Fecha Inicial"].apply(lambda x: dt.strptime(x, "%d %b %y"))
ValueError
ValueError: time data '30 ABR 21' does not match format '%d %b %y'
This can solve your issue
import pandas as pd
import datetime as dt
a = pd.DataFrame({"Fecha Inicial": ['30 ABR 21', '30 JUN 21']})
mapping_dict = {'ENERO':'JAN',
'FEB':'FEB',
'MARZO':'MAR',
'ABR':'APR',
'MAYO':'MAY',
'JUN':'JUN',
'JUL':'JUL',
'AGOSTO':'AUG',
'SEPT':'SEP',
'OCT':'OCT',
'NOV':'NOV',
'DIC':'DEC'
}
a["Fecha Inicial"].apply(lambda x: dt.datetime.strptime(x.replace(x.split(' ')[1],mapping_dict[x.split(' ')[1]].capitalize()), "%d %b %y"))
locale has some issue that affects the dt.datetime.strptime.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have strings in following format:
Friday January 3 2020 16:40:57
Thursday January 2 2020 19:26:19
Sunday January 5 2020 01:24:55
Tuesday December 31 2019 17:31:42
What is the best way to convert them into python date and time?
You can use datetime.strptime:
from datetime import datetime
d = "Friday January 3 2020 16:40:57"
datetime_object = datetime.strptime(d, '%A %B %d %Y %H:%M:%S')
print(datetime_object)
You can use dateparser
Install:
$ pip install dateparser
Sample Code:
import dateparser
t1 = 'Friday January 3 2020 16:40:57'
t2 = 'Thursday January 2 2020 19:26:19'
t3 = 'Sunday January 5 2020 01:24:55'
t4 = 'Tuesday December 31 2019 17:31:42'
dt1 = dateparser.parse(t1)
dt2 = dateparser.parse(t2)
dt3 = dateparser.parse(t3)
dt4 = dateparser.parse(t4)
for dt in [dt1, dt2, dt3, dt4]:
print(dt)
Output:
2020-01-03 16:40:57
2020-01-02 19:26:19
2020-01-05 01:24:55
2019-12-31 17:31:42
This question already has answers here:
How to convert integer timestamp into a datetime
(3 answers)
Closed 5 years ago.
How do I convert an int like 1485714600 such that my result ends up being Monday, January 30, 2017 12:00:00 AM?
I've tried using datetime.datetime but it gives me results like '5 days, 13:23:07'
Like this?
>>> from datetime import datetime
>>> datetime.fromtimestamp(1485714600).strftime("%A, %B %d, %Y %I:%M:%S")
'Sunday, January 29, 2017 08:30:00'
What you describe here is a (Unix) timestamp (the number of seconds since January 1st, 1970). You can use:
datetime.datetime.fromtimestamp(1485714600)
This will generate:
>>> import datetime
>>> datetime.datetime.fromtimestamp(1485714600)
datetime.datetime(2017, 1, 29, 19, 30)
You can get the name of the day by using .strftime('%A'):
>>> datetime.datetime.fromtimestamp(1485714600).strftime('%A')
'Sunday'
Or you can call weekday() to obtain an integers between 0 and 6 (both inclusive) that maps thus from monday to sunday:
>>> datetime.datetime.fromtimestamp(1485714600).weekday()
6
This question already has answers here:
Python date string to date object
(9 answers)
Closed 5 years ago.
I'm a Python newbie and don't how to convert a Python 3.5x string
'2017-04-19 00:23'
into a date and time like
April 19, 2017 12:23 am
and even get individual units like
April
19
2017
12:23 am
or get day of week for 4/19/217
Wednesday
Use python datetime module, something like this :
from datetime import datetime
date_str = '2017-04-19 00:23'
date_obj = datetime.strptime(date_str, '%Y-%m-%d %H:%M')
# To get a particular part of the date in a particular format such as "Wednesday" for the "Datetime Object"
print(date_obj.strftime('%A'))
print(date_obj.strftime('%c'))
This will result in :
Wednesday
Wed Apr 19 00:23:00 2017
Check out the documentation.
I am using Pandas to read and process csv file. My csv file have date/time column that looks like:
11:59:50:322 02 10 2015 -0400 EDT
11:11:55:051 16 10 2015 -0400 EDT
00:38:37:106 02 11 2015 -0500 EST
04:15:51:600 14 11 2015 -0500 EST
04:15:51:600 14 11 2015 -0500 EST
13:43:28:540 28 11 2015 -0500 EST
09:24:12:723 14 12 2015 -0500 EST
13:28:12:346 28 12 2015 -0500 EST
How can I read this using python/pandas, so far what I have is this:
pd.to_datetime(pd.Series(df['senseStartTime']),format='%H:%M:%S:%f %d %m %Y %z %Z')
But this is not working, though previously I was able to use the same code for another format (with a different format specifier). Any suggestions?
The issue you're having is likely because versions of Python before 3.2 (I think?) had a lot of trouble with time zones, so your format string might be screwing up on the %z and %Z parts. For example, in Python 2.7:
In [187]: import datetime
In [188]: datetime.datetime.strptime('11:59:50:322 02 10 2015 -0400 EDT', '%H:%M:%S:%f %d %m %Y %z %Z')
ValueError: 'z' is a bad directive in format '%H:%M:%S:%f %d %m %Y %z %Z'
You're using pd.to_datetime instead of datetime.datetime.strptime but the underlying issues are the same, you can refer to this thread for help. What I would suggest is instead of using pd.to_datetime, do something like
In [191]: import dateutil
In [192]: dateutil.parser.parse('11:59:50.322 02 10 2015 -0400')
Out[192]: datetime.datetime(2015, 2, 10, 11, 59, 50, 322000, tzinfo=tzoffset(None, -14400))
It should be pretty simple to chop off the timezone at the end (which is redundant since you have the offset), and change the ":" to "." between the seconds and microseconds.
Since datetime.timezone has become available in Python 3.2, you can use %z with .strptime() (see docs). Starting with:
dateparse = lambda x: pd.datetime.strptime(x, '%H:%M:%S:%f %d %m %Y %z %Z')
df = pd.read_csv(path, parse_dates=['time_col'], date_parser=dateparse)
to get:
time_col
0 2015-10-02 11:59:50.322000-04:00
1 2015-10-16 11:11:55.051000-04:00
2 2015-11-02 00:38:37.106000-05:00
3 2015-11-14 04:15:51.600000-05:00
4 2015-11-14 04:15:51.600000-05:00
5 2015-11-28 13:43:28.540000-05:00
6 2015-12-14 09:24:12.723000-05:00
7 2015-12-28 13:28:12.346000-05:00