I have a dataframe with a column ‘Valuation Date’. The data for this comes in the format YYYYMMDD. I need to change this to a normal date string (MM-DD-YYYY). This is what I have been using, but it isn’t working.
It does not want to cycle through each value (only pulls the first value and uses it for each of the 84k rows. It is also iterating, which I want to avoid. Is there an easier way to do this?
There are 0 values in the data, keeping them 0 is necessary. All others need to be changed to the MM-DD-YYYY format.
for val in df['Valuation Date']:
if val == 0:
val == 0
else:
val = str(val)
vy = val[:4]
vm = val[4:6]
vd = val[6:8]
val = date(int(vy),int(vm),int(vd))
df['Valuation Date'] = val
Any help would be greatly appreciated!
End Solution provided by @thomask
def change_format(val):
if val == 0:
val == 0
else:
val = str(val)
date = datetime.strptime(val,"%Y%M%d")
new_date = date.strftime("%Y-%M-%d")
return new_date
df['Valuation Date'] = df['Valuation Date'].apply(change_format)
>Solution :
Use strptime and strftime of datetime.datetime methods:
from datetime import datetime
def change_format(val):
val = str(val)
date = datetime.strptime(val, "%Y%M%d")
new_date = date.strftime("%Y-%M-%d")
return new_date
Example :
change_format("20220622")
>>> "2022-06-22"
Then, you can apply this function to you dataframe series:
df['Valuation Date'].apply(change_format)