Skip to content

Instantly share code, notes, and snippets.

@lschwetlick
Created September 19, 2022 12:37
Show Gist options
  • Save lschwetlick/3e96e4f0cd64e4a428c5f420a0cbbefe to your computer and use it in GitHub Desktop.
Save lschwetlick/3e96e4f0cd64e4a428c5f420a0cbbefe to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"source": [
"import pandas as pd"
],
"outputs": [],
"execution_count": 8,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:30:52.302Z",
"iopub.execute_input": "2022-09-19T12:30:52.307Z",
"iopub.status.idle": "2022-09-19T12:30:52.314Z",
"shell.execute_reply": "2022-09-19T12:30:52.318Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Inserting a list into a cell"
],
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
}
},
{
"cell_type": "code",
"source": [
"qwe = pd.DataFrame({\"a\":[3,4,5], \"n\":[5,4,3]})\n",
"qwe[\"q\"] = \"asd\"\n",
"qwe"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": " a n q\n0 3 5 asd\n1 4 4 asd\n2 5 3 asd",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>a</th>\n <th>n</th>\n <th>q</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>3</td>\n <td>5</td>\n <td>asd</td>\n </tr>\n <tr>\n <th>1</th>\n <td>4</td>\n <td>4</td>\n <td>asd</td>\n </tr>\n <tr>\n <th>2</th>\n <td>5</td>\n <td>3</td>\n <td>asd</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
],
"execution_count": 9,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:30:52.630Z",
"iopub.execute_input": "2022-09-19T12:30:52.636Z",
"iopub.status.idle": "2022-09-19T12:30:52.645Z",
"shell.execute_reply": "2022-09-19T12:30:52.648Z"
}
}
},
{
"cell_type": "code",
"source": [
"qwe.at[0, \"q\"] = [1,2,3]\n",
"qwe"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": " a n q\n0 3 5 [1, 2, 3]\n1 4 4 asd\n2 5 3 asd",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>a</th>\n <th>n</th>\n <th>q</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>3</td>\n <td>5</td>\n <td>[1, 2, 3]</td>\n </tr>\n <tr>\n <th>1</th>\n <td>4</td>\n <td>4</td>\n <td>asd</td>\n </tr>\n <tr>\n <th>2</th>\n <td>5</td>\n <td>3</td>\n <td>asd</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
],
"execution_count": 10,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:30:52.789Z",
"iopub.execute_input": "2022-09-19T12:30:52.793Z",
"iopub.status.idle": "2022-09-19T12:30:52.803Z",
"shell.execute_reply": "2022-09-19T12:30:52.806Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"works fine!\n",
"\n",
"Inserting a list into a cell of a multilevel dataframe does not work"
],
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
}
},
{
"cell_type": "code",
"source": [
"qwe = pd.DataFrame({\"a\":[3,4,5], \"n\":[5,4,3]})\n",
"qwe = pd.concat([qwe, qwe], axis = 1, keys=(list('AB')))\n",
"qwe[\"q\"] = \"asd\"\n",
"qwe"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": " A B q\n a n a n \n0 3 5 3 5 asd\n1 4 4 4 4 asd\n2 5 3 5 3 asd",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead tr th {\n text-align: left;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr>\n <th></th>\n <th colspan=\"2\" halign=\"left\">A</th>\n <th colspan=\"2\" halign=\"left\">B</th>\n <th>q</th>\n </tr>\n <tr>\n <th></th>\n <th>a</th>\n <th>n</th>\n <th>a</th>\n <th>n</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>3</td>\n <td>5</td>\n <td>3</td>\n <td>5</td>\n <td>asd</td>\n </tr>\n <tr>\n <th>1</th>\n <td>4</td>\n <td>4</td>\n <td>4</td>\n <td>4</td>\n <td>asd</td>\n </tr>\n <tr>\n <th>2</th>\n <td>5</td>\n <td>3</td>\n <td>5</td>\n <td>3</td>\n <td>asd</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
],
"execution_count": 12,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:31:47.248Z",
"iopub.execute_input": "2022-09-19T12:31:47.252Z",
"iopub.status.idle": "2022-09-19T12:31:47.260Z",
"shell.execute_reply": "2022-09-19T12:31:47.265Z"
}
}
},
{
"cell_type": "code",
"source": [
"print(f\" 'loc' getter works fine for qwe.at[0, ('A', 'n')] {qwe.loc[0, ('A', 'n')]}\")\n",
"print(f\" 'at' getter works fine for qwe.at[0, ('A', 'n')] {qwe.at[0, ('A', 'n')]}\")\n",
"print(f\" 'loc' getter works fine for qwe.loc[0, 'q'] {qwe.loc[0, 'q']}\")\n",
"print(f\" 'at' getter doesnt work for qwe.loc[0, 'q'] {qwe.at[0, 'q']}\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" 'loc' getter works fine for qwe.at[0, ('A', 'n')] 5\n",
" 'at' getter works fine for qwe.at[0, ('A', 'n')] 5\n",
" 'loc' getter works fine for qwe.loc[0, 'q'] asd\n",
"Name: 0, dtype: object\n"
]
},
{
"output_type": "error",
"ename": "TypeError",
"evalue": "only integer scalar arrays can be converted to a scalar index",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [15]\u001b[0m, in \u001b[0;36m<cell line: 4>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mat\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m getter works fine for qwe.at[0, (\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m)] \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mqwe\u001b[38;5;241m.\u001b[39mat[\u001b[38;5;241m0\u001b[39m, (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mn\u001b[39m\u001b[38;5;124m'\u001b[39m)]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mloc\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m getter works fine for qwe.loc[0, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m] \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mqwe\u001b[38;5;241m.\u001b[39mloc[\u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mat\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m getter doesnt work for qwe.loc[0, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m] \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mqwe\u001b[38;5;241m.\u001b[39mat[\u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:2270\u001b[0m, in \u001b[0;36m_AtIndexer.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 2267\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid call for scalar access (getting)!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2268\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39mloc[key]\n\u001b[0;32m-> 2270\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__getitem__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:2221\u001b[0m, in \u001b[0;36m_ScalarAccessIndexer.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 2218\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid call for scalar access (getting)!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2220\u001b[0m key \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_convert_key(key)\n\u001b[0;32m-> 2221\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_value\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtakeable\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_takeable\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3615\u001b[0m, in \u001b[0;36mDataFrame._get_value\u001b[0;34m(self, index, col, takeable)\u001b[0m\n\u001b[1;32m 3612\u001b[0m series \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_ixs(col, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 3613\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m series\u001b[38;5;241m.\u001b[39m_values[index]\n\u001b[0;32m-> 3615\u001b[0m series \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_item_cache\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcol\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3616\u001b[0m engine \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39m_engine\n\u001b[1;32m 3618\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex, MultiIndex):\n\u001b[1;32m 3619\u001b[0m \u001b[38;5;66;03m# CategoricalIndex: Trying to use the engine fastpath may give incorrect\u001b[39;00m\n\u001b[1;32m 3620\u001b[0m \u001b[38;5;66;03m# results if our categories are integers that dont match our codes\u001b[39;00m\n\u001b[1;32m 3621\u001b[0m \u001b[38;5;66;03m# IntervalIndex: IntervalTree has no get_loc\u001b[39;00m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3932\u001b[0m, in \u001b[0;36mDataFrame._get_item_cache\u001b[0;34m(self, item)\u001b[0m\n\u001b[1;32m 3927\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m res \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 3928\u001b[0m \u001b[38;5;66;03m# All places that call _get_item_cache have unique columns,\u001b[39;00m\n\u001b[1;32m 3929\u001b[0m \u001b[38;5;66;03m# pending resolution of GH#33047\u001b[39;00m\n\u001b[1;32m 3931\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mget_loc(item)\n\u001b[0;32m-> 3932\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ixs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mloc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3934\u001b[0m cache[item] \u001b[38;5;241m=\u001b[39m res\n\u001b[1;32m 3936\u001b[0m \u001b[38;5;66;03m# for a chain\u001b[39;00m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3441\u001b[0m, in \u001b[0;36mDataFrame._ixs\u001b[0;34m(self, i, axis)\u001b[0m\n\u001b[1;32m 3437\u001b[0m \u001b[38;5;66;03m# icol\u001b[39;00m\n\u001b[1;32m 3438\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 3439\u001b[0m label \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns[i]\n\u001b[0;32m-> 3441\u001b[0m col_mgr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3442\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_box_col_values(col_mgr, i)\n\u001b[1;32m 3444\u001b[0m \u001b[38;5;66;03m# this is a cached value, mark it so\u001b[39;00m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/internals/managers.py:1020\u001b[0m, in \u001b[0;36mBlockManager.iget\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m 1016\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21miget\u001b[39m(\u001b[38;5;28mself\u001b[39m, i: \u001b[38;5;28mint\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m SingleBlockManager:\n\u001b[1;32m 1017\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1018\u001b[0m \u001b[38;5;124;03m Return the data as a SingleBlockManager.\u001b[39;00m\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1020\u001b[0m block \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mblocks\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mblknos\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1021\u001b[0m values \u001b[38;5;241m=\u001b[39m block\u001b[38;5;241m.\u001b[39miget(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mblklocs[i])\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;66;03m# shortcut for select a single-dim from a 2-dim BM\u001b[39;00m\n",
"\u001b[0;31mTypeError\u001b[0m: only integer scalar arrays can be converted to a scalar index"
]
}
],
"execution_count": 15,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:32:51.005Z",
"iopub.execute_input": "2022-09-19T12:32:51.042Z",
"iopub.status.idle": "2022-09-19T12:32:01.798Z",
"shell.execute_reply": "2022-09-19T12:32:01.803Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"The setter also has a similar problem but with a different error"
],
"metadata": {
"nteract": {
"transient": {
"deleting": false
}
}
}
},
{
"cell_type": "code",
"source": [
"qwe.at[0, (\"A\", \"n\")] = 6 # Works fine!"
],
"outputs": [],
"execution_count": 17,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:33:50.976Z",
"iopub.execute_input": "2022-09-19T12:33:50.981Z",
"iopub.status.idle": "2022-09-19T12:33:50.989Z",
"shell.execute_reply": "2022-09-19T12:33:51.060Z"
}
}
},
{
"cell_type": "code",
"source": [
"qwe.at[0, \"q\"] = 6 # Works fine!"
],
"outputs": [],
"execution_count": 19,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:34:03.071Z",
"iopub.execute_input": "2022-09-19T12:34:03.077Z",
"iopub.status.idle": "2022-09-19T12:34:03.085Z",
"shell.execute_reply": "2022-09-19T12:34:03.089Z"
}
}
},
{
"cell_type": "code",
"source": [
"qwe.at[0, \"q\"] = [9,8,7] # Error"
],
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "Must have equal len keys and value when setting with an iterable",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3868\u001b[0m, in \u001b[0;36mDataFrame._set_value\u001b[0;34m(self, index, col, value, takeable)\u001b[0m\n\u001b[1;32m 3867\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 3868\u001b[0m series \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_item_cache\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcol\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3869\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mget_loc(index)\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3932\u001b[0m, in \u001b[0;36mDataFrame._get_item_cache\u001b[0;34m(self, item)\u001b[0m\n\u001b[1;32m 3931\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mget_loc(item)\n\u001b[0;32m-> 3932\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_ixs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mloc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3934\u001b[0m cache[item] \u001b[38;5;241m=\u001b[39m res\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3441\u001b[0m, in \u001b[0;36mDataFrame._ixs\u001b[0;34m(self, i, axis)\u001b[0m\n\u001b[1;32m 3439\u001b[0m label \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns[i]\n\u001b[0;32m-> 3441\u001b[0m col_mgr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3442\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_box_col_values(col_mgr, i)\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/internals/managers.py:1020\u001b[0m, in \u001b[0;36mBlockManager.iget\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m 1017\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1018\u001b[0m \u001b[38;5;124;03mReturn the data as a SingleBlockManager.\u001b[39;00m\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m-> 1020\u001b[0m block \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mblocks\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mblknos\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1021\u001b[0m values \u001b[38;5;241m=\u001b[39m block\u001b[38;5;241m.\u001b[39miget(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mblklocs[i])\n",
"\u001b[0;31mTypeError\u001b[0m: only integer scalar arrays can be converted to a scalar index",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [20]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m qwe\u001b[38;5;241m.\u001b[39mat[\u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m9\u001b[39m,\u001b[38;5;241m8\u001b[39m,\u001b[38;5;241m7\u001b[39m]\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:2281\u001b[0m, in \u001b[0;36m_AtIndexer.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 2278\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39mloc[key] \u001b[38;5;241m=\u001b[39m value\n\u001b[1;32m 2279\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m-> 2281\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__setitem__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:2236\u001b[0m, in \u001b[0;36m_ScalarAccessIndexer.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 2233\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(key) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mndim:\n\u001b[1;32m 2234\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNot enough indexers for scalar access (setting)!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 2236\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_set_value\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtakeable\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_takeable\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/frame.py:3880\u001b[0m, in \u001b[0;36mDataFrame._set_value\u001b[0;34m(self, index, col, value, takeable)\u001b[0m\n\u001b[1;32m 3878\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39miloc[index, col] \u001b[38;5;241m=\u001b[39m value\n\u001b[1;32m 3879\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 3880\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mloc[index, col] \u001b[38;5;241m=\u001b[39m value\n\u001b[1;32m 3881\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_item_cache\u001b[38;5;241m.\u001b[39mpop(col, \u001b[38;5;28;01mNone\u001b[39;00m)\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:716\u001b[0m, in \u001b[0;36m_LocationIndexer.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 713\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_valid_setitem_indexer(key)\n\u001b[1;32m 715\u001b[0m iloc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124miloc\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39miloc\n\u001b[0;32m--> 716\u001b[0m \u001b[43miloc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_setitem_with_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindexer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:1688\u001b[0m, in \u001b[0;36m_iLocIndexer._setitem_with_indexer\u001b[0;34m(self, indexer, value, name)\u001b[0m\n\u001b[1;32m 1685\u001b[0m \u001b[38;5;66;03m# align and set the values\u001b[39;00m\n\u001b[1;32m 1686\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m take_split_path:\n\u001b[1;32m 1687\u001b[0m \u001b[38;5;66;03m# We have to operate column-wise\u001b[39;00m\n\u001b[0;32m-> 1688\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_setitem_with_indexer_split_path\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindexer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1689\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1690\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_setitem_single_block(indexer, value, name)\n",
"File \u001b[0;32m~/Documents/virtual_envs/sawenv/lib/python3.10/site-packages/pandas/core/indexing.py:1743\u001b[0m, in \u001b[0;36m_iLocIndexer._setitem_with_indexer_split_path\u001b[0;34m(self, indexer, value, name)\u001b[0m\n\u001b[1;32m 1738\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(value) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_integer(info_axis):\n\u001b[1;32m 1739\u001b[0m \u001b[38;5;66;03m# This is a case like df.iloc[:3, [1]] = [0]\u001b[39;00m\n\u001b[1;32m 1740\u001b[0m \u001b[38;5;66;03m# where we treat as df.iloc[:3, 1] = 0\u001b[39;00m\n\u001b[1;32m 1741\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_setitem_with_indexer((pi, info_axis[\u001b[38;5;241m0\u001b[39m]), value[\u001b[38;5;241m0\u001b[39m])\n\u001b[0;32m-> 1743\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 1744\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMust have equal len keys and value \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1745\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwhen setting with an iterable\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1746\u001b[0m )\n\u001b[1;32m 1748\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m lplane_indexer \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(value) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39mindex):\n\u001b[1;32m 1749\u001b[0m \u001b[38;5;66;03m# We get here in one case via .loc with a all-False mask\u001b[39;00m\n\u001b[1;32m 1750\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n",
"\u001b[0;31mValueError\u001b[0m: Must have equal len keys and value when setting with an iterable"
]
}
],
"execution_count": 20,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2022-09-19T12:34:24.764Z",
"iopub.execute_input": "2022-09-19T12:34:24.805Z"
}
}
},
{
"cell_type": "code",
"source": [],
"outputs": [],
"execution_count": null,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
}
}
}
],
"metadata": {
"kernel_info": {
"name": "sawenv"
},
"language_info": {
"name": "python",
"version": "3.10.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"argv": [
"/Users/lisa/Documents/virtual_envs/sawenv/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "sawenv",
"language": "python",
"metadata": {
"debugger": true
},
"name": "sawenv"
},
"nteract": {
"version": "0.28.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment