Python 查找最接近给定日期的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32237862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:16:00  来源:igfitidea点击:

Find the closest date to a given date

pythondatedatetime

提问by user3600497

I have an array of datetime objects, and I would like to find which element in the array is the closest to a given date (e.g datetime.datetime(2014,12,16))

我有一个 datetime 对象数组,我想找到数组中最接近给定日期的元素(例如datetime.datetime(2014,12,16)

Thispost shows how to find the nearest date which is not before the given date. How can I alter this code so that it can return dates that are before a given date?

这篇文章展示了如何找到不在给定日期之前的最近日期。如何更改此代码以便它可以返回给定日期之前的日期?

For example, if the array housed elements datetime.datetime(2014,12,10)and datetime.datetime(2014,12,28), the former item should be returned because it is closest to datetime.datetime(2014,12,16)in absolute value.

例如,如果数组包含元素datetime.datetime(2014,12,10)and datetime.datetime(2014,12,28),则应返回前一项,因为它datetime.datetime(2014,12,16)在绝对值上最接近。

采纳答案by Tamas Hegedus

This function will return the datetimein itemswhich is the closest to the date pivot.

该函数将返回datetimeitems其最接近的日期pivot

def nearest(items, pivot):
    return min(items, key=lambda x: abs(x - pivot))

The good part this function works on types other than datetimetoo out of the box, if the type supports comparison, subtraction and abs, e.g.: numbers and vector types.

datetime如果类型支持比较、减法和abs,例如:数字和向量类型,则此函数适用于除开箱即用以外的类型。

回答by 3442

def nearestDate(base, dates):
    nearness = { abs(base.timestamp() - date.timestamp()) : date for date in dates }
    return nearness[min(nearness.keys())]

回答by Kevin Zhu

As answered on this linklink, 'truncate' function is there for you.

如此链接链接所回答的那样,“截断”功能就在您身边。

df.truncate(before='2012-01-07')

df.truncate(before='2012-01-07')

Or you can use get_locwith 'nearest' option.

或者您可以将get_loc与 'nearest' 选项一起使用。

df.iloc[df.index.get_loc(datetime.datetime(2016,02,02),method='nearest')]

回答by MarMat

To find a closest date andreturn the timedelta (difference between two dates) I did the following:

要找到最近的日期返回时间增量(两个日期之间的差异),我执行了以下操作:

def nearest_date(items,pivot):
    nearest=min(items, key=lambda x: abs(x - pivot))
    timedelta = abs(nearest - pivot)
    return nearest, timedelta

This may be useful when you have a minimum threshold for nearness for your app like I did.

当您像我一样对应用程序有一个最小接近度阈值时,这可能很有用。

回答by Lorenzo Meneghetti

My solution to find the closest index instead of the value

我的解决方案是找到最接近的索引而不是值

def nearest_ind(items, pivot):
    time_diff = np.abs([date - pivot for date in items])
    return time_diff.argmin(0)

回答by Chiel

This code returns the nearest date beforethe given date:

此代码返回给定日期之前最近的日期:

def nearest(items, pivot):
    return min([i for i in items if i < pivot], key=lambda x: abs(x - pivot))