Python days from timedelta

extracting days from a numpy.timedelta64 value

Even if you’re not interested in hours it might be relevant whether 2 days 23:59 is mapped to 2 days or to 3?

4 Answers 4

You can convert it to a timedelta with a day precision. To extract the integer value of days you divide it with a timedelta of one day.

>>> x = np.timedelta64(2069211000000000, 'ns') >>> days = x.astype('timedelta64[D]') >>> days / np.timedelta64(1, 'D') 23 

Or, as @PhillipCloud suggested, just days.astype(int) since the timedelta is just a 64bit integer that is interpreted in various ways depending on the second parameter you passed in ( ‘D’ , ‘ns’ , . ).

You can find more about it here.

more recent versions of pandas support a full fledged Timedelta type, see docs here: pandas.pydata.org/pandas-docs/stable/timedeltas.html

This is a good candidate for .apply. You can do this in the same line where you compute column values by putting .apply(lambda x: x/np.timedelta64(1,’D’)) at the end to apply the conversion at the column level. e.g. s3=(s1-s2).apply(lambda x: x/np.timedelta64(1,’D’)).

This method astype(‘timedelta64[D]’) (about 96ms) is much more efficient than dt.days. (about 24s) for 4,000,000 rows.

Use dt.days to obtain the days attribute as integers.

In [14]: s = pd.Series(pd.timedelta_range(start='1 days', end='12 days', freq='3000T')) In [15]: s Out[15]: 0 1 days 00:00:00 1 3 days 02:00:00 2 5 days 04:00:00 3 7 days 06:00:00 4 9 days 08:00:00 5 11 days 10:00:00 dtype: timedelta64[ns] In [16]: s.dt.days Out[16]: 0 1 1 3 2 5 3 7 4 9 5 11 dtype: int64 

More generally — You can use the .components property to access a reduced form of timedelta .

In [17]: s.dt.components Out[17]: days hours minutes seconds milliseconds microseconds nanoseconds 0 1 0 0 0 0 0 0 1 3 2 0 0 0 0 0 2 5 4 0 0 0 0 0 3 7 6 0 0 0 0 0 4 9 8 0 0 0 0 0 5 11 10 0 0 0 0 0 

Now, to get the hours attribute:

In [23]: s.dt.components.hours Out[23]: 0 0 1 2 2 4 3 6 4 8 5 10 Name: hours, dtype: int64 

Источник

Convert timedelta to Days in Python (Example)

TikTok Icon Statistics Globe

In this article, I’ll illustrate how to convert timedelta objects to days in Python programming.

The article looks as follows:

Exemplifying Data & Loaded Modules

import datetime # Loading datetime module

Let’s also create a timedelta object in Python:

td = datetime.timedelta(days=75, seconds=54321) # generating sample timedelta print(td) # printing timedelta # 75 days, 15:05:21.600300

Example: Calculate Equivalent Time in Days

The following code demonstrates how to express a timedelta object in terms of days.

seconds_in_day = 60 * 60 * 24 # setting second count in a day day_count = td.days + td.seconds/seconds_in_day # calculating total time in days print(day_count) # printing the result # 75.62872222569443

We can reconstruct the timedelta object in order to confirm our result.

td_days = datetime.timedelta(days=day_count) # timedelta recreation print(td_days) # printing the verification timedelta # 75 days, 15:05:21.600300

As seen, it is identical to the initial timedelta object td.

Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I explain the Python programming codes of this article in the Python programming language.

The YouTube video will be added soon.

Furthermore, you might want to have a look at the other articles on this homepage.

You have learned in this tutorial how to express timedelta objects in days in the Python programming language. Please let me know in the comments section, in case you have additional questions.

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

Источник

Convert a timedelta to days, hours and minutes

I’ve got a timedelta. I want the days, hours and minutes from that — either as a tuple or a dictionary. I’m not fussed. I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so I thought I’d ask here before busting out some nauseatingly simple (yet verbose) mathematics. Mr Fooz raises a good point. I’m dealing with «listings» (a bit like ebay listings) where each one has a duration. I’m trying to find the time left by doing when_added + duration — now Am I right in saying that wouldn’t account for DST? If not, what’s the simplest way to add/subtract an hour?

If it is just to get it as a string in HH:mm:ss format, say «0:19:37» from the timedelta object «datetime.timedelta(seconds=1177)»: simply use str()

14 Answers 14

If you have a datetime.timedelta value td , td.days already gives you the «days» you want. timedelta values keep fraction-of-day as seconds (not directly hours or minutes) so you’ll indeed have to perform «nauseatingly simple mathematics», e.g.:

def days_hours_minutes(td): return td.days, td.seconds//3600, (td.seconds//60)%60 

// is used instead of / to get an integer, not a float. // is a floor division operator: «division that results into whole number adjusted to the left in the number line», see here.

how about adding the remaining seconds as well ? days, hours, minutes = x.days, x.seconds // 3600, x.seconds %3600//60 and then seconds = x.seconds — hours*3600 — minutes*60

