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')
Related
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
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.
converting string to datetime in python pandas with multiple date formats in a column.
Dataframe startdate has date values in 'mm-dd-YYYY hh:mm' format and 'mm/dd/YYYY hh:mm' format.
I have used following query
df['START_DATE*'] = pd.to_datetime(df['START_DATE*'], format= '%m-%d-%Y hh:mm')
START_DATE* is the column name .giving following
time data '01-01-2016 21:11' does not match format '%m-%d-%Y hh:mm' (match)
hh:mm does not mean hour:minute. Try:
df['START_DATE*'] = pd.to_datetime(df['START_DATE*'], format= '%m-%d-%Y %H:%M')
I am trying to convert a pandas column to datetime. This is my error message.
ValueError: time data '01-JUN-17 00:00:00' does not match format
'%d-%b-%y %H.%M.%S' (match)
This is my code :
df['dayofservice'] = pd.to_datetime(df['dayofservice'], format = '%d-%b-%y %H.%M.%S')
I have read this documentation to ensure my format is correct : https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
It's still not working for me.
pandas is man/woman enough to parse this without a format field:
In[90]:
pd.to_datetime('01-JUN-17 00:00:00')
Out[90]: Timestamp('2017-06-01 00:00:00')
So this should work:
df['dayofservice'] = pd.to_datetime(df['dayofservice'])
Change . to : in times like:
df['dayofservice'] = pd.to_datetime(df['dayofservice'], format = '%d-%b-%y %H:%M:%S')
I have an column in excel which has dates in the format ''17-12-2015 19:35". How can I extract the first 2 digits as integers and append it to a list? In this case I need to extract 17 and append it to a list. Can it be done using pandas also?
Code thus far:
import pandas as pd
Location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(Location)
time = df['Creation Date'].tolist()
print (time)
You could extract the day of each timestamp like
from datetime import datetime
import pandas as pd
location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(location)
timestamps = df['Creation Date'].tolist()
dates = [datetime.strptime(timestamp, '%d-%m-%Y %H:%M') for timestamp in timestamps]
days = [date.strftime('%d') for date in dates]
print(days)
The '%d-%m-%Y %H:%M'and '%d' bits are format specififers, that describe how your timestamp is formatted. See e.g. here for a complete list of directives.
datetime.strptime parses a string into a datetimeobject using such a specifier. dateswill thus hold a list of datetime instances instead of strings.
datetime.strftime does the opposite: It turns a datetime object into string, again using a format specifier. %d simply instructs strftime to only output the day of a date.