Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime defaulting to 1970 in pandas

I am having issues converting my date in the right format.

I have a column that looks like this: 20130525, stored as an int64.

I am trying to set it up as a date, but having issues.

I wrote a function that looks like this:

def reformat_dates(df):
    df['column'] = pd.to_datetime(df['column'], format = "%Y-%m-%d")

    return df

but when I execute the function, I end up with a column like this:

1970-01-01 00:00:00.020130525

Is there something wrong with my function that makes it default this way? I would like the format to be

2013-05-25
like image 850
pynewbee Avatar asked Jul 30 '18 05:07

pynewbee


2 Answers

I think the column that you are converting is in UNIX timestamp format.

You should use unit='s'.

def reformat_dates(df):
    df['column'] = pd.to_datetime(df['column'], unit='s')
    return df
like image 189
sukesh kamath Avatar answered Sep 19 '22 21:09

sukesh kamath


Can this be useful?

df['column'] = pd.to_datetime(df['column'], format = "%Y%m%d").dt.strftime('%Y-%m-%d') 
like image 24
Joe Avatar answered Sep 18 '22 21:09

Joe