May-22-2020, 03:08 PM
(This post was last modified: May-22-2020, 03:09 PM by donnertrud.)
Hi guys,
I got a data set with Dates looking like this :
Jan. 01
Feb. 01
Mar. 01
and so on. It seems Python can't transform this kind of date format using
Jan. 2001
Feb. 2001
Mar. 2001
What I have been trying, and I am 100% sure there is a fancier way, is to replace all "01" with "2001" and so on. The code looks like that :
Is there a way to fix my approach, or a different approach that solves my problem?
I got a data set with Dates looking like this :
Jan. 01
Feb. 01
Mar. 01
and so on. It seems Python can't transform this kind of date format using
df['Date'] = pd.to_datetime(df['Date'])In order to work I guess the format has to be:
Jan. 2001
Feb. 2001
Mar. 2001
What I have been trying, and I am 100% sure there is a fancier way, is to replace all "01" with "2001" and so on. The code looks like that :
for i in range(len(df["Date"])): df["Date"][i] = df["Date"][i].replace("00","2000") df["Date"][i] = df["Date"][i].replace("01","2001") df["Date"][i] = df["Date"][i].replace("02","2002") df["Date"][i] = df["Date"][i].replace("03","2003")The problem what that code is, that transformed strings, e.g. 2020, are getting replaced again, because they have the string "02" in them. As a result, I get dates like : Jan 202000

Is there a way to fix my approach, or a different approach that solves my problem?