Skip to content
Advertisement

Change a column format while ignoring (or keeping) NaN

I want to change a column from a DataFrame which contains values of this format hh:mm:ss to a column containing the number of minutes (while keeping the NaN values)

I can’t change it directly from the excel file so I’ve tried to do it with pandas (I’m working on a ML model with a health database):

JavaScript

I tried to separate the values from the NaN values using a mask then convert to minutes with str.split()

JavaScript

But then I cannot merge to two series without loosing the order (I get the NaN values with the correct indexes at the end of the merged series)

JavaScript

I also tried to go from hh:mm:ss to minutes with datatime.time and timedelta using a loop (without using a mask) but I still can’t have a column (series or DF) with the all the values in minutes while keeping the NaN …

Advertisement

Answer

You can use pd.to_timedelta to convert the delay column to pandas timedelta series then divide it by Timedelta of 1 min to get total minutes:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement