0

I'm trying to change the time format of my data that's now in form of 15:41:28:4330 or hh:mm:ss:msmsmsms to seconds.

I browsed through some of the pandas documentation but can't seem to find this format anywhere.

Would it be possible to simply calculate the seconds from that time format row by row?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
user2882635
  • 133
  • 2
  • 19
  • important to know is the type, rather than the form: are you talking about [datetime](https://docs.python.org/3/library/datetime.html) objects, or [timedelta](https://docs.python.org/3/library/datetime.html#datetime.timedelta) objects, or is this `15:41:28:4330` a string? – j-i-l Dec 21 '21 at 15:48
  • What do you mean by seconds? What is the intended end format? – Patryk Kowalski Dec 21 '21 at 15:48
  • an expected output would help us help you :) – Adam.Er8 Dec 21 '21 at 15:49
  • 2
    Does this answer your question? [How to convert a time string to seconds?](https://stackoverflow.com/questions/10663720/how-to-convert-a-time-string-to-seconds) – rustyhu Dec 21 '21 at 15:51
  • @jojo How do I detect if it's a str/datetime/timedelta? – user2882635 Dec 22 '21 at 08:10
  • @PatrykKowalski intended end format is a decimal number. – user2882635 Dec 22 '21 at 08:10
  • @Adam.Er8 I'm trying to do numerical integration on a set of time dependant acceleration data. – user2882635 Dec 22 '21 at 08:11
  • My suspicion is that I will lose more time figuring out what format the original time is and trying out libraries I never used before than to just come up with a piece of code to calculate the seconds row by row. – user2882635 Dec 22 '21 at 08:17

1 Answers1

1

You'll want to obtain a timedelta and take the total_seconds method to get seconds after midnight. So you can parse to datetime first, and subtract the default date (that will be added automatically). Ex:

#1 - via datetime

import pandas as pd

df = pd.DataFrame({'time': ["15:41:28:4330"]})
df['time'] = pd.to_datetime(df['time'], format='%H:%M:%S:%f')
df['sec_after_mdnt'] = (df['time']-df['time'].dt.floor('d')).dt.total_seconds()

df
                     time  sec_after_mdnt
0 1900-01-01 15:41:28.433       56488.433

Alternatively, you can clean your time format and parse directly to timedelta:

#2 - str cleaning & to timedelta

df = pd.DataFrame({'time': ["15:41:28:4330"]})
# last separator must be a dot...
df['time'] = df['time'].str[::-1].str.replace(':', '.', n=1, regex=False).str[::-1]
df['sec_after_mdnt'] = pd.to_timedelta(df['time']).dt.total_seconds()

df
            time  sec_after_mdnt
0  15:41:28.4330       56488.433
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • First method works like a charm, the second method returns a deprecated error. Thanks a bunch, that was what I was looking for. This is much faster than calculating row by row in a for loop. – user2882635 Dec 22 '21 at 09:58
  • @user2882635 a deprecated error? That's interesting, I didn't get any, pd.__version__ 1.3.5, could you add details? – FObersteiner Dec 22 '21 at 10:02
  • Error mesage: "FutureWarning: Passing datetime64-dtype data to TimedeltaIndex is deprecated, will raise a TypeError in a future version" I'm using python 2.7, pandas u'0.24.2' (long story, some compatibility issues with openCV and it's just easier to stay on these versions) – user2882635 Dec 22 '21 at 10:06
  • 1
    @user2882635 ok that's not so common anymore these days ^^ Anyway, glad the first method works. – FObersteiner Dec 22 '21 at 10:08