timedelta

converting string 'yyyy-mm-dd' into datetime python [duplicate]

你。 提交于 2019-11-30 17:28:57
This question already has an answer here: Converting string into datetime 19 answers I have a raw input from the user such as "2015-01-30"...for the query I am using, the date has to be inputed as a string as such "yyyy-mm-dd". I would like to increment the date by 1 month at end of my loop s.t "2015-01-30" becomes "2015-02-27" (ideally the last business day of the next month). I was hoping someone could help me; I am using PYTHON, the reason I want to convert to datetime is I found a function to add 1 month. Ideally my two questions to be answered are (in Python): 1) how to convert string

Python - Date & Time Comparison using timestamps, timedelta

霸气de小男生 提交于 2019-11-30 17:13:10
I've spent the past hour digging around the Python docs and many SO questions; please forgive me for being another Python newbie trapped by the mystery of time difference in Python. My goal is to determine the difference between the current time and a certain date/time regardless of being in the past/future and return a workable format such as seconds. For example, if the inputs are 2:00PM and 4:00PM (now), I'd like it to say "-7200", representing the event occurred two hours AGO. If the inputs are Fri 4:00PM (now) and Sun 5:00PM the output should be "176400" seconds, representing two days and

converting string 'yyyy-mm-dd' into datetime python [duplicate]

帅比萌擦擦* 提交于 2019-11-30 16:41:36
问题 This question already has answers here : Converting string into datetime (19 answers) Closed 4 years ago . I have a raw input from the user such as "2015-01-30"...for the query I am using, the date has to be inputed as a string as such "yyyy-mm-dd". I would like to increment the date by 1 month at end of my loop s.t "2015-01-30" becomes "2015-02-27" (ideally the last business day of the next month). I was hoping someone could help me; I am using PYTHON, the reason I want to convert to

How can I perform divison on a datetime.timedelta in python?

老子叫甜甜 提交于 2019-11-30 16:26:31
问题 I'd like to be able to do the following: num_intervals = (cur_date - previous_date) / interval_length or print (datetime.now() - (datetime.now() - timedelta(days=5))) / timedelta(hours=12) # won't run, would like it to print '10' but the division operation is unsupported on timedeltas. Is there a way that I can implement divison for timedeltas? Edit: Looks like this was added to Python 3.2 (thanks rincewind!): http://bugs.python.org/issue2706 回答1: Sure, just convert to a number of seconds

Pandas: add timedelta column to datetime column (vectorized)

半世苍凉 提交于 2019-11-30 13:00:35
I have a pandas dataframe with two columns, a date column and an int column, and I'd simply like to add the int column (in days) to the date column. I found a solution using df.apply(), but that was too slow on my full dataset. I don't see a ton of documentation on doing this in a vectorized manner (the closest I could find was this ), so I wanted to make sure the solution I found was the best way to go forward. My raw data is just a column of strings as a column of ints (days). import pandas as pd from datetime import timedelta df = pd.DataFrame([['2016-01-10',28],['2016-05-11',28],['2016-02

Comparing a time delta in python

坚强是说给别人听的谎言 提交于 2019-11-30 10:21:34
问题 I have a variable which is <type 'datetime.timedelta'> and I would like to compare it against certain values. Lets say d produces this datetime.timedelta value 0:00:01.782000 I would like to compare it like this: #if d is greater than 1 minute if d>1:00: print "elapsed time is greater than 1 minute" I have tried converting datetime.timedelta.strptime() but that does seem to work. Is there an easier way to compare this value? 回答1: You'll have to create a new timedelta with the specified amount

From TimeDelta to float days in Pandas

一个人想着一个人 提交于 2019-11-30 02:29:19
问题 I have a TimeDelta column with values that look like this: 2 days 21:54:00.000000000 I would like to have a float representing the number of days, let's say here 2+21/24 = 2.875, neglecting the minutes. Is there a simple way to do this ? I saw an answer suggesting res['Ecart_lacher_collecte'].apply(lambda x: float(x.item().days+x.item().hours/24.)) But I get "AttributeError: 'str' object has no attribute 'item' " Numpy version is '1.10.4' Pandas version is u'0.17.1' The columns has originally

Python - Date & Time Comparison using timestamps, timedelta

故事扮演 提交于 2019-11-30 00:36:28
问题 I've spent the past hour digging around the Python docs and many SO questions; please forgive me for being another Python newbie trapped by the mystery of time difference in Python. My goal is to determine the difference between the current time and a certain date/time regardless of being in the past/future and return a workable format such as seconds. For example, if the inputs are 2:00PM and 4:00PM (now), I'd like it to say "-7200", representing the event occurred two hours AGO. If the

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

放肆的年华 提交于 2019-11-29 01:12:02
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 different value) - Added: Irrespective of the time value in the table, python-MySQLdb seems to only

How can I make a python numpy arange of datetime

故事扮演 提交于 2019-11-29 01:11:08
I have some input data, with timestamps in the input file in the form of hours from the date time specified in the filename. This is a bit useless, so I need to convert it to python datetime.datetime objects, and then put it in a numpy array. I could write a for loop, but I'd like to do something like: numpy.arange(datetime.datetime(2000, 1,1), datetime.datetime(2000, 1,2), datetime.timedelta(hours=1)) which throws a TypeError. Can this be done? I'm stuck with python 2.6 and numpy 1.6.1. nneonneo See NumPy Datetimes and Timedeltas . Basically, you can represent datetimes in NumPy using the