timedelta

Python timedelta issue with negative values

不羁的心 提交于 2019-11-27 20:27:08
Hi I need some help to understand why this is happening. I have a method to track 'time remaining' in an event program: def get_program_time_budget(self): return self.estimated_duration-self.get_program_duration() All fine when the estimated_duration > self.get_program_duration() but when this goes the other way things get funny. Results are displayed to the user: Estimated 11 hours Allocated 10 hours 55 minutes Remaining 5 minutes When the result goes negative it does this: Estimated 11 hours Allocated 11 hours 5 minutes Remaining -1 day 23 hours 55 minutes Any ideas how to get the result -5

Python: How do I get time from a datetime.timedelta object?

耗尽温柔 提交于 2019-11-27 15:55:52
问题 A mysql database table has a column whose datatype is time ( http://dev.mysql.com/doc/refman/5.0/en/time.html ). When the table data is accessed, Python returns the value of this column as a datetime.timedelta object. How do I extract the time out of this? (I didn't really understand what timedelta is for from the python manuals). E.g. The column in the table contains the value "18:00:00" Python-MySQLdb returns this as datetime.timedelta(0, 64800) Please ignore what is below (it does return

Convert timedelta to floating-point

隐身守侯 提交于 2019-11-27 11:28:10
问题 I got a timedelta object from the subtraction of two datetimes. I need this value as floating point for further calculations. All that I've found enables the calculation with floating-points, but the result is still a timedelta object. time_d = datetime_1 - datetime_2 time_d_float = float(time_d) does not work. 回答1: You could use the total_seconds method: time_d_float = time_d.total_seconds() 回答2: In Python 3.2 or higher, you can divide two timedelta s to give a float. This is useful if you

How can I display timedelta in hours:min:sec?

孤街浪徒 提交于 2019-11-27 06:26:22
问题 I am exporting a list of timedeltas to csv and the days really messes up the format. I tried this: while time_list[count] > datetime.timedelta(days = 1): time_list[count] = (time_list[count] - datetime.timedelta(days = 1)) + datetime.timedelta(hours = 24) But it's instantly converted back into days and creates an infinite loop. 回答1: By default the str() conversion of a timedelta will always include the days portion. Internally, the value is always normalised as a number of days, seconds and

Python: Adding hours to pandas timestamp

会有一股神秘感。 提交于 2019-11-27 05:36:07
I read a csv file into pandas dataframe df and I get the following: df.columns Index([u'TDate', u'Hour', u'SPP'], dtype='object') >>> type(df['TDate'][0]) <class 'pandas.tslib.Timestamp'> type(df['Hour'][0]) <type 'numpy.int64'> >>> type(df['TradingDate']) <class 'pandas.core.series.Series'> >>> type(df['Hour']) <class 'pandas.core.series.Series'> Both the Hour and TDate columns have 100 elements. I want to add the corresponding elements of Hour to TDate. I tried the following: import pandas as pd from datetime import date, timedelta as td z3 = pd.DatetimeIndex(df['TDate']).to_pydatetime() +

Python: Difference of 2 datetimes in months [duplicate]

烂漫一生 提交于 2019-11-27 05:35:24
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Best way to find the months between two dates (in python) I would like to know how I can have the exact number of months for this difference: date1 = datetime.strptime(str('2011-08-15 12:00:00'), '%Y-%m-%d %H:%M:%S') date2 = datetime.strptime(str('2012-02-15'), '%Y-%m-%d') date2-date1 results in datetime.timedelta(183, 43200) I would like to know the exact number of months, in this case it should return 5 and

What is the difference between “datetime.timedelta” and “dateutil.relativedelta.relativedelta” when working only with days?

空扰寡人 提交于 2019-11-27 05:32:11
问题 What is the difference between datetime.timedelta (from Python's standard library) and dateutil.relativedelta.relativedelta when working only with days? As far as I understand, timedelta only supports days (and weeks), while relativedelta adds support for periods defined in terms of years, months, weeks or days, as well as defining absolute values for year, month or day. (remember, for the purposes of this question, I don't have to worry about hours, minutes or seconds) Considering that I'm

Understanding timedelta

走远了吗. 提交于 2019-11-27 05:09:48
问题 Given the python code below, please help me understand what is happening there. start_time = time.time() time.sleep(42) end_time = time.time() uptime = end_time - start_time human_uptime = str(datetime.timedelta(seconds=int(uptime))) So I get the difference between start time and end time , on line 5 I round up the duration by casting and what now, what's the further explanation? I know what delta means(average or difference), but why do I have to pass seconds = uptime to timedelta and why

How to calculate time difference by group using pandas?

こ雲淡風輕ζ 提交于 2019-11-27 01:02:19
问题 Problem I want to calculate diff by group. And I don’t know how to sort the time column so that each group results are sorted and positive. The original data : In [37]: df Out[37]: id time 0 A 2016-11-25 16:32:17 1 A 2016-11-25 16:36:04 2 A 2016-11-25 16:35:29 3 B 2016-11-25 16:35:24 4 B 2016-11-25 16:35:46 The result I want Out[40]: id time 0 A 00:35 1 A 03:12 2 B 00:22 notice: the type of time col is timedelta64[ns] Trying In [38]: df['time'].diff(1) Out[38]: 0 NaT 1 00:03:47 2 -1 days +23

Python: How do you convert datetime/timestamp from one timezone to another timezone?

喜夏-厌秋 提交于 2019-11-26 23:02:20
问题 Specifically, given the timezone of my server (system time perspective) and a timezone input, how do I calculate the system time as if it were in that new timezone (regardless of daylight savings, etc)? import datetime current_time = datetime.datetime.now() #system time server_timezone = "US/Eastern" new_timezone = "US/Pacific" current_time_in_new_timezone = ??? 回答1: If you know your origin timezone and the new timezone you want to convert it to, first use pytz.localize to handle any