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
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.
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.
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.
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.
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]
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With