1

I am new to python. I have been trying to convert datatype of date column from int64 to date in jupyter notebook but whenever I try something I get the format correct but all the rows in the file shows same date. I don't understand what's wrong.

Date 43390 43599 43605 43329 43330 43604 43601

After I use any function- for example: df["DATE2"] = pd.to_datetime(pd.Series(df["DATE"])) df[["DATE","DATE2"]].head()

I get this:

DATE DATE2 0 1970-01-01 00:00:00.000043390 1970-01-01 00:00:00.000043390 1 1970-01-01 00:00:00.000043599 1970-01-01 00:00:00.000043599 2 1970-01-01 00:00:00.000043605 1970-01-01 00:00:00.000043605 3 1970-01-01 00:00:00.000043329 1970-01-01 00:00:00.000043329 4 1970-01-01 00:00:00.000043330 1970-01-01 00:00:00.000043330

  • Hi, welcome to SO! Could you please put your code here and a sample of your dataframe? This will help the others find a solution for you. – Eduardo Coltri Sep 11 '20 at 14:07
  • Hi Eduardo and Chris, I have attached the screenshot above. Please click on Screenshot you will find it. Also I am adding the code here again. I have got date in this format: DATE 43390 43599 Whenever I use any function for eg: df["DATE2"] = pd.to_datetime(pd.Series(df["DATE"])) df[["DATE","DATE2"]].head() I get this: DATE DATE2 0 1970-01-01 00:00:00.000043390 1970-01-01 00:00:00.000043390 1 1970-01-01 00:00:00.000043599 1970-01-01 00:00:00.000043599 2 1970-01-01 00:00:00.000043605 1970-01-01 00:00:00.000043605 – Sampada Pandit Sep 11 '20 at 14:14
  • 1
    This looks like Excel styled dates, try [this question](https://stackoverflow.com/questions/38454403/convert-excel-style-date-with-pandas). ie try `import datetime as dt` and `df['DATE2'] = pd.TimedeltaIndex(df['DATE'], unit='d') + dt.datetime(1900,1,1)` – Chris Adams Sep 11 '20 at 14:17

1 Answers1

0

Try this:

df['DATE'] = df['DATE'].astype('datetime64[ns]')
gtomer
  • 5,643
  • 1
  • 10
  • 21
  • I am getting this date in every column : 1970-01-01 00:00:00.000043390 The last 5 numbers are the ones which I have got in the original file which have int64 as the datatype. So, basically 1970-01-01 00:00:00.0000 is common in every row appended by the last 5 numbers of int64 file. – Sampada Pandit Sep 12 '20 at 03:09