Home > Back-end >  Want to convert time to minutes -> ValueError: not enough values to unpack (expected 3, got 2)
Want to convert time to minutes -> ValueError: not enough values to unpack (expected 3, got 2)

Time:09-03

I wrote a code to convert a column I have that contains time written in this format '36:21' which I am trying to convert to h, m, s format.

time_list = df7[' Chip Time'].tolist()
time_mins = []
for i in time_list:
    h, m, s = i.split(':')
    math = (int(h) * 3600   int(m) * 60   int(s))/60
    time_mins.append(math)
#print(time_mins)

I keep receiving this error whenever I run the code

ValueError                                Traceback (most recent call last)
Input In [43], in <cell line: 6>()
      5 time_mins = []
      6 for i in time_list:
----> 7     h, m, s = i.split(':')
      8     math = (int(h) * 3600   int(m) * 60   int(s))/60
      9     time_mins.append(math)

ValueError: not enough values to unpack (expected 3, got 2)

CodePudding user response:

You wrote that in this column it is given as XX:YY. So if you do split(":") you will get two values not three. If they are hours and minutes then you are missing seconds which is why you have the error.

CodePudding user response:

You are trying to access three values from a list of two.
You are splitting the value 36:21 by :, which gives you ['36', '21']. This has a length of two. However, you are trying to set three values (h, m, and s) to these two values, hence the error.

If you want to remove the h value, try removing the h, , changing the line to:

m, s = i.split(':')

You will then have to update the following lines accordingly.
Alternatively, if you want to leave the lines after it the same, you could define h as 0.

You can also follow that same process if you want to get rid of a different value (m or s) instead of h.

  • Related