{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nimport time\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def query_cached_dataframe(key, force_update=False):\n    path = '{}.pickle'.format(key)\n    # check if a file exist\n    exists = os.path.isfile(path)\n    if exists and not force_update:\n        print('cache hits', key)\n        return (True, pd.read_pickle(path))\n    else:\n        print('cache misses', key)\n\n        def do_cache(df):\n            df.to_pickle(path)\n\n        return (False, do_cache)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85f0c4f34553744ec5729273aa580e4530ad00e2"},"cell_type":"code","source":"df = pd.read_csv('../input/new_merchant_transactions.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ebc53fe62cc3d73195cff4dfb2d7e9e86489441"},"cell_type":"code","source":"def run():\n    by = 'card_id'\n    feature = 'purchase_amount'\n    agg_dfs = []\n    for agg in ['sum', 'mean', 'var', 'min', 'max']:\n        new_feature_name = '{}_{}_{}'.format(feature, agg, by)\n        cache_hit, cache_result = query_cached_dataframe(new_feature_name)\n        if cache_hit:\n            agg_df = cache_result\n        else:\n            agg_df = df.groupby(by)[feature].agg(agg).reset_index()\\\n                .rename(columns={feature: new_feature_name})\n            cache_result(agg_df)\n        agg_dfs.append(agg_df)\n    return agg_dfs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f78d486973a4b53e7e2c78d6f2371d1964658339"},"cell_type":"code","source":"start = time.time()\nagg_dfs = run()\nprint(time.time() - start)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c131514c1bae255407e67795af67ba1091de9c9c"},"cell_type":"code","source":"display(agg_dfs[0].head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e59db07b1edb35e1b73ffe995b85b86a86999910"},"cell_type":"code","source":"start = time.time()\nagg_dfs = run()\nprint(time.time() - start)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a64b2e9eb60eced7bf3daa4b4cf9595d04865d9b"},"cell_type":"code","source":"display(agg_dfs[0].head())","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}