Reputation: 4943
I have column in pandas dataframe as follows:
0 2018-04-06
1 2018-04-06
2 2018-04-09
3 2018-04-19
4 2018-04-19
5 2018-04-17
I want to convert this column into yyyy/mm/dd for which I have coaded as follows:
def change_date_format(x):
if x != 'nan' and x != '' and x != ' ' and x != 0:
x = parse(x, dayfirst=True).strftime("%Y-%m-%d")
return x
else:
return ''
read4['Column Name'] = read4['Column Name'].apply(lambda x : change_date_format(x) )
But it's convert as follows:
2018-06-04
2018-06-04
2018-09-04
2018-04-19
2018-04-19
2018-04-17
which ideally should be :
2018-04-06
2018-04-06
2018-04-09
2018-04-19
2018-04-19
2018-04-17
How do I force it do work as above. Basically it should consider the input also and depends on that it should work.
Upvotes: 2
Views: 357
Reputation: 863741
I think need to_datetime
with parameter errors='coerce'
for convert not parseable values to NaT
, then strftime
and last replace
:
read4['Column Name'] = (pd.to_datetime(read4['Column Name'], errors='coerce')
.dt.strftime("%Y-%m-%d")
.replace('NaT', ''))
Column Name
0 2018-04-06
1 2018-04-06
2 2018-04-09
3 2018-04-19
4 2018-04-19
5 2018-04-17
Upvotes: 2
Reputation: 1176
Maybe you need to set your dayfirst
flag to False
?
x = parse(x, dayfirst=False).strftime("%Y-%m-%d")
Upvotes: 1