1

I can have stored a list of timestamp (in milliseconds) in one column in a dataframe. so i can call them with the following code:

dataframename['columnname']

gives me something like:

0      1512734400000

1      1512738000000

2      1512741600000

...

498    1514527200000

499    1514530800000

I want to convert all of them in to a readable format. I can do it with the following code:

for x in range(len(dataframename["columnname"])):
      dataframename['columnname'][x] = datetime.datetime.fromtimestamp(dataframename['columnname'][x]/1000).strftime('%Y-%m-%d %H:%M:%S')

However, this is taking too much time. Is there a quicker way of doing it?

Thank you in advance for anyone who is answering the question.

Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43
MATTHEW
  • 69
  • 8

2 Answers2

1
import pandas as pd
dataframename['date'] = pd.to_datetime(dataframename['columnname'], unit='ms')

See pandas.to_datetime docs for more examples

E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
Ivan
  • 3,781
  • 16
  • 20
  • Thanks! it works perfectly! Also if i want to adjust according to my time zone, i simply need to add a value after dataframe. for example, for GMT+8: dataframename['date']=pd.to_datetime(dataframename['columnname']+3600*1000*8, unit='ms') – MATTHEW Dec 29 '17 at 14:58
0

Pandas supports timestamp to datetime out-of-the-box:

import pandas as pd
dataframename['datetime'] = pd.to_datetime(dataframename['columnname'], unit='ms')

In case you have multiple columns:

# processing the UNIX timestamps into readable dates (pandas datetime):
ts_cols = dataframename.select_dtypes(['datetime']).columns
for col in ts_cols:
    dataframename['%s' % col] = pd.to_datetime(dataframename['%s' % col], unit='ms')

The performance gain for using the pandas' to_datetime over external date - it's 2x faster on small dataset, and it'd scale.

Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63