i have a data set with a columns name JobDate and need to change of data type from object to datetime
VIN JobDate JobType CategoryName
0 MHFGB8GS2K0896470 03/10/2023 13:03 GR Service GR
1 MHKA4DB3JGJ054415 03/10/2023 10:46 GR Service GR
2 MHKM5EA2JLK080462 03/10/2023 10:58 SBE Service GR
3 MHFJW8EM1G2303370 04/10/2023 09:24 SBE Service GR
4 MR2BF3HK1L4009617 03/10/2023 07:48 SBE Service GR
then i try to do it with a line of code as :
from datetime import datetime as dt
df[‘Date’] = pd.to_datetime(df[‘JobDate’],errors=’coerce’ ,format=’mixed’)
df.dtypes
VIN object
JobDate object
JobType object
CategoryName object
Date datetime64[ns]
dtype: object
df.head()
VIN JobDate JobType CategoryName Date
0 MHFGB8GS2K0896470 03/10/2023 13:03 GR Service GR 2023-03-10 13:03:00
1 MHKA4DB3JGJ054415 03/10/2023 10:46 GR Service GR 2023-03-10 10:46:00
2 MHKM5EA2JLK080462 03/10/2023 10:58 SBE Service GR 2023-03-10 10:58:00
3 MHFJW8EM1G2303370 04/10/2023 09:24 SBE Service GR 2023-04-10 09:24:00
4 MR2BF3HK1L4009617 03/10/2023 07:48 SBE Service GR 2023-03-10 07:48:00
the data type is changed from object to datetime, but i need to change a format date to only year-month-day without a time
enter image description here
please any body have a solution with this case ?
i try to write a line of code with :
df[‘Date’] = pd.to_datetime(df[‘JobDate’]).dt.strftime(format=’%Y-%m-%d’)
but the result is :
ValueError Traceback (most recent call last)
in <cell line: 1>()
—-> 1 df[‘Date’] = pd.to_datetime(df[‘JobDate’]).dt.strftime(format=’%Y-%m-%d’)
4 frames
/usr/local/lib/python3.10/dist-packages/pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data “13/10/2023 10:20” doesn’t match format “%m/%d/%Y %H:%M”, at position 57. You might want to try:
– passing format
if your strings have a consistent format;
– passing format='ISO8601'
if your strings are all ISO8601 but not necessarily in exactly the same format;
– passing format='mixed'
, and the format will be inferred for each element individually. You might want to use dayfirst
alongside this.