{"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":"import pandas as pd\nimport numpy as np\nimport cudf","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:18:55.335911Z","iopub.execute_input":"2023-02-17T13:18:55.336311Z","iopub.status.idle":"2023-02-17T13:18:57.955186Z","shell.execute_reply.started":"2023-02-17T13:18:55.336282Z","shell.execute_reply":"2023-02-17T13:18:57.954170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Objective :**\n- Sort N lists of 1000 items","metadata":{}},{"cell_type":"code","source":"N = 100000\ndf = cudf.DataFrame({'index': np.arange(N, dtype=\"int32\"), \"list\": [list(np.random.randint(0, 1000, 1000).astype(np.uint16)) for _ in range(N)]})","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:01.695232Z","iopub.execute_input":"2023-02-17T13:21:01.695606Z","iopub.status.idle":"2023-02-17T13:21:26.386364Z","shell.execute_reply.started":"2023-02-17T13:21:01.695576Z","shell.execute_reply":"2023-02-17T13:21:26.385345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:26.388448Z","iopub.execute_input":"2023-02-17T13:21:26.388862Z","iopub.status.idle":"2023-02-17T13:21:26.415371Z","shell.execute_reply.started":"2023-02-17T13:21:26.388824Z","shell.execute_reply":"2023-02-17T13:21:26.414466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This does not work with cudf :","metadata":{}},{"cell_type":"code","source":"df['list'] = df['list'].apply(lambda x: np.sort(x))","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:20:24.756745Z","iopub.execute_input":"2023-02-17T13:20:24.757116Z","iopub.status.idle":"2023-02-17T13:20:24.777093Z","shell.execute_reply.started":"2023-02-17T13:20:24.757086Z","shell.execute_reply":"2023-02-17T13:20:24.775362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"But works with pandas !","metadata":{}},{"cell_type":"code","source":"%%time\n\ndf_pandas = df.to_pandas()\n\ndf_pandas['list'] = df_pandas['list'].apply(lambda x: np.sort(x))","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:26.416850Z","iopub.execute_input":"2023-02-17T13:21:26.417216Z","iopub.status.idle":"2023-02-17T13:21:30.468443Z","shell.execute_reply.started":"2023-02-17T13:21:26.417180Z","shell.execute_reply":"2023-02-17T13:21:30.467353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pandas.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:30.470859Z","iopub.execute_input":"2023-02-17T13:21:30.471691Z","iopub.status.idle":"2023-02-17T13:21:30.488318Z","shell.execute_reply.started":"2023-02-17T13:21:30.471653Z","shell.execute_reply":"2023-02-17T13:21:30.487505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The cudf workaround is much faster !","metadata":{}},{"cell_type":"code","source":"%%time\n\ndf = df.explode('list')\ndf = df.sort_values(['index', 'list'])\ndf = df.groupby('index').agg(list).reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:30.489591Z","iopub.execute_input":"2023-02-17T13:21:30.490024Z","iopub.status.idle":"2023-02-17T13:21:31.022570Z","shell.execute_reply.started":"2023-02-17T13:21:30.489987Z","shell.execute_reply":"2023-02-17T13:21:31.021541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T13:21:31.024543Z","iopub.execute_input":"2023-02-17T13:21:31.025198Z","iopub.status.idle":"2023-02-17T13:21:31.055234Z","shell.execute_reply.started":"2023-02-17T13:21:31.025161Z","shell.execute_reply":"2023-02-17T13:21:31.054378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done !","metadata":{}}]}