{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f1039720-9692-4605-adf9-d37651958e4c", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from pandas import DataFrame, Series" ] }, { "cell_type": "code", "execution_count": 2, "id": "8d39819f-2cac-437f-b5fc-633ca7443f8a", "metadata": {}, "outputs": [], "source": [ "rs = DataFrame({0: range(10, 101, 10), 1: range(10, 20), 2: range(20, 40, 2), \"symbols\": [chr(i) for i in [65]*5 + [68]*5]})" ] }, { "cell_type": "code", "execution_count": 6, "id": "d7976bb8-05cb-4924-a6e2-90ea8af85d9d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012symbols
0101020A
1201122A
2301224A
3401326A
4501428A
\n", "
" ], "text/plain": [ " 0 1 2 symbols\n", "0 10 10 20 A\n", "1 20 11 22 A\n", "2 30 12 24 A\n", "3 40 13 26 A\n", "4 50 14 28 A" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs[rs.symbols == 'A']" ] }, { "cell_type": "code", "execution_count": 71, "id": "eb69c292-79e3-4105-b10c-6f4f5c22074f", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "None", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[71], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m ind \u001b[38;5;241m=\u001b[39m rs[rs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msymbols\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39miloc[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mname\n\u001b[1;32m----> 2\u001b[0m \u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43mind\u001b[49m\u001b[43m]\u001b[49m\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1191\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1189\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[0;32m 1190\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_deprecated_callable_usage(key, maybe_callable)\n\u001b[1;32m-> 1191\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[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmaybe_callable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1431\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1429\u001b[0m \u001b[38;5;66;03m# fall thru to straight lookup\u001b[39;00m\n\u001b[0;32m 1430\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[1;32m-> 1431\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[43m_get_label\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1381\u001b[0m, in \u001b[0;36m_LocIndexer._get_label\u001b[1;34m(self, label, axis)\u001b[0m\n\u001b[0;32m 1379\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_label\u001b[39m(\u001b[38;5;28mself\u001b[39m, label, axis: AxisInt):\n\u001b[0;32m 1380\u001b[0m \u001b[38;5;66;03m# GH#5567 this will fail if the label is not present in the axis.\u001b[39;00m\n\u001b[1;32m-> 1381\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[43mxs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlabel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\generic.py:4301\u001b[0m, in \u001b[0;36mNDFrame.xs\u001b[1;34m(self, key, axis, level, drop_level)\u001b[0m\n\u001b[0;32m 4299\u001b[0m new_index \u001b[38;5;241m=\u001b[39m index[loc]\n\u001b[0;32m 4300\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 4301\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(loc, np\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[0;32m 4304\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m loc\u001b[38;5;241m.\u001b[39mdtype \u001b[38;5;241m==\u001b[39m np\u001b[38;5;241m.\u001b[39mbool_:\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:417\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 415\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Hashable):\n\u001b[1;32m--> 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n\u001b[0;32m 418\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n\u001b[0;32m 419\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n", "\u001b[1;31mKeyError\u001b[0m: None" ] } ], "source": [ "ind = rs[rs['symbols'] != 'A'].iloc[0].index.name\n", "rs.loc[ind]" ] }, { "cell_type": "code", "execution_count": 44, "id": "cce9fa9e-d841-4f45-8813-313f17e7b12f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012symbols
0101020A
1201122A
2301224A
3401326A
4501428A
5601530D
6701632D
7801734D
8901836D
91001938D
\n", "
" ], "text/plain": [ " 0 1 2 symbols\n", "0 10 10 20 A\n", "1 20 11 22 A\n", "2 30 12 24 A\n", "3 40 13 26 A\n", "4 50 14 28 A\n", "5 60 15 30 D\n", "6 70 16 32 D\n", "7 80 17 34 D\n", "8 90 18 36 D\n", "9 100 19 38 D" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs" ] }, { "cell_type": "code", "execution_count": 31, "id": "3c456100-4931-4fbd-a63d-531eaf735e70", "metadata": {}, "outputs": [ { "ename": "InvalidIndexError", "evalue": "RangeIndex(start=0, stop=10, step=1)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mInvalidIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[31], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrs\u001b[49m\u001b[43m[\u001b[49m\u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m31\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:418\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Hashable):\n\u001b[0;32m 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n\u001b[1;32m--> 418\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_check_indexing_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 419\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n", "File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:6059\u001b[0m, in \u001b[0;36mIndex._check_indexing_error\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 6055\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_check_indexing_error\u001b[39m(\u001b[38;5;28mself\u001b[39m, key):\n\u001b[0;32m 6056\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_scalar(key):\n\u001b[0;32m 6057\u001b[0m \u001b[38;5;66;03m# if key is not a scalar, directly raise an error (the code below\u001b[39;00m\n\u001b[0;32m 6058\u001b[0m \u001b[38;5;66;03m# would convert to numpy arrays and raise later any way) - GH29926\u001b[39;00m\n\u001b[1;32m-> 6059\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key)\n", "\u001b[1;31mInvalidIndexError\u001b[0m: RangeIndex(start=0, stop=10, step=1)" ] } ], "source": [ "rs.index.get_loc(rs[rs.index <= 31].index)" ] }, { "cell_type": "code", "execution_count": 73, "id": "1a5a99bb-3fc4-458d-95b8-4e031f3e04c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index([20, 22, 24, 26, 28, 30, 32, 34, 36, 38], dtype='int64', name=2)" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs.index" ] }, { "cell_type": "code", "execution_count": 28, "id": "2d53ccef-72e1-4378-818f-b60682bbd8b7", "metadata": {}, "outputs": [], "source": [ "g = rs[rs.index <= 31].iloc[-1]" ] }, { "cell_type": "code", "execution_count": 87, "id": "7318351e-e185-4695-ada5-43596b4844e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(20)" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs.index[0]" ] }, { "cell_type": "code", "execution_count": 114, "id": "55441116-8c00-4fd1-94c8-939320c6dfc4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[25, 26, 27, 28, 29]" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = list(range(30))\n", "t[25:30]" ] }, { "cell_type": "code", "execution_count": 121, "id": "6ef28e4c-15a5-4fe5-aa66-10ba464f1256", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 7, 8, 9]" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[6: 10]" ] }, { "cell_type": "code", "execution_count": 126, "id": "f15cf18c-54cb-4624-8d18-3998bde2b9ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "p = 0 or None\n", "print(p)" ] }, { "cell_type": "code", "execution_count": null, "id": "47dd4064-dde6-4824-a12b-ecf334bf4ebe", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }