0

I am trying to convert a column df["time_ro_reply"] which contains only days in decimal to a timedelta format where it contains days, hours, minutes. This makes it more human readable.

I am reading about pd.to_timedelta, but I am struggling implementing it: pd.to_timedelta(df["time_to_reply"]) This returns me only 0.

Sample input:

df["time_ro_reply"]
1.881551
0.903264
2.931560
2.931560

Expected output:

df["time_ro_reply"]
1 days 19 hours 4 minutes
0 days 23 hours 2 minutes
2 days 2 hours 23 minutes
2 days 2 hours 23 minutes
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Solal
  • 611
  • 2
  • 9
  • 26
  • Would be excellent if you could share sample input with expected output. – Mayank Porwal May 14 '20 at 19:59
  • @MayankPorwal Thanks for the feedback I have updated the question. And actually my input are days in a decimal format (example 3.23 days...) – Solal May 14 '20 at 20:03
  • I found a solution but I though there were already a coded function for that: ```def days_hours_minutes(td): return td.days, td.seconds//3600, (td.seconds//60)%60 ``` – Solal May 14 '20 at 20:11
  • I am not sure if there's a coded function specifically for this. Got to do math for it, just like you did. – Mayank Porwal May 14 '20 at 20:14
  • Does this answer your question? [Converting decimal time (HH.HHH) into HH:MM:SS in Python](https://stackoverflow.com/questions/32087209/converting-decimal-time-hh-hhh-into-hhmmss-in-python) – Trenton McKinney May 14 '20 at 21:11

1 Answers1

1

I suggest using using a custom function as follows:

import numpy as np
import pandas as pd

# creating the provided dataframe
df = pd.DataFrame([1.881551, 0.903264, 2.931560, 2.931560],
                   columns = ["time_ro_reply"])

# this function converts a time as a decimal of days into the desired format
def convert_time(time):

    # calculate the days and remaining time
    days, remaining = divmod(time, 1)

    # calculate the hours and remaining time
    hours, remaining = divmod(remaining * 24, 1)

    # calculate the minutes
    minutes = divmod(remaining * 60, 1)[0]

    # a list of the strings, rounding the time values
    strings = [str(round(days)), 'days',
               str(round(hours)), 'hours',
               str(round(minutes)), 'minutes']

    # return the strings concatenated to a single string
    return ' '.join(strings)

# add a new column to the dataframe by applying the function
# to all values of the column 'time_ro_reply' using .apply()
df["desired_output"] = df["time_ro_reply"].apply(lambda t: convert_time(t))

This yields the following dataframe:

    time_ro_reply   desired_output
0   1.881551        1 days 21 hours 9 minutes
1   0.903264        0 days 21 hours 40 minutes
2   2.931560        2 days 22 hours 21 minutes
3   2.931560        2 days 22 hours 21 minutes

However, this yields different outputs than the ones you described. If the 'time_ro_reply' values are indeed to be interpreted as pure decimals, I don't see how you got your expected results. Do you mind sharing how you got them?

I hope the comments explain the code well enough. If not and you are unfamiliar with syntax such as e.g. divmod(), apply(), I suggest looking them up in the Python / Pandas documentations.

Let me know if this helps.

Michael Hodel
  • 2,845
  • 1
  • 5
  • 10