在 pandas DataFrame 中查找与时间戳对应的行

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

Find row corresponding to timestamp in pandas DataFrame

pythonpandasdataframe

提问by pbreach

I am trying to find the row number corresponding to a timestamp in a pandas dataframe. I think the way I am currently doing it comes up with ambiguous results and does not select the right row:

我正在尝试在 Pandas 数据框中找到与时间戳对应的行号。我认为我目前的做法会产生模棱两可的结果并且没有选择正确的行:

idx = pd.DatetimeIndex(freq='d', start='1979-01-01', end='2015-12-30')
df = pd.DataFrame(data=randint(-10, high=20, size=(len(idx),2)), index=idx)
row = abs(df.sum(axis=1)- df.ix['2014-05-30'].sum(axis=1)).values.argmin()

when I check my result I get a row number of 77 which gives:

当我检查结果时,我得到的行号为 77,它给出:

df.ix[row]

0    14
1     9
Name: 1979-03-19 00:00:00, dtype: int32

This is not the correct date which should have been '2014-05-30'

这不是正确的日期,应该是“2014-05-30”

Is there a more general way of doing this with the pandas timestamp?

有没有更通用的方法来使用Pandas时间戳来做到这一点?

回答by Jeff

In [12]: np.random.seed(1234)

In [13]: df = pd.DataFrame(data=randint(-10, high=20, size=(len(idx),2)), index=idx)

If you really want the row number

如果你真的想要行号

In [14]: df.index.get_loc('2014-05-30')
Out[14]: array([12933])

In [15]: df.iloc[12933]
Out[15]: 
0    18
1     8
Name: 2014-05-30 00:00:00, dtype: int64

This is partial string indexing, see here: http://pandas-docs.github.io/pandas-docs-travis/timeseries.html#datetimeindex-partial-string-indexing; in this case its the same as if you specified df.loc[Timestamp('2014-05-30')]because its an exact match (e.g. you have daily freq)

这是部分字符串索引,请参见此处:http: //pandas-docs.github.io/pandas-docs-travis/timeseries.html#datetimeindex-partial-string-indexing;在这种情况下,它与您指定的相同,df.loc[Timestamp('2014-05-30')]因为它完全匹配(例如,您有每日频率)

In [16]: df.loc['2014-05-30']
Out[16]: 
0    18
1     8
Name: 2014-05-30 00:00:00, dtype: int64