I am trying to convert a date column which is object data type which is in YYYY/MMDD & YYYY/MM/DD Timeestamp format to customized format i.e. MM/DD/YYYY
Dataset CreditDetail_bkp: has these below columns with below data types.
Code:
Printed the unique types available for each column for the conversion
print('SO Start Date',CreditDetail['SO Start Date'].unique())
print('##########################################')
print('SO End Date',CreditDetail['SO End Date'].unique())
print('##########################################')
print('Cancellation Date (UTC)',CreditDetail['Cancellation Date (UTC)'].unique())
print('##########################################')
print('Registration Date (UTC)',CreditDetail['Registration Date (UTC)'].unique())
print('##########################################')
print('Country Hire Date',CreditDetail['Country Hire Date'].unique())
O/P :
SO Start Date [nan '2022/08/23 17:30:00' '2022/08/25 15:02:00' ... '2022/09/13 08:43:00'
'2022/09/14 19:00:00' '2022/08/17 18:00:00']
##########################################
SO End Date [nan '2022/08/23 18:30:00' '2022/08/25 16:46:00' ... '2022/09/14 20:00:00'
'2022/09/30 15:00:00' '2022/08/17 19:30:00']
##########################################
Cancellation Date (UTC) [nan '2022/10/20' '2022/08/02']
##########################################
Registration Date (UTC) [nan '2023/01/03' '2022/08/31' '2022/11/04' '2022/11/23' '2022/11/21'
'2022/09/18' '2022/08/04' '2022/09/16' '2022/08/16' '2022/12/07'
##########################################
Country Hire Date ['2022/08/17' '2022/09/05' '2022/08/22' ... '1993/09/13' '2018/06/30'
'2022/05/21']
Code:
Tried multiple approaches , but getting different errors due to object data type and format:
Approach1:
CreditDetail_bkp['Cancellation Date (UTC)_1'] = CreditDetail_bkp['Cancellation Date
(UTC)'].strftime("%m/%d/%Y")
Approach2:
CreditDetail_bkp['Cancellation Date (UTC)'].apply(lambda x: x.strftime('%m%d%Y'))
Approach3:
CreditDetail_bkp['SO Start Date1'] = CreditDetail_bkp['SO Start Date'].dt.strftime('%m%d%Y')
Approach4:
pd.to_datetime(CreditDetail_bkp['Cancellation Date (UTC)_1']).strftime('%m/%d%Y')
All the above approaches are throwing errors and not giving the expected format which is
MM/DD/YYYY