0

I have a large dataset with unix (epoch) time that I need to convert to datetime in milliseconds. My code should do the job since I specified to return milliseconds, but somehow it only returns up to seconds. What needs to be changed?

import pandas as pd

# reading in the excel file timestamps.xlsx
# this file contains a column 'epoch' with the unix epoch timestamps
df = pd.read_excel('epoch_time.xlsx')

# translate epochs into human readable and write into newly created column
# timestamps are in ms, hence the unit
df['time'] = pd.to_datetime(df['epoch'], unit='ms')

# remove epoch row
df = df.drop('epoch', axis=1)

# write to excel file 'new_timestamps.xlsx'
# index=False prevents pandas to add the indices as a new column
df.to_excel('new_timestamps.xlsx', index=False)
luigigi
  • 4,146
  • 1
  • 13
  • 30
robmiller505
  • 79
  • 1
  • 9
  • [This](https://stackoverflow.com/questions/6999726/how-can-i-convert-a-datetime-object-to-milliseconds-since-epoch-unix-time-in-p) might answer your question. – 0buz Feb 20 '20 at 12:16
  • please provide an example timestamp – luigigi Feb 20 '20 at 12:17
  • btw you didnt specified to return milliseconds. you set ms as parameter for to_datetime input – luigigi Feb 20 '20 at 12:17

1 Answers1

2

I guess this is what you are looking for:

pd.to_datetime(1582200012345, unit='ms').to_datetime64()

Output:

numpy.datetime64('2020-02-20T12:00:12.345000000')

Or for a column use:

df['time'] = pd.to_datetime(df['time'], unit='ms').apply(lambda x: x.to_datetime64())

Example:

df=pd.DataFrame([1582200012345,1582200012346])
df['time'] = pd.to_datetime(df[0], unit='ms').apply(lambda x: x.to_datetime64())
df
               0                    time
0  1582200012345 2020-02-20 12:00:12.345
1  1582200012345 2020-02-20 12:00:12.346
luigigi
  • 4,146
  • 1
  • 13
  • 30