pandas:无法使用 Timestamp 的这些索引器 [2016-08-01 00:00:00] 对 DatetimeIndex 进行位置索引

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

pandas: cannot do positional indexing on DatetimeIndex with these indexers [2016-08-01 00:00:00] of Timestamp

pythonpandasdataframe

提问by Homunculus Reticulli

I a newbie using pandas, I am trying to access my (date indexed) dataframe dfusing code similar to this:

我是一个使用 Pandas 的新手,我正在尝试df使用与此类似的代码访问我的(日期索引)数据框:

for idx, row in df.iterrows():
    if idx < startrow:
        continue

    col1_data = df.iloc[idx]['col1']

I get the following error:

我收到以下错误:

cannot do positional indexing on <class 'pandas.core.indexes.datetimes.DatetimeIndex'> with these indexers [2016-08-01 00:00:00] of <class 'pandas._libs.tslib.Timestamp'>

How do I fix this ?

我该如何解决 ?

回答by Psidom

The ilocneeds to be loc, as the former is integer-location based indexing; To select rows by labels (or the actual index as is idx) you need loc:

iloc需求是loc,因为前者是整数,基于位置的索引; 要按标签(或原样的实际索引idx)选择行,您需要loc

col1_data = df.loc[idx]['col1']