如何使用 Python Pandas 在特定日期时间索引后获取最近的单行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9877391/
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-09-13 15:41:10  来源:igfitidea点击:

How to get the closest single row after a specific datetime index using Python Pandas

pythonpandas

提问by John Cornwell

DataFrame I have:

我有数据帧:

            A   B   C 
2012-01-01  1   2   3 
2012-01-05  4   5   6 
2012-01-10  7   8   9 
2012-01-15  10  11  12 

What I am using now:

我现在使用的是:

date_after = dt.datetime( 2012, 1, 7 )
frame.ix[date_after:].ix[0:1]
Out[1]: 
            A  B  C
2012-01-10  7  8  9

Is there any better way of doing this? I do not like that I have to specify .ix[0:1] instead of .ix[0], but if I don't the output changes to a TimeSeries instead of a single row in a DataFrame. I find it harder to work with a rotated TimeSeries back on top of the original DataFrame.

有没有更好的方法来做到这一点?我不喜欢我必须指定 .ix[0:1] 而不是 .ix[0],但如果我不指定输出更改为 TimeSeries 而不是 DataFrame 中的单行。我发现在原始 DataFrame 之上使用旋转的 TimeSeries 更困难。

Without .ix[0:1]:

没有.ix[0:1]

frame.ix[date_after:].ix[0]
Out[1]: 
A    7
B    8
C    9
Name: 2012-01-10 00:00:00

Thanks,

谢谢,

John

约翰

回答by Wes McKinney

You might want to go directly do the index:

你可能想直接做索引:

i = frame.index.searchsorted(date)
frame.ix[frame.index[i]]

A touch verbose but you could put it in a function. About as good as you'll get (O(log n))

触摸冗长,但你可以把它放在一个函数中。和你会得到的一样好 ( O(log n))

回答by Kevin Zhu

Couldn't resist answering this, even though the question was asked, and answered, in 2012, by Wes himself, and again in 2015, by ajsp. Yes, besides 'truncate', you can also use get_locwith the option 'nearst'

忍不住要回答这个问题,尽管这个问题是在 2012 年由 Wes 本人提出并在 2015 年再次由 ajsp 提出和回答的。是的,除了“截”,你也可以使用get_loc与选项“nearst”

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

回答by ajsp

Couldn't resist answering this, even though the question was asked, and answered, in 2012, by Wes himself. Yes, just use truncate.

尽管 Wes 本人在 2012 年提出并回答了这个问题,但还是忍不住要回答这个问题。是的,只需使用截断。

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