1

Here is an dataframe I'm working with:

     date           price   trade size                                             
     1417412036    300.0    0.010000                                 
     1417412423    300.0    0.010000                       
     1417415048    370.0    0.010000                                         
     1417416612    370.0    0.026556                                               
     1417498166    377.0    0.010000                                 

How do I convert the values of index to human readable dates?

For example 1420705427 should change to 2015-01-08T03:23:47

E.TTT
  • 11
  • 4

2 Answers2

1

A very easy way is to use pd.to_datetime() specifying the unit:

pd.to_datetime(df.date, unit='s')

You can also use transform if you want, but will be slower

import datetime
df.date.transform(datetime.datetime.fromtimestamp)

Both methods yield the same output:

0   2014-12-01 05:33:56
1   2014-12-01 05:40:23
2   2014-12-01 06:24:08
3   2014-12-01 06:50:12
4   2014-12-02 05:29:26
Name: date, dtype: datetime64[ns]
rafaelc
  • 57,686
  • 15
  • 58
  • 82
0

Those numbers are actually epoch times

You could do what RafaelC is doing. An alternative is to do

In [8]: import time

In [9]: time.localtime(1417412036)
Out[9]: time.struct_time(tm_year=2014, tm_mon=11, tm_mday=30, tm_hour=21, tm_min=33, tm_sec=56, tm_wday=6, tm_yday=334, tm_isdst=0)

In [10]: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1417412036))
Out[10]: '2014-11-30 21:33:56'

IS this the kind of readable time you want?

Srini
  • 1,619
  • 1
  • 19
  • 34