Hours and time converting to a certain format [closed]

2024/11/20 20:44:32

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 :.

Answer

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, timedeltafor 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 pds = 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
https://en.xdnf.cn/q/119790.html

Related Q&A

Python socket server: listening to multiple clients [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

I have a problem with encoding with russian language for my python script [duplicate]

This question already has answers here:UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)(34 answers)Closed last year.I am trying to send an email fro…

how do you style data frame in Pandas

I have this data frame: dfServer Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc.…

Vacation price program Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Why did push of a Flask app to Heroku failed?

Im simply trying to push my Flask app to Heroku but I encountered the following error: remote: ERROR: Command errored out with exit status 1: remote: command: /app/.heroku/python…

How to navigate through HTMl pages that have paging for their content using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How to merge one list elements with another list elements in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Display and play audio files

I am new to python, and Im trying to build a simple recording program. With the some help from my previous question, I was able to add a timestamp for each recorded fileEDIT:I did some research and dec…

Web Scraping BeautifulSoup - Next Page parsing

Im just learning web scraping & want to output the result of this website to a csv file https://www.avbuyer.com/aircraft/private-jets but am struggling with parsing the next pages here is my code (…

convert sum value to percentage by userid django

Im trying to convert the total sum to a percentage by userid but an error pops up when I try to run the following program. The error is: name mark is not definedBelow is my code for views.pydef attStud…