0

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
Anonymous
  • 1
  • 2
  • Use the dt accessor: `pd.to_datetime(CreditDetail_bkp['Cancellation Date (UTC)_1']).dt.strftime('%m/%d%Y')`. What you need to do is #1 convert string to datetime, #2 format datetime to string. – FObersteiner Jan 12 '23 at 14:04
  • I have tried this but the output i see is YYYY/MM/DD format and not MM/DD/YYYY format due to date_time format – Anonymous Jan 12 '23 at 14:07
  • Side note: if some elements fail to convert to datetime, you might add keyword `errors='coerce'` in the call to pd.to_datetime. – FObersteiner Jan 12 '23 at 14:07
  • Approach #4 is missing .dt, if you take a closer look. – FObersteiner Jan 12 '23 at 14:08
  • Yes In Approach 4 it is missing but i have tried explicitly again with this code too , but it isnt working .. Still the same format YYYY/MM/DD and the output to be also string format only and not the datetime format – Anonymous Jan 12 '23 at 14:10
  • 1
    Thank you for the help , iT worked. Yes missed assigning it back to df. – Anonymous Jan 12 '23 at 14:29

0 Answers0