Pandas: to.datetime() quesiton - python

I have Date/Time in the following format:
10/01/21 04:49:43.75
MM/DD/YY HH/MM/SS.ms
I am trying to convert this from being an object to a datetime. I tried the following code but i am getting an error that it does not match the format. Any ideas?
df['Date/Time'] = pd.to_datetime(df['Date/Time'], format = '%m%d%y %H%M%S%f')

you can try letting pandas infer the datetime format with:
pd.to_datetime(df['Date/Time'], infer_datetime_format=True)

Related

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.

How can I convert following string date-time into pandas datetime

How can I convert the following string format of datetime into datetime object to be used in pandas Dataframe? I tried many examples, but it seems my format is different from the standard Pandas datetime object. I know this could be a repetition, but I tried solutions on the Stackexchange, but they don't work!
Below code will convert it into appropriate format
df = pd.DataFrame({'datetime':['2013-11-1_00:00','2013-11-1_00:10','2013-11-1_00:20']})
df['datetime_changed'] = pd.to_datetime(df['datetime'].str.replace('_','T'))
df.head()
output:
You can use pd.to_datetime with format
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d_%H:%M')

Pandas Datetime Format

I'm trying to convert 01/02/20(Thu)05:05:17 into DD-MM-YYYY format, but struggling through pandas.
df['_source.now'] = pd.to_datetime(df['_source.now'])
I get this error: ParserError: Unknown string format: 01/02/20(Thu)05:05:17
You can try this
# day first, use '%d/%m/%y(%a)%H:%M:%S'
# month first, use '%m/%d/%y(%a)%H:%M:%S'
pd.to_datetime(df['_source.now'],format='%m/%d/%y(%a)%H:%M:%S',errors='coerce').dt.strftime('%d-%m-%Y')

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')

Converting pandas Column to datetime

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')

Categories