timedelta

Addition of two datetime.datetime.strptime().time() objects in python

为君一笑 提交于 2019-12-11 05:46:59
问题 I want to add two time values t1 and t2 in format 'HH:MM:SS'. t1 ='12:00:00' t2='02:00:00' t1+t2 should be 14:00:00 I tried t1+t2 . But as t1 & t2 are im string format the output was concatenation 12:00:00 02:00:00 . So I tried to convert in datetime.datetime.strptime().time() object like t1 = datetime.datetime.strptime('12:00:00', '%H:%M:%S').time() t2 = datetime.datetime.strptime('02:00:00', '%H:%M:%S').time() but gives error TypeError: unsupported operand type(s) for +: 'datetime.time' and

Pandas; transform column with MM:SS,decimals into number of seconds

淺唱寂寞╮ 提交于 2019-12-11 04:26:36
问题 Hey: Spent several hours trying to do a quite simple thing,but couldnt figure it out. I have a dataframe with a column, df['Time'] which contains time, starting from 0, up to 20 minutes,like this: 1:10,10 1:16,32 3:03,04 First being minutes, second is seconds, third is miliseconds (only two digits). Is there a way to automatically transform that column into seconds with Pandas, and without making that column the time index of the series? I already tried the following but it wont work: pd.to

How to not start the next day with datetime while adding a timedelta?

匆匆过客 提交于 2019-12-11 03:15:41
问题 Given a current time of 23:30:00 and I add two hours (7200 seconds). How can I get the time of the same day? So I want as a result 25:30:00 . Currently I am only able to get the time of the next day: >>> from datetime import datetime, timedelta >>> current_time = "23:30:00" >>> duration = 3600 >>> (datetime.strptime(current_time, "%H:%M:%S") + timedelta(seconds=duration)).strftime("%H:%M:%S") '00:30:00' 回答1: If you are just wanting to increment hours minutes, seconds and create a string: def

TypeError: can't compare datetime.timedelta to float

安稳与你 提交于 2019-12-10 17:53:13
问题 I'm working on my python script to work out the duration times between start date and end date format like 20140520160000 and 20140520170000 so I can get the hour. I'm having a trouble with this code: if epgDuration >= 0.10 and epgDuration <= 0.30: epgwidth = "250" I get an error when I'm trying to compare the range of the times between 0.10 mins and 0.30 mins. The error I get is: TypeError: can't compare datetime.timedelta to float. The error are jumping on this line: if epgDuration >= 0.10

Pandas adding Time column to Date index

北城余情 提交于 2019-12-10 13:28:57
问题 I have a dataframe, Date index type is Timestamp , Time column is datetime.Time : Time Value Date 2004-05-01 0:15 3.58507 2004-05-02 0:30 3.84625 ... How do I convert it to: Value Date 2004-05-01 0:15 3.74618 2004-05-01 0:30 3.58507 2004-05-01 0:45 3.30998 I wrote a code which does work, but it's not very pythonic: ind = frame.index.get_level_values(0).tolist() tms = frame['Time'] new_ind = [] for i in range(0, len(ind)): tm = tms[i] val = ind[i] + timedelta(hours=tm.hour, minutes=tm.minute,

Python question about time spent

徘徊边缘 提交于 2019-12-10 09:38:40
问题 I would like to know that how much time a particular function has spent during the duration of the program which involves recursion, what is the best way of doing it? Thank you 回答1: The best way would be to run some benchmark tests (to test individual functions) or Profiling (to test an entire application/program). Python comes with built-in Profilers. Alternatively, you could go back to the very basics by simply setting a start time at the beginning of the program, and, at the end of the

calculate datetime-difference in years, months, etc. in a new pandas dataframe column

瘦欲@ 提交于 2019-12-09 08:12:22
问题 I have a pandas dataframe looking like this: Name start end A 2000-01-10 1970-04-29 I want to add a new column providing the difference between the start and end column in years, months, days. So the result should look like: Name start end diff A 2000-01-10 1970-04-29 29y9m etc. the diff column may also be a datetime object or a timedelta object, but the key point for me is, that I can easily get the Year and Month out of it. What I tried until now is: df['diff'] = df['end'] - df['start']

Counting day-of-week-hour pairs between two dates

别说谁变了你拦得住时间么 提交于 2019-12-08 22:07:15
问题 Consider the following list of day-of-week-hour pairs in 24H format: { 'Mon': [9,23], 'Thu': [12, 13, 14], 'Tue': [11, 12, 14], 'Wed': [11, 12, 13, 14] 'Fri': [13], 'Sat': [], 'Sun': [], } and two time points, e.g.: Start: datetime.datetime(2015, 7, 22, 17, 58, 54, 746784) End : datetime.datetime(2015, 8, 30, 10, 22, 36, 363912) Say we need to know how many hours there are between these two datetimes (either rounding up or down) for each of the day-of-week-hour pairs specified above. How can

How to find the date n days ago in Python?

╄→гoц情女王★ 提交于 2019-12-08 15:56:32
问题 Good evening chaps, I would like to write a script where I give python a number of days (let s call it d) and it gives me the date we were d days ago. I am struggling with the module datetime: import datetime tod = datetime.datetime.now() d = timedelta(days = 50) a = tod - h Type Error : unsupported operand type for - : "datetime.timedelta" and "datetime.datetime" Thanks for your help 回答1: You have mixed something up with your variables, you can subtract timedelta d from datetime.datetime.now

Sum overflow TimeDeltas in Python Pandas

社会主义新天地 提交于 2019-12-08 12:37:27
问题 While trying to sum across timedeltas in pandas, it seems to work for a slice but not the whole column. >> d.ix[0:100, 'VOID-DAYS'].sum() Timedelta('2113 days 00:00:00') >> d['VOID-DAYS'].sum() ValueError: overflow in timedelta operation 回答1: If VOID-DAYS represents an integer number of days, convert the Timedeltas into integers: df['VOID-DAYS'] = df['VOID-DAYS'].dt.days import numpy as np import pandas as pd df = pd.DataFrame({'VOID-DAYS': pd.to_timedelta(np.ones((106752,)), unit='D')}) try: