【问题标题】:Select Range of DatetimeIndex Rows Using .loc (Pandas Python 3)使用 .loc (Pandas Python 3) 选择 DatetimeIndex 行的范围
【发布时间】:2018-08-18 05:22:56
【问题描述】:

使用 DatetimeIndex 处理 pandas 系列。 期望的结果是一个数据框,其中包含 .loc[] 函数中指定的范围内的所有行。

当我尝试以下代码时:

aapl.index = pd.to_datetime(aapl.index)
print(aapl.loc[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-12-30')])

我回来了:

Empty DataFrame
Columns: [Open, High, Low, Close, Volume, ExDividend, SplitRatio, 
AdjOpen, AdjHigh, AdjLow, AdjClose, AdjVolume]
Index: []

重申一下,我想要的结果是数据框的一个子集,其中包含 (2010-11-01):(2010-12-30) 范围内的所有行。

【问题讨论】:

  • 打印aapl.head()并将输出粘贴到您的问题中?
  • @anon,以下解决方案之一有帮助吗?如果是这样,请随时接受一个(左侧的绿色勾号),或提出更多问题。

标签: python python-3.x pandas time-series datetimeindex


【解决方案1】:

IIUC:

import pandas_datareader as web
aapl = web.get_data_yahoo('aapl')

aapl.loc['2010-11-01':'2010-12-30']

使用partial string indexing 和切片。

【讨论】:

    【解决方案2】:

    您似乎需要将索引转换为datetime,然后使用标准索引/切片表示法。

    import pandas as pd, numpy as np
    
    df = pd.DataFrame(list(range(365)))
    
    # these lines are for demonstration purposes only
    df['date'] = pd.date_range('2010-1-1', periods=365, freq='D').astype(str)
    df = df.set_index('date')
    
    df.index = pd.to_datetime(df.index)
    
    res = df[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-11-10')]
    
    #               0
    # date           
    # 2010-11-01  304
    # 2010-11-02  305
    # 2010-11-03  306
    # 2010-11-04  307
    # 2010-11-05  308
    # 2010-11-06  309
    # 2010-11-07  310
    # 2010-11-08  311
    # 2010-11-09  312
    # 2010-11-10  313
    

    【讨论】:

      【解决方案3】:

      出于好奇,我尝试将最近的日期作为选择的开始,将最近的日期作为结束。令我惊讶的是,这有效,但时间序列数据的顺序相反。

      在:

      aapl.loc[pd.Timestamp('2010-12-30'):pd.Timestamp('2010-11-01')]
      

      所以...呃,我意识到我的时间序列数据必须是相反的顺序。 现在的问题是,如何将 DatetimeIndex df 排序为正确的顺序?

      希望的订单将第 n 个日期作为最后一行,将最早的日期作为第一行。

      ******编辑******

      aapl.index = pd.to_datetime(aapl.index)
      aapl =  aapl.sort_index(ascending=True)
      
      aaplrange = aapl.loc[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-12-30')]
      

      有效!

      【讨论】:

        猜你喜欢
        • 2021-05-20
        • 2015-09-15
        • 1970-01-01
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        • 2017-12-07
        • 2016-09-04
        • 2020-10-06
        相关资源
        最近更新 更多