|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 27, |
| 5 | + "execution_count": 64, |
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [ |
8 | 8 | { |
9 | 9 | "name": "stdout", |
10 | 10 | "output_type": "stream", |
11 | 11 | "text": [ |
12 | | - "shape: (8, 3)\n", |
13 | | - "┌────────────────────┬───────┬──────────┐\n", |
14 | | - "│ position ┆ hello ┆ world!!! │\n", |
15 | | - "│ --- ┆ --- ┆ --- │\n", |
16 | | - "│ array[f64, 3] ┆ i64 ┆ bool │\n", |
17 | | - "╞════════════════════╪═══════╪══════════╡\n", |
18 | | - "│ [-1.0, -1.0, -1.0] ┆ 42 ┆ true │\n", |
19 | | - "│ [-1.0, -1.0, 1.0] ┆ 42 ┆ true │\n", |
20 | | - "│ [-1.0, 1.0, -1.0] ┆ 42 ┆ true │\n", |
21 | | - "│ [-1.0, 1.0, 1.0] ┆ 42 ┆ true │\n", |
22 | | - "│ [1.0, -1.0, -1.0] ┆ 42 ┆ true │\n", |
23 | | - "│ [1.0, -1.0, 1.0] ┆ 42 ┆ true │\n", |
24 | | - "│ [1.0, 1.0, -1.0] ┆ 42 ┆ true │\n", |
25 | | - "│ [1.0, 1.0, 1.0] ┆ 42 ┆ true │\n", |
26 | | - "└────────────────────┴───────┴──────────┘\n" |
| 12 | + "shape: (8, 4)\n", |
| 13 | + "┌────────────────────┬───────┬──────────┬─────────────────────────────────┐\n", |
| 14 | + "│ position ┆ hello ┆ world!!! ┆ advanced │\n", |
| 15 | + "│ --- ┆ --- ┆ --- ┆ --- │\n", |
| 16 | + "│ array[f64, 3] ┆ i64 ┆ bool ┆ array[f64, 4] │\n", |
| 17 | + "╞════════════════════╪═══════╪══════════╪═════════════════════════════════╡\n", |
| 18 | + "│ [-1.0, -1.0, -1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 19 | + "│ [-1.0, -1.0, 1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 20 | + "│ [-1.0, 1.0, -1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 21 | + "│ [-1.0, 1.0, 1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 22 | + "│ [1.0, -1.0, -1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 23 | + "│ [1.0, -1.0, 1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 24 | + "│ [1.0, 1.0, -1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 25 | + "│ [1.0, 1.0, 1.0] ┆ 42 ┆ true ┆ [0.992774, 0.11234, … 0.016647… │\n", |
| 26 | + "└────────────────────┴───────┴──────────┴─────────────────────────────────┘\n" |
27 | 27 | ] |
28 | 28 | } |
29 | 29 | ], |
|
56 | 56 | }, |
57 | 57 | { |
58 | 58 | "cell_type": "code", |
59 | | - "execution_count": 24, |
| 59 | + "execution_count": 69, |
60 | 60 | "metadata": {}, |
61 | 61 | "outputs": [ |
62 | 62 | { |
63 | | - "ename": "ComputeError", |
64 | | - "evalue": "datatype array[f64, 3] cannot be written to CSV\n\nConsider using JSON or a binary format.", |
65 | | - "output_type": "error", |
66 | | - "traceback": [ |
67 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
68 | | - "\u001b[0;31mComputeError\u001b[0m Traceback (most recent call last)", |
69 | | - "\u001b[0;32m/var/folders/0n/w5m51rrn71db4cvkds08wdg80000gn/T/ipykernel_10042/3097064528.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Write to temporary file\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mtemp_dir\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtempfile\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgettempdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mtemp_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtemp_dir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"mesh_attributes.csv\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtemp_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"CSV written to: {temp_file}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
70 | | - "\u001b[0;32m~/Library/Application Support/Blender/4.4/extensions/.local/lib/python3.11/site-packages/polars/dataframe/frame.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(***failed resolving arguments***)\u001b[0m\n\u001b[1;32m 2990\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2991\u001b[0m \u001b[0;31m# Handle empty dict input\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2992\u001b[0m \u001b[0mstorage_options\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2993\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2994\u001b[0;31m self._df.write_csv(\n\u001b[0m\u001b[1;32m 2995\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2996\u001b[0m \u001b[0minclude_bom\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2997\u001b[0m \u001b[0minclude_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
71 | | - "\u001b[0;31mComputeError\u001b[0m: datatype array[f64, 3] cannot be written to CSV\n\nConsider using JSON or a binary format." |
| 63 | + "name": "stdout", |
| 64 | + "output_type": "stream", |
| 65 | + "text": [ |
| 66 | + "Column dtypes: [Array(Float64, shape=(3,)), Int64, Boolean, Array(Float64, shape=(4,))]\n", |
| 67 | + "Array column found: position with dtype Array(Float64, shape=(3,))\n", |
| 68 | + "Array column found: advanced with dtype Array(Float64, shape=(4,))\n", |
| 69 | + "Array columns: ['position', 'advanced']\n", |
| 70 | + "DataFrame after expansion:\n", |
| 71 | + "shape: (8, 9)\n", |
| 72 | + "┌───────────┬───────────┬───────────┬───────┬───┬───────────┬───────────┬───────────┬───────────┐\n", |
| 73 | + "│ position1 ┆ position2 ┆ position3 ┆ hello ┆ … ┆ advanced1 ┆ advanced2 ┆ advanced3 ┆ advanced4 │\n", |
| 74 | + "│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n", |
| 75 | + "│ f64 ┆ f64 ┆ f64 ┆ i64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", |
| 76 | + "╞═══════════╪═══════════╪═══════════╪═══════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡\n", |
| 77 | + "│ -1.0 ┆ -1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 78 | + "│ -1.0 ┆ -1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 79 | + "│ -1.0 ┆ 1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 80 | + "│ -1.0 ┆ 1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 81 | + "│ 1.0 ┆ -1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 82 | + "│ 1.0 ┆ -1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 83 | + "│ 1.0 ┆ 1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 84 | + "│ 1.0 ┆ 1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 85 | + "└───────────┴───────────┴───────────┴───────┴───┴───────────┴───────────┴───────────┴───────────┘\n" |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "source": [ |
| 90 | + "# Check dtypes and expand array columns\n", |
| 91 | + "dtypes = df.dtypes\n", |
| 92 | + "print(\"Column dtypes:\", dtypes)\n", |
| 93 | + "\n", |
| 94 | + "# Get columns that are arrays and expand them\n", |
| 95 | + "array_columns = []\n", |
| 96 | + "expanded_df = df.clone()\n", |
| 97 | + "\n", |
| 98 | + "for i, dtype in enumerate(dtypes):\n", |
| 99 | + " col_name = df.columns[i]\n", |
| 100 | + " if str(dtype).startswith('Array'):\n", |
| 101 | + " array_columns.append(col_name)\n", |
| 102 | + " print(f\"Array column found: {col_name} with dtype {dtype}\")\n", |
| 103 | + " \n", |
| 104 | + " # Get the array length from the first non-null value\n", |
| 105 | + " first_array = expanded_df.select(pl.col(col_name)).item(0, 0)\n", |
| 106 | + " array_length = len(first_array)\n", |
| 107 | + " \n", |
| 108 | + " # Expand array into separate columns with indexed names\n", |
| 109 | + " for j in range(array_length):\n", |
| 110 | + " expanded_df = expanded_df.with_columns([\n", |
| 111 | + " pl.col(col_name).arr.get(j).alias(f\"{col_name}{j+1}\")\n", |
| 112 | + " ])\n", |
| 113 | + " # Drop the original array column\n", |
| 114 | + " expanded_df = expanded_df.drop(col_name)\n", |
| 115 | + "\n", |
| 116 | + "# Reorder columns to place position columns first\n", |
| 117 | + "position_columns = [col for col in expanded_df.columns if col.startswith('position')]\n", |
| 118 | + "other_columns = [col for col in expanded_df.columns if not col.startswith('position')]\n", |
| 119 | + "column_order = position_columns + other_columns\n", |
| 120 | + "expanded_df = expanded_df.select(column_order)\n", |
| 121 | + "\n", |
| 122 | + "print(\"Array columns:\", array_columns)\n", |
| 123 | + "print(\"DataFrame after expansion:\")\n", |
| 124 | + "print(expanded_df)" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 71, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "CSV written to: /var/folders/0n/w5m51rrn71db4cvkds08wdg80000gn/T/mesh_attributes.csv\n", |
| 137 | + "shape: (8, 9)\n", |
| 138 | + "┌───────────┬───────────┬───────────┬───────┬───┬───────────┬───────────┬───────────┬───────────┐\n", |
| 139 | + "│ position1 ┆ position2 ┆ position3 ┆ hello ┆ … ┆ advanced1 ┆ advanced2 ┆ advanced3 ┆ advanced4 │\n", |
| 140 | + "│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n", |
| 141 | + "│ f64 ┆ f64 ┆ f64 ┆ i64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", |
| 142 | + "╞═══════════╪═══════════╪═══════════╪═══════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡\n", |
| 143 | + "│ -1.0 ┆ -1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 144 | + "│ -1.0 ┆ -1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 145 | + "│ -1.0 ┆ 1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 146 | + "│ -1.0 ┆ 1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 147 | + "│ 1.0 ┆ -1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 148 | + "│ 1.0 ┆ -1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 149 | + "│ 1.0 ┆ 1.0 ┆ -1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 150 | + "│ 1.0 ┆ 1.0 ┆ 1.0 ┆ 42 ┆ … ┆ 0.992774 ┆ 0.11234 ┆ 0.038769 ┆ 0.016647 │\n", |
| 151 | + "└───────────┴───────────┴───────────┴───────┴───┴───────────┴───────────┴───────────┴───────────┘\n" |
72 | 152 | ] |
73 | 153 | } |
74 | 154 | ], |
|
79 | 159 | "# Write to temporary file\n", |
80 | 160 | "temp_dir = tempfile.gettempdir()\n", |
81 | 161 | "temp_file = os.path.join(temp_dir, \"mesh_attributes.csv\")\n", |
82 | | - "df.write_csv(temp_file)\n", |
| 162 | + "expanded_df.write_csv(temp_file)\n", |
83 | 163 | "print(f\"CSV written to: {temp_file}\")\n", |
84 | | - "print(df)" |
| 164 | + "print(expanded_df)" |
85 | 165 | ] |
86 | 166 | }, |
87 | 167 | { |
|
0 commit comments