strptime format - Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm currently working with an API that returns date in the following format:
d1 = '2015-06-25T18:45:24'
d2 = '2015-07-11T18:45:35'
So, to find the difference between two times, in days, I'm doing the following:
import datetime
d1 = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f")
d2 = datetime.datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f")
print abs((d2-d1).days)
However, I'm getting the following error:
ValueError: time data '2015-06-25T18:45:24' does not match format '%Y-%m-%dT%H:%M:%S.%f'
I know I could just simply split the string in 'T' and then convert, however is there an easy way to convert to such format to datetime object?

The %f does not apply in this case, and you need that T in your format:
dt_format = "%Y-%m-%dT%H:%M:%S"
d1 = datetime.datetime.strptime(d1, dt_format)

Related

Parsing datetime value using strptime() problem [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I try to convert data like
'2021-07-06T07:31:02Z'
to a datetime using this way
from datetime import datetime
datetime.strptime(created, '%Y-%m-%dT%H:%M:%S.%fZ')
but got the error
time data '2021-07-06T07:31:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
What am I doing wrong?
You are using %f in your code, but your aren't giving a fraction in your string example.
I tried the following which worked:
datetime.strptime('2021-07-06T07:31:02Z','%Y-%m-%dT%H:%M:%SZ')
Out[14]: datetime.datetime(2021, 7, 6, 7, 31, 2)
So either remove the .%f or add the fraction value to your string.
For more info:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

How can I convert a str type to date type using python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to convert a str type ( like :"1995-01-09 00:00:00") to date, here is my attempt
for line in ratings_df[['user_id','movie_id','rating','timestamp']].values:
annee= line[3]
print datetime.strptime(annee, "%Y-%m-%d %H:%M:%S")
and I got this error:
If your example date string is accurately copied to your question there is a trailing space character at the end. The format string does not have this space, so strptime() is not able to parse the string. You can strip() the date string beforehand:
for line in ratings_df[['user_id','movie_id','rating','timestamp']].values:
annee= line[3].strip()
print datetime.strptime(annee, "%Y-%m-%d %H:%M:%S")
Or you could add the space to the end of the format string. I think stripping is better.
>>> annee
'1995-01-09 00:00:00 '
>>> datetime.datetime.strptime(annee, "%Y-%m-%d %H:%M:%S ")
datetime.datetime(1995, 1, 9, 0, 0)
No issues found other than extra space at the end, strip timestamp string and try again
Try printing value of the variable annee first.
This might solve the problem.

Date time index sets wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a Dataframe called file, but the column 'Time valid (UTC)' has its data values in the following way: '25-10-2017 03:30' which does not conform with datetime index. Now I tried to correct this date by using
file['Time valid (UTC)']=pd.to_datatime(file['Time valid (UTC)'],format='%d-%m-%Y %H:%M')
However, the following errors pops up
file['Time valid (UTC)']=pd.to_datatime(file['Time valid (UTC)'],format='%d-%m-%Y %H:%M') AttributeError: 'module' object has no attribute 'to_datatime'
Any idead about how to fix this or how to set the datetime correctly at once with pd.DatetimeIndex?
You have to correct the function name to pd.to_datetime()

'-' is a bad directive in format '%Y-%-m-%-d' - python/django [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Trying to convert str to date ,e.g '2017-8-27'
my code:
from datetime import datetime as dt
import datetime, collections
date = dt.strptime('2017-8-27', '%Y-%-m-%-d')
But I see the following message:
'-' is a bad directive in format '%Y-%-m-%-d'
And don't understand what is incorrect. How can I solve it?
Thanks in advance,
The % should be followed by a format specifier.
The correct format string is '%Y-%m-%d'

ValueError in string to datetime conversion [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to convert strings (which I have in a list) to datetimes.
I tried this:
import datetime
list = [
'12-October-2014-18:30',
'12-October-2014-19:30',
'12-October-2014-20:00',
'12-October-2014-20:30',
'13-October-2014-00:30',
]
for item in list:
item_time = datetime.datetime.strptime(item, "%m-%B-%Y-%H-%M")
print item_time
but I get this error:
ValueError: time data '12-October-2014-18:30' does not match format '%m-%B-%Y-%H-%M'
I dont see the Error, can somebody help please?
There are two things wrong.
for '%m-%B-%Y-%H-%M', your date should be: '12-October-2014-18-30'
In some you have 12th where it should just be 12 and in all of them you have 18:30 but need it to be 18-30

Categories

Resources