0
            Time    volts
0   15:15:10.951    368
1   15:15:11.950    373
2   15:15:12.950    368
3   15:15:13.949    316
4   15:15:14.949    368
... ... ...
2141    15:50:54.087    337
2142    15:50:55.069    343
2143    15:50:56.085    344
2144    15:50:57.081    339
2145    15:50:58.090    347
def time_convert(x):
  h,m,s = map(int,x.split(':'))
  return int(h) * 3600 + int(m) * 60 + int(s)        

The output I get:

ValueError                                Traceback (most recent call last)
<ipython-input-17-68cf4416cc88> in <module>
----> 1 df['Time'] = df['Time'].apply(time_convert)

4 frames
<ipython-input-12-42bee45f8bd8> in time_convert(x)
      1 def time_convert(x):
----> 2   h,m,s = map(int,x.split(':'))
      3   return int(h) * 3600 + int(m) * 60 + int(s)
      4 
      5 

ValueError: invalid literal for int() with base 10: '10.951'

I was expecting it to be converted to seconds. I only find HH:MM:SS format to seconds for solutions but I have not found any cases regarding SS.SS conversion.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Please, [do not post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – chrslg Nov 02 '22 at 00:14
  • I apologize for that, I am beginner here. I will edit it. Thank you . – Manish Gurung Nov 02 '22 at 00:22
  • 1
    No need to reinvent the wheel here. Convert to a duration and get the seconds from it `df['Time'] = pd.to_timedelta(df['Time']).dt.total_seconds()` as demonstrated in [the answer to the linked question](/a/40485568/15497888) – Henry Ecker Nov 02 '22 at 00:32

1 Answers1

0

I can only surmise what is in your dataframe. And anyway, iterating (even with apply) rows, is generally speaking a bad idea (very slow).

But, as for why it doesn't work, it lies in your conversion function

def time_convert(x):
  h,m,s = map(int,x.split(':'))
  return int(h) * 3600 + int(m) * 60 + int(s)

you are converting to int twice here! Once when mapping int to x.split(':'). And then, when converting each of h,m,s

So, simply

def time_convert(x):
  h,m,s = x.split(':')
  return int(h) * 3600 + int(m) * 60 + int(s)

does the same. And still doesn't work. Because you cannot convert s to int, since it is not one

def time_convert(x):
  h,m,s = x.split(':')
  return int(h) * 3600 + int(m) * 60 + float(s)

As is, your code works. There must be more efficient way, but it works.

More efficient way

df.Time.str[:2].astype(int) is a series of int conversion of the 2 first chars of df for example.

df.Time.str[3:5].astype(int) likewise for 4th and 5th chars.

Likewise df.Time.str[6:].astype(float)

And you can do arithmetic on whole series. So 3600*df.Time.str[:2].astype(int) + 60*df.Time.str[3:5].astype(int) + df.Time.str[6:].astype(float) is the series of values you wanted.

Hence, a fastest version of what you wanted

df['Time'] = 3600*df.Time.str[:2].astype(int) + 60*df.Time.str[3:5].astype(int) + df.Time.str[6:].astype(float)
chrslg
  • 9,023
  • 5
  • 17
  • 31
  • yes, removing map and int worked. Also, changing the int(s) to float(s) worked. Thank you so much. – Manish Gurung Nov 02 '22 at 00:29
  • Well, yes, it works. But still it is a bad idea :D. My second version is not just a little bit faster. It is a game changer. Avoiding iterations (for loops, or apply) makes code some 1000× faster. It is really when we can't avoid them at all, even at the price of convoluted methods, that we should fall back on apply (I think I've used 'apply' only once in my data-scientist life. And, it was when I was starting with pandas. I am pretty sure that nowadays I would find a way not to do it) – chrslg Nov 02 '22 at 00:32
  • I tried your v2 and I must say it is clean and faster then v1. I wanted to avoid using def when working with panda dataframe. I will keep this v2. I am doing an intern as a data-scientist ( since you mentioned you are a DS, it just makes this answer 100X better). I might browse through your stack profile. I am trying to make my codes better and efficient by practicing on it. – Manish Gurung Nov 02 '22 at 01:52