【问题标题】:Select some rows from a dataframe with date index从具有日期索引的数据框中选择一些行
【发布时间】:2021-07-13 08:53:59
【问题描述】:

我有一个 DataFrame 作为图像中的那个,我想选择某个时间段内的行,例如从 2005 年初到 2007 年 7 月。

我已经尝试过this question as 中所说的

#Enero 2005-Julio 2007
AntCris=df.loc['2005-01-02 00:00:00':'2007-07-01 00:00:00']

AntCris=df.loc['2005-01-02':'2007-07-01 00:00:00']

甚至

AntCris=Datos.loc[Timestamp('2005-01-02 00:00:00'):Timestamp('2007-07-01 00:00:00')]

但我收到了错误TypeError: '<' not supported between instances of 'Timestamp' and 'str'

此错误已在here 讨论,但我无法解决我的问题。我也试过用

重新索引我的df
A=pd.to_datetime(df.index)
df.set_index(A)

但我得到了TypeError: 'DatetimeIndex' object is not callable

我希望有人可以解决这个问题,因为我不知道我在搞砸什么。

【问题讨论】:

    标签: python pandas dataframe indexing


    【解决方案1】:

    似乎有混合值 - 带有字符串的日期时间,如果需要创建 DatetimeIndex 使用:

    df.index= pd.to_datetime(df.index)
    
    AntCris=df.loc['2005-01-02':'2007-07-01']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 2012-08-10
      • 2021-04-26
      • 2022-09-22
      • 2021-04-05
      • 2014-04-02
      • 2018-11-23
      相关资源
      最近更新 更多