0

This is a 2 part question. I would like to know more about is pd.(NaT) and why did it give me an error that did not exists. This was the solution to my problem but I do not understand the error I got and why the solution works.

First, I would like to say this error has come up before and I have solved it by going to one of these 3 questions asked in the links below. Resetting the index is a normal solution here.

None of these worked and I was able to solve my problem but there is no online solution for it or an explanation on why it works.

My problem:

I needed to convert an object to a datetime for 4 series in my pandas data frame. date1 to date4 is what I'll call them for this question. Date1 converted no problem, but the other 3 in the same data frame I would get that error in the subject title. After two hours of research and finding nothing to solve my problem. I remembered I had come across the pd.NaT before so solve a past problem to fill in missing values.

To solved it I did the following:

df_both["Date2"] = df_both["Date2"].fillna(pd.NaT)
df_both["Date3"] = df_both["Date3"].fillna(pd.NaT)
df_both["Date4"] = df_both["Date4"].fillna(pd.NaT)

then a normal Converting

df_both["Date2"] = pd.to_datetime(df_both["Date2"])
df_both["Date3"] = pd.to_datetime(df_both["Date3"])
df_both["Date4"] = pd.to_datetime(df_both["Date4"])

Can someone please explain to me how this works? Also, My index was unique when i tested it with df_both.index.is_unique = True. So why did I get that specific error?

Side note Date1 had missing values as well and did not need the pd.NaT to make it work.

JQTs
  • 62
  • 1
  • 8

0 Answers0