{"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":"!pip install polars","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:21:44.256216Z","iopub.execute_input":"2023-02-07T03:21:44.257473Z","iopub.status.idle":"2023-02-07T03:22:02.377508Z","shell.execute_reply.started":"2023-02-07T03:21:44.257348Z","shell.execute_reply":"2023-02-07T03:22:02.376295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\nimport pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-07T03:22:02.380314Z","iopub.execute_input":"2023-02-07T03:22:02.380722Z","iopub.status.idle":"2023-02-07T03:22:02.448530Z","shell.execute_reply.started":"2023-02-07T03:22:02.380684Z","shell.execute_reply":"2023-02-07T03:22:02.447394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_memory_usage_pl(df):\n    \n    start_mem = df.estimated_size(\"mb\")\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    # pl.Uint8,pl.UInt16,pl.UInt32,pl.UInt64\n    Numeric_Int_types = [pl.Int8,pl.Int16,pl.Int32,pl.Int64]\n    Numeric_Float_types = [pl.Float32,pl.Float64]\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        c_min = df[col].min()\n        c_max = df[col].max()\n        if col_type in Numeric_Int_types:\n            if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                df = df.with_columns(df[col].cast(pl.Int8))\n            elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                df = df.with_columns(df[col].cast(pl.Int16))\n            elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                df = df.with_columns(df[col].cast(pl.Int32))\n            elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                df = df.with_columns(df[col].cast(pl.Int64))\n\n        elif col_type in Numeric_Float_types:\n            if c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                df = df.with_columns(df[col].cast(pl.Float32))\n            else:\n                pass\n        elif col_type == pl.Utf8:\n            df = df.with_columns(df[col].cast(pl.Categorical))\n        else:\n            pass\n    mem_usg = df.estimated_size(\"mb\")\n    print(\"Memory usage became: \",mem_usg,\" MB\")\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:22:02.450131Z","iopub.execute_input":"2023-02-07T03:22:02.450865Z","iopub.status.idle":"2023-02-07T03:22:02.464720Z","shell.execute_reply.started":"2023-02-07T03:22:02.450823Z","shell.execute_reply":"2023-02-07T03:22:02.463653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pl.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:22:02.466895Z","iopub.execute_input":"2023-02-07T03:22:02.467517Z","iopub.status.idle":"2023-02-07T03:22:24.859139Z","shell.execute_reply.started":"2023-02-07T03:22:02.467478Z","shell.execute_reply":"2023-02-07T03:22:24.858178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col,dtype in zip(train_df.columns,train_df.dtypes):\n    print(f'{col} : {dtype}')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:22:52.791780Z","iopub.execute_input":"2023-02-07T03:22:52.792651Z","iopub.status.idle":"2023-02-07T03:22:52.801071Z","shell.execute_reply.started":"2023-02-07T03:22:52.792608Z","shell.execute_reply":"2023-02-07T03:22:52.798637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = reduce_memory_usage_pl(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:22:59.370088Z","iopub.execute_input":"2023-02-07T03:22:59.370499Z","iopub.status.idle":"2023-02-07T03:23:05.822253Z","shell.execute_reply.started":"2023-02-07T03:22:59.370466Z","shell.execute_reply":"2023-02-07T03:23:05.821394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col,dtype in zip(train_df.columns,train_df.dtypes):\n    print(f'{col} : {dtype}')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T03:24:25.488112Z","iopub.execute_input":"2023-02-07T03:24:25.488509Z","iopub.status.idle":"2023-02-07T03:24:25.499051Z","shell.execute_reply.started":"2023-02-07T03:24:25.488476Z","shell.execute_reply":"2023-02-07T03:24:25.497876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}