-2

I have been trying to change the following format of time 8.25 (fractional hours) to 8.15 meaning 8:15. And 17.75 to 17.45 meaning 17:45. The problem is that I specifically need that format (timedelta) e.g 17.45 with a point . instead of :.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Dave Will
  • 95
  • 7
  • 2
    Hint: `60 * .75 = 45`… – deceze Feb 10 '21 at 13:35
  • 2
    Removed `pandas` tag as it is irrelevant. – Mayank Porwal Feb 10 '21 at 13:51
  • 1
    The question is unclear about the type of all the literals. It's telling us something about a *format* but instead only shows confusing examples. – Wolf Feb 10 '21 at 16:45
  • 2
    @Wolf: I think the point here is that a number like `17.75` is not a *time format* but fractional hours, namely a *duration* (to be represented by a `timedelta` in Python). I posted a way how to do this on Dave's [other question](https://stackoverflow.com/a/66141034/10197418). – FObersteiner Feb 10 '21 at 16:56
  • @MrFuppes Maybe, but it could also be some string – Wolf Feb 10 '21 at 17:01
  • @Wolf: why would the datatype matter? you could convert to float in that case. Anyways, I'd avoid representing date & time like that, just too confusing ^^ – FObersteiner Feb 10 '21 at 17:02
  • 1
    Wouldn't it be much less confusing to integrate [your follow-up question](https://stackoverflow.com/q/66140155/2932052) here? – Wolf Feb 10 '21 at 17:22

2 Answers2

2

Given your string of format HH.MM, you can approach it as follows:

my_time = "17.75"
hours, minutes = my_time.split(".")  # (17, 75)
minutes_converted = round(float(minutes) / 100 * 60)  # 45
my_time_converted = "{}.{}".format(hours, minutes_converted)  #17.45
print(my_time_converted)

> 17.45
R. dV
  • 416
  • 1
  • 3
  • 15
  • Thank you very much. It works porperly but the thing now is I would need the other format as well such as ```17:45``` not only ```17.45```. I tried to modify the code above but it did not work either. Given that that value in ```my_time = "17.75"``` is only in that form. – Dave Will Feb 10 '21 at 15:30
  • 1
    @Dave You literally just need to change `{}.{}` to `{}:{}` in the above code… – deceze Feb 10 '21 at 16:49
  • @deceze OP believes to want both? ... a really confusing (and frustrating) case where we are denied the really informative details! – Wolf Feb 10 '21 at 17:13
2

This can be done conveniently by using the appropriate representation for your fractional hours, namely a timedelta. If the input is of datatype string, convert to float first.

Ex:

from datetime import datetime, timedelta

for td in [8.25, 17.75]:
    # add the duration as timedelta to an arbitrary date to get a time object:
    print((datetime(2020,1,1) + timedelta(hours=td)).time())
08:15:00
17:45:00

Using pandas, that could look like

import pandas as pd

s = pd.Series([8.25, 17.75])

refDate = '2020-01-01' # need a date..

t = pd.Timestamp(refDate) + pd.to_timedelta(s, unit='h')

print(t)

# 0   2020-01-01 08:15:00
# 1   2020-01-01 17:45:00
# dtype: datetime64[ns]

print(t.dt.time)

# 0    08:15:00
# 1    17:45:00
# dtype: object
FObersteiner
  • 22,500
  • 8
  • 42
  • 72