WHY are these not in the standard library? days and seconds are available but hours and minutes not? srsly? Some batteries were not included.

This is a bit more compact, you get the hours, minutes and seconds in two lines.

days = td.days hours, remainder = divmod(td.seconds, 3600) minutes, seconds = divmod(remainder, 60) # If you want to take into account fractions of a second seconds += td.microseconds / 1e6 
days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60 

As for DST, I think the best thing is to convert both datetime objects to seconds. This way the system calculates DST for you.

>>> m13 = datetime(2010, 3, 13, 8, 0, 0) # 2010 March 13 8:00 AM >>> m14 = datetime(2010, 3, 14, 8, 0, 0) # DST starts on this day, in my time zone >>> mktime(m14.timetuple()) - mktime(m13.timetuple()) # difference in seconds 82800.0 >>> _/3600 # convert to hours 23.0 

mktime() may fail if the local timezone may have a different utc offset at different times (many do) — you could use pytz module to get access to the tz database in a portable deteministic way. Also, local time may be ambiguous (50% chances of an error) — you need some additional info to disambiguate e.g., often (not always) dates in a log file are monotonous. See How can I subtract a day from a python date? that may have to deal with similar issues.

For all coming along and searching for an implementation:

The above posts are related to datetime.timedelta, which is sadly not having properties for hours and seconds. So far it was not mentioned, that there is a package, which is having these. You can find it here:

>>> import timedelta >>> td = timedelta.Timedelta(days=2, hours=2) # init from datetime.timedelta >>> td = timedelta.Timedelta(datetime1 - datetime2) 
>>> td = timedelta.Timedelta(days=2, hours=2) >>> td.total.seconds 180000 >>> td.total.minutes 3000 >>> td.total.hours 50 >>> td.total.days 2 

I hope this could help someone.

This would also get around the limitation of timedelta, in that it depends on C. Thus, it cannot handle even moderately large times. It can only deal with day counts that are less than 1 billion, and if you use a value larger than +2G, you get a type conversion error as the underlying type appears to be just a 32-bit signed integer.

days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60 
days, hours, minutes = td.days, td.seconds // 3600, td.seconds % 3600 / 60.0 

You get minutes and seconds of a minute as a float.

delta = timedelta() totalMinute, second = divmod(delta.seconds, 60) hour, minute = divmod(totalMinute, 60) print(f"hms") 

Here is a little function I put together to do this right down to microseconds:

def tdToDict(td:datetime.timedelta) -> dict: def __t(t, n): if t < n: return (t, 0) v = t//n return (t - (v * n), v) (s, h) = __t(td.seconds, 3600) (s, m) = __t(s, 60) (micS, milS) = __t(td.microseconds, 1000) return

Here is a version that returns a tuple :

# usage: (_d, _h, _m, _s, _mils, _mics) = tdTuple(td) def tdTuple(td:datetime.timedelta) -> tuple: def _t(t, n): if t < n: return (t, 0) v = t//n return (t - (v * n), v) (s, h) = _t(td.seconds, 3600) (s, m) = _t(s, 60) (mics, mils) = _t(td.microseconds, 1000) return (td.days, h, m, s, mics, mils) 

While pandas.Timedelta does not provide these attributes directly, it indeed provide a method called total_seconds , based on which days, hours, and minutes can be easily derived:

import pandas as pd td = pd.Timedelta("2 days 12:30:00") minutes = td.total_seconds()/60 hours = minutes/60 days = hours/ 24 print(minutes, hours, days) 

Finally the best answer. It is most important for every other answer to mention the total_seconds() property! Otherwise you get weird numbers that are not exactly the seconds.

I found the easiest way is using str(timedelta) . It will return a sting formatted like 3 days, 21:06:40.001000 , and you can parse hours and minutes using simple string operations or regular expression.

While if you are using python datetime package, you can also code like below:

import datetime tap_in = datetime.datetime.strptime("04:12", "%H:%M") tap_out = datetime.datetime.strptime("18:20", "%H:%M") num_of_hour = (tap_out - tap_in).total_seconds()/3600 num_of_hour # 14.133333333333333 

This is another possible approach, though a bit wordier than those already mentioned. It maybe isn't the best approach for this scenario but it is handy to be able to obtain your time duration in a specific unit that isn't stored within the object (weeks, hours, minutes, milliseconds) and without having to remember or calculate conversion factors.

from datetime import timedelta one_hour = timedelta(hours=1) one_minute = timedelta(minutes=1) print(one_hour/one_minute) # Yields 60.0 

I've got a timedelta. I want the days, hours and minutes from that - either as a tuple or a dictionary. I'm not fussed.

in_time_delta = timedelta(days=2, hours=18, minutes=30) td_d = timedelta(days=1) td_h = timedelta(hours=1) td_m = timedelta(minutes=1) dmh_list = [in_time_delta.days, (in_time_delta%td_d)//td_h, (in_time_delta%td_h)//td_m] 

Which should assign [2, 18, 30] to dmh_list

Источник

Читайте также:  Java how to program deitel and deitel pdf
Оцените статью