Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: timestamp to datetime

I have dataframe and column with dates looks like

 date
 1476329529    
 1476329530    
 1476329803 
 1476329805 
 1476329805  
 1476329805 

I use df['date'] = pd.to_datetime(df.date, format='%Y-%m-%d %H:%M:%S') to convert that, but I'm get strange result

 date 
 1970-01-01 00:00:01.476329529   
 1970-01-01 00:00:01.476329530   
 1970-01-01 00:00:01.476329803  
 1970-01-01 00:00:01.476329805    
 1970-01-01 00:00:01.476329805   
 1970-01-01 00:00:01.476329805   

Maybe I did anything wrong

like image 622
Petr Petrov Avatar asked Oct 14 '16 08:10

Petr Petrov


People also ask

How do I change timestamp to datetime?

Timestamp to DateTime object You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I change time stamp to date in pandas?

read_csv(), the timestamps column from the data Dataframe is given as an argument in the to_datetime() for it to be converted into DateTime. unit='s' is used to convert the values of the timestamp column to epoch time after converting the values to DateTime it is stored in a column called 'Datetime' in the Dataframe.

How do I convert a timestamp to a date in Python?

Import the “datetime” file to start timestamp conversion into a date. Create an object and initialize the value of the timestamp. Use the ” fromtimestamp ()” method to place either data or object. Print the date after conversion of the timestamp.

Is timestamp the same as datetime pandas?

Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.


2 Answers

This looks look epoch timestamps which is number of seconds since 1st January 1970 :

In [71]:
pd.to_datetime(df['date'], unit='s')
​
Out[71]:
0   2016-10-13 03:32:09
1   2016-10-13 03:32:10
2   2016-10-13 03:36:43
3   2016-10-13 03:36:45
4   2016-10-13 03:36:45
5   2016-10-13 03:36:45
Name: date, dtype: datetime64[ns]
like image 50
EdChum Avatar answered Oct 05 '22 08:10

EdChum


Something like this maybe:

import datetime
date = datetime.datetime.fromtimestamp(1476329529)
date
# gives
# datetime.datetime(2016, 10, 13, 5, 32, 9)

str(date) # '2016-10-13 05:32:09'
like image 40
milos.ai Avatar answered Oct 05 '22 10:10

milos.ai