timedelta

Python: Convert timedelta to int in a dataframe

浪尽此生 提交于 2019-11-26 05:20:06
问题 I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use \'datetime.days\' or do I need to do something more manual? timedelta column 7 days, 23:29:00 day integer column 7 回答1: Use the dt.days attribute. Supposing td is the name of your timedelta Series, access this attribute via: td.dt.days You can also get the seconds and microseconds attributes in the same way. 回答2: You could do this, where

How to construct a timedelta object from a simple string

纵饮孤独 提交于 2019-11-26 02:51:56
问题 I\'m writing a function that needs a timedelta input to be passed in as a string. The user must enter something like \"32m\" or \"2h32m\", or even \"4:13\" or \"5hr34m56s\"... Is there a library or something that has this sort of thing already implemented? 回答1: for the 4:13, and other standard formats(but if you don't know which one) use dateutil.parser.parse from python-dateutil For the first format(5hr34m56s), you should parse using regular expressions Here is re-based solution: import re

Format timedelta to string

回眸只為那壹抹淺笑 提交于 2019-11-26 00:11:03
问题 I\'m a Python newbie (2 weeks) and I\'m having trouble formatting a datetime.timedelta object. Here\'s what I\'m trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that duration in the format of hours:minutes. I have tried a variety of methods for doing this and I\'m having difficulty. My current approach is to add methods to the class for my objects that return hours and

Calculating the difference between two Java date instances

此生再无相见时 提交于 2019-11-25 22:13:23
问题 I\'m using Java\'s java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a long representing milliseconds. Is there any simpler, nicer way to get a time delta? 回答1: The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library. Joda Time has a concept of time Interval: Interval interval = new