Convert string of dd-MON-yy to date in Python - python

I have a string in dd-MON-yy format. While converting to date in python, its is causing issue since the year is in tow digits.
datetime.datetime.strptime('17-JUN-03', '%d-%m-%y')
The error is,
ValueError: time data '17-JUN-03' does not match format '%d-%m-%y'

Try this:
import datetime
print(datetime.datetime.strptime('17-JUN-03', '%d-%b-%y'))
Result:
2003-06-17 00:00:00
Datetime format codes

Related

Pandas.to_datetime doesn't recognize the format of the string to be converted to datetime

I am trying to convert some data from a .txt file to a dataframe to use it for some analysis
the form of the data in the .txt is a follows
DATE_TIME VELOC MEASURE
[m/s] [l/h]
A 09.01.2023 12:45:20 ??? ???
A 09.01.2023 12:46:20 0,048 52,67
A 09.01.2023 12:47:20 0,049 53,77
A 09.01.2023 12:48:20 0,050 54,86
I load the data to a dataframe no problem i covnert the str values of the measurement to float etc everything is good as shows in the
image
the problem I get is when trying to convert the column of the date time that is string to datetime pandas format using this line of code:
volume_flow['DATE_TIME'] = pd.to_datetime(volume_flow['DATE_TIME'], format = '%d.%m.%Y %H:%M:S')
and i get the following error
ValueError: time data '09.01.2023 12:46:20' does not match format '%d.%m.%Y %H:%M:S' (match)
but i don't see how the format is off
I am really lost as to why this is caused as i used the same code with different formats of datetime before with no problem
further more i tried using format = '%dd.%mm.%yyyy %H:%M:S' as well with the same results and when i let the pandas.to_datetime convert it automatically it confuses the day and the month of the data. the data is between 09.01-12.01 so you can't really tell if one is the month or day just by the values.
I think you should go from this
(..., format='%d.%m.%Y %H:%M:S')
to this
(..., format='%d.%m.%Y %H:%M:%S')
You forgot the percentage character!
check the documentations for correct time format. You will note that the directive %S represents the seconds.
Second as a decimal number [00,61].

Convert ISO 8061 datetime to timestamp in python

I have a string with a date in the format: 2021-03-12T14:45:34.000Z
I would like to convert it to a standard format as this one: 12-Mar-2021 14:45:34
I tried using:
print(datetime.datetime.strptime("2021-03-12T14:45:34.000Z", "%Y-%m-%dT%H:%M:%S%fZ"))
but I get the error:
ValueError: time data '2021-03-12T14:45:34.000Z' does not match format
'%Y-%m-%dT%H:%M:%S%fZ'
How can I solve it?
You need to get as datetime then convert to forrmat as you like:
import datetime
date = '2021-03-12T14:45:34.000Z'
datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime('%d-%b-%Y %H:%M:%S')
Output:
'12-Mar-2021 14:45:34'
You are missing a . in your format string. The correct format string is
"%Y-%m-%dT%H:%M:%S.%fZ"
Notice the . after %S and before %fZ.

Convert Dates pandas datetime

I'm trying to convert dates in the format '31-Aug-91' into '31-08-1991' using pandas datetime.
I've tried pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%m-%y').dt.date
But, I get the error ValueError: time data '31-Aug-91' does not match format '%d-%m-%y' (match)
How do I fix this?
Use %b for match first 3 letters of month names:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.date
If need format DD-MM-YYYY:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.strftime('%d-%m-%Y')

ValueError: time data does not match format (convert part of string to time)

I have data that is formatted like DDHHMM (day hour minutes), for example 120630. So the 12th at 06:30. I want to extract only the hour and minutes and convert it to a time object. Is this possible. I get the following error.
time = datetime.strptime(column[3], '%H:%M') #data is from CSV
ValueError: time data '120630' does not match format '%H:%M'
You first need to parse the datetime string in the format it currently is using strptime, and then convert the datetime object to the format you want using strftime:
datetime.strptime('120630', '%d%H%M').strftime('%H:%M')
# '06:30'

python parse string in date format to get the date

There is a string and a date format. I want to get the date based on format.
If date format is YYYY.MM.dd and string is 2017.01.01. It should transform to a valid date object.
How can I find the date.
You can use datetime module something like this :
from datetime import datetime
date_object = datetime.strptime('2017.01.01', '%Y.%m.%d') # Converting the given date string into a datetime object.
formatted_date = date_object.strftime('%c') #User Defined Output Format
print(formatted_date)
This will result in :
Sun Jan 1 00:00:00 2017
You can refer to the documentation here.

Categories