1

I'm looking to convert Unix timestamps to datetime but would like to have the values start from 00:00:00:000

example date:

timeStamp,elapsed
1585620084122,2
1585620084122,1
1585620084123,1
1585620084124,6
1585620084124,5

If possible I would like to see something similar to the following

timeStamp,elapsed
00:00:00.000,2
00:00:00.000,1
00:00:00.001,1
00:00:00.002,6
00:00:00.002,5
stiggy008
  • 125
  • 7

1 Answers1

1

Convert column to datetimes by to_datetime and then subtract first value for timedeltas:

df['timeStamp'] = pd.to_datetime(df['timeStamp'], unit='ms')
#sorting for first value
df = df.sort_values('timeStamp')

df['timeStamp'] -= df['timeStamp'].iat[0]
#same like
#df['timeStamp'] = df['timeStamp'] - df['timeStamp'].iat[0]
print (df)
        timeStamp  elapsed
0        00:00:00        2
1        00:00:00        1
2 00:00:00.001000        1
3 00:00:00.002000        6
4 00:00:00.002000        5

Or subtract by minimal value if order is not important:

df['timeStamp'] = pd.to_datetime(df['timeStamp'], unit='ms')

df['timeStamp'] -= df['timeStamp'].min()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    im seeing some incorrect timestamp values such as -1 days +23:59:57.987000. It should be one of these values causing it 1585620084229 1585620084111 1585620082199 – stiggy008 Jun 07 '20 at 05:28
  • @stiggy008 - You can sorting for first values are minimal or subtact minimal value, edited answer. – jezrael Jun 07 '20 at 05:30
  • just one more thing... seems like my script has added 000 to the time, such as 00:00:08.187000. Is there an easy way to remove them? – stiggy008 Jun 07 '20 at 06:02
  • @stiggy008 - Unfortunately need custom function, check [this](https://stackoverflow.com/a/51102096/2901002) solution – jezrael Jun 07 '20 at 06:06