当前位置: 首页>>代码示例>>Python>>正文


Python DataReader.set_index方法代码示例

本文整理汇总了Python中pandas.io.data.DataReader.set_index方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.set_index方法的具体用法?Python DataReader.set_index怎么用?Python DataReader.set_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas.io.data.DataReader的用法示例。


在下文中一共展示了DataReader.set_index方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import set_index [as 别名]

#.........这里部分代码省略.........


  def end(self):
    """
    Check that the end date has the correct format. Convert to datetime.date format
    Then check that the end date is later than the start date
    """
    if type(self._end) != datetime.date:
      self._end,check = stock_helper.check_date_format(self._end)
      if check == 'fail':
        print ('please use the correct format for the date, dd/mm/yyyy')
        del self
        sys.exit()
    if self._start > self._end:
      print ('The start date must be prior to the end date')
      del self
      sys.exit()
    return self._end


  def get_historical(self):
    """
    If mode = "online", download Yahoo quotes from start to end date in a pandas dataframe
    If mode = "disk", the data is extracted from the hard drive
    """
    if self._mode == "online":
      self._historical = DataReader(self._symbol,'yahoo',self._start,self._end)
    else:
      # check dictionary of symbols and open the file at the correct location
      data = stock_database.extract_series(self._symbol)
      df = pd.DataFrame(data,columns = ['date','Open','High','Low','Close','Volume','Adj Close'])
      self._historical = df[pd.to_datetime(df['date']) > self._start]
      self._historical = self._historical[pd.to_datetime(df['date']) < self._end]
    self._historical = self._historical.set_index('date')
    return self._historical


  def get_name(self):
    """
    Get the name of the company associated with the symbol
    """
    try:
      self._name = SYMBOL_DIC[self._symbol]
    except:
      if self._interactive == True:
        print 'this symbol is currently not in the dictionary'
    return self._name


  def get_symbol(self):
    """
    Get the symbol associated with the company's or index name.
    Careful: If not in interactive mode, using company's name in class
             initialization is not recommended, or it has to be exactly matching
             the name in the dictionary.
    """
    idx = 0
    for company_name in SYMBOL_DIC.values():
      if self._name.lower() == company_name[0]:           # <-- check for exact match first
        self._symbol = SYMBOL_DIC.keys()[idx]
        break
      elif self._name.lower() in company_name[0] and self._interactive == True:  # <-- if partial match and interactive mode, ask the user
        question = eg.ynbox('are you talking about %s?' %(company_name[0]))
        if question == 1:
          self._symbol = SYMBOL_DIC.keys()[idx]
          break
开发者ID:mihwano,项目名称:stock_market,代码行数:70,代码来源:stock_class.py


注:本文中的pandas.io.data.DataReader.set_index方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。