-1

I want to convert the DATE column values (5 integers (dtypes: int64) to datetime, however, after conversion all of my rows are 1970-01-01 with different hours/minutes/seconds.

The first three rows for the DATE column is shown below:

DATE
43390
43359
43531

I used df['DATE'] = pd.to_datetime(df['DATE'],unit = 's')

and my results are the following:

DATE
1970-01-01 12:03:10
1970-01-01 12:02:39
1970-01-01 12:05:31

I want the conversion to show the actual date and time, not 1970-01-01 which clearly is not correct.

I also tried using pd.to_datetime(df['DATE'], format='%Y%m%d') after applied df['DATE'] = pd.to_datetime(df['DATE'],unit = 's') but it gives me the same result.

If I apply pd.to_datetime(df['DATE'], format='%Y%m%d') to the original DATE with int64 dtypes, I got error time data 43390 does not match format '%Y%m%d'

I am struggling to get the current datetime, any suggestions would be extremely grateful, thanks.

Peter
  • 45
  • 1
  • 1
  • 6
  • 2
    What date do you think 43390 is representing and why? – matszwecja May 26 '22 at 09:15
  • 1
    It's assuming that it's converting from [POSIX time](https://kb.narrative.io/what-is-unix-time). Try `datetime.fromtimestamp(43390)` to check that it returns the same. If you want to use some other origin, you can use the `origin` attribute and pass it a timestamp, e.g. `pd.to_datetime(df['DATE'], format='%Y%m%d'), origin="2010-01-01"` – 2293980990 May 26 '22 at 09:31
  • https://stackoverflow.com/a/65460255/13123580, this link answers my question. – Peter May 26 '22 at 12:59

0 Answers0