Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding mid-point date between two dates in Python

Given dates:

my_guess = '2014-11-30'
their_guess = '2017-08-30'

How do I split the delta between the dates, returning the correct calendar date?

like image 982
blacksheep9000 Avatar asked Aug 24 '14 15:08

blacksheep9000


People also ask

How do you find the midpoint between two dates?

As we known, the dates are recorded as serial number in Excel, and to get a midpoint of two numbers, you need to add two numbers together and then divided by 2.

How do you get the second date in Python?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.

How do you check if a date is between two dates in pandas?

You can use pandas. Series. between() method to select DataFrame rows between two dates. This method returns a boolean vector representing whether series element lies in the specified range or not.


1 Answers

One way would be to use datetime. Find the difference between two dates, halve it, and add it on the earlier date:

>>> from datetime import datetime
>>> a = datetime(2014, 11, 30)
>>> b = datetime(2017, 8 ,30)
>>> a + (b - a)/2
2016-04-15 00:00:00
like image 134
Alex Riley Avatar answered Oct 07 '22 16:10

Alex Riley