Home > Net >  Convert '00:00' to '00:00:00' pandas
Convert '00:00' to '00:00:00' pandas

Time:11-29

I have a DataFrame that has a column with time data like ['25:45','12:34:34'], the initial idea is: first convert this column to a list called "time_list". Then with a for, iterate and convert it to minutes

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)

But I got this error: ValueError: too many values ​​to unpack... . It is because within the data there are values ​​of only minutes and seconds but also others with hours, minutes and seconds.

My idea is to convert this column into a single format, that of 'hh:mm:ss' but I can't find the way. Thanks for your reply

CodePudding user response:

Instead of using h, m, s = i.split(':') you should be using t = i.split(':') because the first expects the i.split to always return 3 values, but in cases where i = [aa:bb] it will only return two. From there you use the length of t to decide if you need to calculate the seconds or not.

Your code is converting everything to seconds and could be replaced with something like this:

t = i.split(':')
for i in range(len(t)):
    math  = t[i] * 3600 ** (1./(60 ** i))
    # 3600 ** (1./(60 ** i)) returns 3600, 60, 1 for i = 0, 1, 2
time_mins.append(math)

but if your goal is just to append :00 to any entry that dosen't have seconds then you could just do this:

t = i.split(':')
if len(t) == 2:
    time_mins.append(str(i) ":00")
else:
    time_mins.append(i)

CodePudding user response:

I found a solution: I reformatted that column with a function:

  1. Creating function
def format_date(n):
    if len(n) == 5:
        return "{}:{}:{}".format('00', n[0:2], n[3:5])
    else:
        return n**

  1. Applying in that column with labda:
df7['ChipTime'] = df7.apply(lambda x: format_date(x.ChipTime), axis=1)
df7

and done. Thank you.

  • Related