Created
May 3, 2015 16:29
-
-
Save Shinichi-Nakagawa/5ac5acfda26c8f69e548 to your computer and use it in GitHub Desktop.
Retrosheet(MySQL)のデータを読んでデータフレームにして返すコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'Shinichi Nakagawa' | |
import pandas as pd | |
from sqlalchemy import create_engine | |
from configparser import ConfigParser | |
class RetroSheetAnalytics(object): | |
def __init__(self, config_file='config.ini', database_engine='mysql'): | |
config = ConfigParser() | |
config.read(config_file) | |
params = dict(config[database_engine]) | |
connection = "{dialect}+{driver}://{user}:{password}@{host}:{port}/{database}".format(**params) | |
encoding = params.get('encoding') | |
self.engine = create_engine(connection, encoding=encoding) | |
# Roster(選手情報), Games(試合情報)のデータフレームを読み込み | |
self.rosters = self.read_sql_table('rosters') | |
self.games = self.read_sql_table('games') | |
def read_sql_table(self, table_name): | |
""" | |
指定したtableのデータフレームを返す | |
:param table_name: table名 | |
:return: Dataframe | |
""" | |
return pd.read_sql_table(table_name=table_name, con=self.engine) | |
def read_sql_query(self, query): | |
""" | |
検索条件を元にデータフレームを返す | |
:param query: 検索条件 | |
:return: Dataframe | |
""" | |
return pd.read_sql_query(sql=query, con=self.engine) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment