【问题标题】:How to get all day data from index matching one day如何从索引匹配一天获取全天数据
【发布时间】:2015-12-10 16:41:39
【问题描述】:

我有一个数据框 df1 按日期时间索引,数周内每分钟输入一次 示例:

           SAMPLE_TIME       Bottom     Top      Out     state                                                                    
0  2015-07-15 16:41:56      48.625   55.812   43.875        1       
1  2015-07-15 16:42:55      48.750   55.812   43.875        1     
2  2015-07-15 16:43:55      48.937   55.812   43.875        1       
3  2015-07-15 16:44:56      49.125   55.812   43.812        1      
4  2015-07-15 16:45:55      49.312   55.812   43.812        1     

我想找到 Avg(TempBottom,TempTop) 最低的那一天,然后按分钟获取全天数据,这样我就可以绘制那一天,我试过了:

df2 = df1.groupby(pd.TimeGrouper('D')).agg(min) \
.sort(['TempTop','TempBottom'], ascending=[True,True])

这给了我订购的最低温度天数。 示例:

SAMPLE_TIME       Bottom     Top      Out     state                                                                    
2015-10-17       19.994   25.840   21.875        0       
2015-08-29       26.182   28.777   25.937        0       
2015-11-19       19.244   33.027   28.937        0        
2015-11-07       19.744   33.527   28.125        0           

然后我只需要从 df2 获取第一个条目的索引:

 df1[df2.index[1]]

但我收到一个错误:

KeyError: Timestamp('2015-08-29 00:00:00')

【问题讨论】:

    标签: python pandas timestamp datetimeindex


    【解决方案1】:

    来自docs

    警告

    以下选择将引发KeyError;否则这种选择方法将与 pandas 中的其他选择方法不一致(因为这不是切片,也不会解析为切片)

    dft['2013-1-15 12:30:00']

    要选择单行,请使用.loc

    In [71]: dft.loc['2013-1-15 12:30:00']
    Out[71]: 
    A    0.193284
    Name: 2013-01-15 12:30:00, dtype: float64
    

    所以你需要在你的情况下使用loc 方法:

    In [103]: df1.loc[df2.index[0]]
    Out[103]: 
               SAMPLE_TIME  TempBottom  TempTop  TempOut  State  Bypass
    2015-07-15    16:41:56      48.625   55.812   43.875      1       1
    2015-07-15    16:42:55      48.750   55.812   43.875      1       1
    2015-07-15    16:43:55      48.937   55.812   43.875      1       1
    2015-07-15    16:44:56      49.125   55.812   43.812      1       1
    2015-07-15    16:45:55      49.312   55.812   43.812      1       1
    

    编辑

    当您传递单个参数时,它会尝试使用标签进行访问。但是,当您传递间隔时,它将用作切片。您可以通过技巧来传递价值 + 1 天:

    In [276]: df2.index[0]
    Out[276]: Timestamp('2015-07-15 00:00:00', offset='D')
    
    In [277]: df2.index[0] + 1
    Out[277]: Timestamp('2015-07-16 00:00:00', offset='D')
    
    In [278]: df1.loc[df2.index[0]: df2.index[0] + 1]
    Out[278]: 
                         TempBottom  TempTop  TempOut  State  Bypass
    SAMPLE_TIME                                                     
    2015-07-15 16:41:56      48.625   55.812   43.875      1       1
    2015-07-15 16:42:55      48.750   55.812   43.875      1       1
    2015-07-15 16:43:55      48.937   55.812   43.875      1       1
    2015-07-15 16:44:56      49.125   55.812   43.812      1       1
    2015-07-15 16:45:55      49.312   55.812   43.812      1       1
    

    EDIT2

    或者您可以将dateTimestamp 转换为str

    In [355]: df2.index[0]
    Out[355]: Timestamp('2015-07-15 00:00:00', offset='D')
    
    In [356]: df2.index[0].date()
    Out[356]: datetime.date(2015, 7, 15)
    
    In [357]: str(df2.index[0].date())
    Out[357]: '2015-07-15'
    
    In [359]: df1[str(df2.index[0].date())]
    Out[359]: 
                         TempBottom  TempTop  TempOut  State  Bypass
    2015-07-15 16:41:56      48.625   55.812   43.875      1       1
    2015-07-15 16:42:55      48.750   55.812   43.875      1       1
    2015-07-15 16:43:55      48.937   55.812   43.875      1       1
    2015-07-15 16:44:56      49.125   55.812   43.812      1       1
    2015-07-15 16:45:55      49.312   55.812   43.812      1       1
    

    【讨论】:

    • 我试过你的解决方案,但我现在得到了这个而不是KeyError: 'the label [2015-11-04 00:00:00] is not in the [index]'
    • 然而这是有效的df1.loc['2015-11-04'] 问题似乎在于索引中包含的额外精度(时间)
    • 您是否需要将索引存储在df1 中作为日期时间,或者您可以将其转换为日期?如果您可以将其转换为日期,那么您可以使用df1.loc[df2.index[0].date()]
    • 我认为我们使用的是不同版本的python:df2.index[0] + 1 给出了这个错误ValueError: Cannot add integral value to Timestamp without offset.
    • 执行时:df1.loc[df2.index[0].date()] 给出此错误KeyError: 'the label [2015-11-04] is not in the [index]'
    【解决方案2】:

    所以这是我所做的思考过程,结合@Anton Protopopov答案:

    In [1]: df1.ix[df2]
    # call trace
    ValueError: Cannot index with multidimensional key
    
    In [2]: df1.ix[df2.index]
    out[2]:
    SAMPLE_TIME       Bottom     Top      Out     state                                                                    
    2015-10-17          NaN      NaN      NaN      NaN        
    2015-08-29          NaN      NaN      NaN      NaN         
    2015-11-19          NaN      NaN      NaN      NaN        
    2015-11-07          NaN      NaN      NaN      NaN         
    
    In [3]: df1.ix[df2.index[4:5]]
    Out[3]: 
    SAMPLE_TIME       Bottom     Top      Out     state                                                                    
    2015-11-04           NaN      NaN      NaN      NaN     
    
    In [33]: df1.loc[df2.index[4:5]]
    KeyError: "None of [DatetimeIndex(['2015-11-04'], dtype='datetime64[ns]', name=u'SAMPLE_TIME', freq=None, tz=None)] are in the [index]"
    

    最后我放弃了ix 并决定让loc 工作,因为Anton 建议我尝试:

    In [4]: df1.loc[df2.index[0].date()]
    KeyError: 'the label [2015-11-04] is not in the [index]'
    

    让我想到 loc 只接受最终有效的字符串:

    In [5]: df1.loc[df2.index[4].strftime('%Y-%m-%d')]
    Out[5]: 
    SAMPLE_TIME              Bottom     Top      Out     state                                                                    
    2015-11-04 00:00:22      56.256   56.300   43.750        0     
    2015-11-04 00:01:22      56.256   56.300   43.812        0      
    2015-11-04 00:02:22      56.256   56.300   43.812        0       
    2015-11-04 00:03:22      56.256   56.300   43.812        0     
    

    【讨论】:

    • 您可以使用str(df2.index[4].date()) 执行str 函数将date 转换为str,我认为这比strftime 更直接。顺便说一句,您使用的是哪个 pythonpandas 版本?我正在使用3.4.30.17.1
    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2023-04-01
    • 2015-11-10
    相关资源
    最近更新 更多