{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pyarrow as pa\nimport pyarrow.parquet as pq\nimport time\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-04T11:42:38.837238Z","iopub.execute_input":"2022-06-04T11:42:38.837857Z","iopub.status.idle":"2022-06-04T11:42:38.850190Z","shell.execute_reply.started":"2022-06-04T11:42:38.837812Z","shell.execute_reply":"2022-06-04T11:42:38.848630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Loading Train file first 10k rows for getting the dtypes of all columns","metadata":{}},{"cell_type":"code","source":"train_path = '/kaggle/input/amex-default-prediction/train_data.csv'\nparquet_file = 'train_red_mem_32.parquet'\ntrain = pd.read_csv(train_path, nrows=10000)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:42:38.939273Z","iopub.execute_input":"2022-06-04T11:42:38.939910Z","iopub.status.idle":"2022-06-04T11:42:39.404158Z","shell.execute_reply.started":"2022-06-04T11:42:38.939874Z","shell.execute_reply":"2022-06-04T11:42:39.402872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_col_dtypes(data):\n    col_dtypes = {}\n    for col in data.columns:\n        if str(data[col].dtype) == 'float64':\n            col_dtypes[col] = 'float32'\n        elif str(data[col].dtype) == 'int64':\n            col_dtypes[col] = 'int32'\n        elif str(data[col].dtype) == 'object':\n            col_dtypes[col] = 'category'\n        else:\n            col_dtypes[col] = str(data[col].dtype)\n    return col_dtypes","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:42:39.406387Z","iopub.execute_input":"2022-06-04T11:42:39.407259Z","iopub.status.idle":"2022-06-04T11:42:39.417365Z","shell.execute_reply.started":"2022-06-04T11:42:39.407194Z","shell.execute_reply":"2022-06-04T11:42:39.415703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Casting all float64 to 32, int64 to 32 and object columns to category","metadata":{}},{"cell_type":"code","source":"col_dtypes = get_col_dtypes(train)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:42:39.419952Z","iopub.execute_input":"2022-06-04T11:42:39.420691Z","iopub.status.idle":"2022-06-04T11:42:39.444590Z","shell.execute_reply.started":"2022-06-04T11:42:39.420634Z","shell.execute_reply":"2022-06-04T11:42:39.443150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def write_parquet(path, save_loc, col_dtypes, chunk_size=10000):\n    schema = ''\n    writer = ''\n    for i,chunk in enumerate(pd.read_csv(path, dtype=col_dtypes, iterator=True, chunksize=10000)):\n        if i == 0:\n            schema = pa.Table.from_pandas(df=chunk).schema\n            writer = pq.ParquetWriter(save_loc, schema, compression='snappy')\n        table = pa.Table.from_pandas(chunk, schema=schema)\n        writer.write_table(table)\n    return None","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:42:39.447827Z","iopub.execute_input":"2022-06-04T11:42:39.448666Z","iopub.status.idle":"2022-06-04T11:42:39.458120Z","shell.execute_reply.started":"2022-06-04T11:42:39.448612Z","shell.execute_reply":"2022-06-04T11:42:39.456874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Converting csv to praquet file","metadata":{}},{"cell_type":"code","source":"start = time.time()\nwrite_parquet(train_path, parquet_file, col_dtypes)\nend = time.time()\nminutes, seconds = divmod(end-start, 60)\nprint(f\"Time taken for converting csv to parquet: {minutes} minutes and {seconds} seconds....\")","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:42:39.459762Z","iopub.execute_input":"2022-06-04T11:42:39.460372Z","iopub.status.idle":"2022-06-04T11:50:58.277617Z","shell.execute_reply.started":"2022-06-04T11:42:39.460292Z","shell.execute_reply":"2022-06-04T11:50:58.273956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new = pd.read_parquet(parquet_file)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T11:50:58.279476Z","iopub.execute_input":"2022-06-04T11:50:58.280288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}