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


Python DataReader.to_records方法代码示例

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


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

示例1: Equity

# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import to_records [as 别名]
class Equity(object):
    """
    An Equity object provides an interface to Share Price Data for a specified Ticker Code.

    It uses pandas (DataReader and DataFrame) and SQLite as a local data store.

    Use importData() to update the SQLite data from Yahoo (adjusting as necessary) and use dataFrame() to get at the
    adjusted data.
    """

    def __init__(self, tickerCode):
        """
        Constructor.

        There isn't much to see here. It just makes a note of the Ticker Code.

        :param tickerCode: Ticker Code.
        """

        Logger.log(logging.DEBUG, "Log Object Creation", {"scope":__name__, "arguments":" ".join({tickerCode})})

        self._tickerCode = tickerCode


    def importData(self):
        """
        Import (New) Data from Yahoo.
        """

        start = self._getLatestDate()
        end = self._getTodaysDate()

        Logger.log(logging.INFO, "Loading Data", {"scope":__name__, "tickerCode":self._tickerCode, "start":str(start), "end":str(end)})
        self._data = DataReader(self._tickerCode, "yahoo", start, end)

        self._data['Code'] = self._tickerCode

        for item in ['Open', 'High', 'Low']:
            self._data[item] = self._data[item] * self._data['Adj Close'] / self._data['Close']

        self._data.drop('Close', axis=1, inplace=True)
        self._data.rename(columns={'Adj Close':'Close'}, inplace=True)
        self._data['Volume'] = self._data['Volume'].astype(float)

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)

        query = "insert or replace into Equities (Date, Open, High, Low, Volume, Close, Code) values (?,?,?,?,?,?,?)"
        connection.executemany(query, self._data.to_records(index=True))
        connection.commit()

        connection.close()

    def dataFrame(self):

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)
        query = "select * from Equities where Code = '%s'" % (self._tickerCode)
        equityData = read_sql_query(query, connection, 'Date')
        connection.close()

        return equityData


    def _getLatestDate(self):

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)

        query = "select max(Date) from Equities where Code = '%s'" % (self._tickerCode)

        cursor = connection.cursor()

        cursor.execute(query)
        dateString = cursor.fetchone()[0]

        connection.close()

        date = pyswing.constants.pySwingStartDate
        if dateString:
            date = datetime.datetime.strptime(dateString, "%Y-%m-%d %H:%M:%S")

        return date

    def _getTodaysDate(self):

        return datetime.datetime.now()
开发者ID:garyjoy,项目名称:pyswing,代码行数:86,代码来源:equity.py


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