Statement of Completion#528e788c
Intro to Pandas for Data Analysis
easy
Introduction to Pandas
Resolution
Activities
Project.ipynb
1. Loading the Data¶
Import pandas:
In [1]:
import pandas as pd
Load the data:
In [2]:
df = pd.read_csv("s&p500.csv", index_col='Date', parse_dates=True)
Take a quick peek at the data:
In [3]:
df.head()
Out[3]:
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2017-01-03 | 2251.570068 | 2263.879883 | 2245.129883 | 2257.830078 | 2257.830078 | 3773010000 |
2017-01-04 | 2261.600098 | 2272.820068 | 2261.600098 | 2270.750000 | 2270.750000 | 3768890000 |
2017-01-05 | 2268.179932 | 2271.500000 | 2260.449951 | 2269.000000 | 2269.000000 | 3785080000 |
2017-01-06 | 2271.139893 | 2282.100098 | 2264.060059 | 2276.979980 | 2276.979980 | 3342080000 |
2017-01-09 | 2273.590088 | 2275.489990 | 2268.899902 | 2268.899902 | 2268.899902 | 3219730000 |
In [4]:
df.tail()
Out[4]:
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2022-12-23 | 3815.110107 | 3845.800049 | 3797.010010 | 3844.820068 | 3844.820068 | 2819280000 |
2022-12-27 | 3843.340088 | 3846.649902 | 3813.219971 | 3829.250000 | 3829.250000 | 3030300000 |
2022-12-28 | 3829.560059 | 3848.320068 | 3780.780029 | 3783.219971 | 3783.219971 | 3083520000 |
2022-12-29 | 3805.449951 | 3858.189941 | 3805.449951 | 3849.280029 | 3849.280029 | 3003680000 |
2022-12-30 | 3829.060059 | 3839.850098 | 3800.340088 | 3839.500000 | 3839.500000 | 2979870000 |
2. Analyzing the Data¶
Quick summary statistics of our DataFrame:
In [5]:
df.describe()
Out[5]:
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
count | 1510.000000 | 1510.000000 | 1510.000000 | 1510.000000 | 1510.000000 | 1.510000e+03 |
mean | 3283.215944 | 3301.894648 | 3262.660734 | 3283.391821 | 3283.391821 | 4.096083e+09 |
std | 716.906855 | 722.545366 | 710.808656 | 716.838804 | 716.838804 | 1.054008e+09 |
min | 2251.570068 | 2263.879883 | 2191.860107 | 2237.399902 | 2237.399902 | 1.296530e+09 |
25% | 2713.589966 | 2729.655090 | 2697.770020 | 2712.177429 | 2712.177429 | 3.418972e+09 |
50% | 3006.380005 | 3016.780029 | 2991.500000 | 3005.584961 | 3005.584961 | 3.858670e+09 |
75% | 3919.070007 | 3945.934998 | 3892.040039 | 3916.182434 | 3916.182434 | 4.526435e+09 |
max | 4804.509766 | 4818.620117 | 4780.040039 | 4796.560059 | 4796.560059 | 9.976520e+09 |
Single column statistics:
In [6]:
df['Close'].min()
Out[6]:
2237.39990234375
In [7]:
df['Close'].max()
Out[7]:
4796.56005859375
Single column selection:
In [8]:
df['Close'].head()
Out[8]:
Date 2017-01-03 2257.830078 2017-01-04 2270.750000 2017-01-05 2269.000000 2017-01-06 2276.979980 2017-01-09 2268.899902 Name: Close, dtype: float64
Visualizations:¶
A simple line chart showing Close price:
In [9]:
df['Close'].plot(figsize=(14, 7), title='S&P Closing Price | 2017 - 2022')
Out[9]:
<Axes: title={'center': 'S&P Closing Price | 2017 - 2022'}, xlabel='Date'>
A more advanced chart combining Close Price
and Volume
:
In [10]:
ax1 = df['Close'].plot(figsize=(14, 7), title='S&P Closing Price | 2017 - 2022')
ax2 = ax1.twinx()
df['Volume'].plot(ax=ax2, color='red', ylim=[df['Volume'].min(), df['Volume'].max() * 5])
ax1.figure.legend(["Close", "Volume"])
Out[10]:
<matplotlib.legend.Legend at 0x730b8de46250>
A few statistical visualizations.
A histogram:
In [11]:
df['Volume'].plot(kind='hist')
Out[11]:
<Axes: ylabel='Frequency'>
A box plot:
In [12]:
df['Volume'].plot(kind='box', vert=False)
Out[12]:
<Axes: >
3. Data Wrangling¶
Close
price and it's SMA (Simple Moving Average):
In [ ]:
df['Close SMA'] = df['Close'].rolling(60).mean()
In [16]:
df['Close SMA'].tail(10)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Close SMA' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[16], line 1 ----> 1 df['Close SMA'].tail(10) File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key) 4100 if self.columns.nlevels > 1: 4101 return self._getitem_multilevel(key) -> 4102 indexer = self.columns.get_loc(key) 4103 if is_integer(indexer): 4104 indexer = [indexer] File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: 'Close SMA'
In [14]:
ax = df[['Close', 'Close SMA']].plot(figsize=(14, 7), title='Close Price & its SMA')
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[14], line 1 ----> 1 ax = df[['Close', 'Close SMA']].plot(figsize=(14, 7), title='Close Price & its SMA') File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4108, in DataFrame.__getitem__(self, key) 4106 if is_iterator(key): 4107 key = list(key) -> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1] 4110 # take() does not accept boolean indexers 4111 if getattr(indexer, "dtype", None) == bool: File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['Close SMA'] not in index"
Calculate the bands as:
Lower Band = Close SMA - 2 * rolling std
Upper Band = Close SMA + 2 * rolling std
In [17]:
df['Lower Band'] = df['Close SMA'] - (2 * df['Close'].rolling(60).std())
df['Upper Band'] = df['Close SMA'] + (2 * df['Close'].rolling(60).std())
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Close SMA' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[17], line 1 ----> 1 df['Lower Band'] = df['Close SMA'] - (2 * df['Close'].rolling(60).std()) 2 df['Upper Band'] = df['Close SMA'] + (2 * df['Close'].rolling(60).std()) File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key) 4100 if self.columns.nlevels > 1: 4101 return self._getitem_multilevel(key) -> 4102 indexer = self.columns.get_loc(key) 4103 if is_integer(indexer): 4104 indexer = [indexer] File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: 'Close SMA'
In [18]:
df[['Close', 'Close SMA', 'Lower Band', 'Upper Band']].tail()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[18], line 1 ----> 1 df[['Close', 'Close SMA', 'Lower Band', 'Upper Band']].tail() File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4108, in DataFrame.__getitem__(self, key) 4106 if is_iterator(key): 4107 key = list(key) -> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1] 4110 # take() does not accept boolean indexers 4111 if getattr(indexer, "dtype", None) == bool: File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['Close SMA', 'Lower Band', 'Upper Band'] not in index"
This is how it looks like:
In [19]:
df[['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA')
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[19], line 1 ----> 1 df[['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA') File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4108, in DataFrame.__getitem__(self, key) 4106 if is_iterator(key): 4107 key = list(key) -> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1] 4110 # take() does not accept boolean indexers 4111 if getattr(indexer, "dtype", None) == bool: File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['Lower Band', 'Upper Band'] not in index"
Let's find that lower point that crosses the lower band:
In [20]:
ax = df[['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA')
ax.annotate(
"Let's find this point", xy=(pd.Timestamp("2020-03-23"), 2237),
xytext=(0.9, 0.1), textcoords='axes fraction',
arrowprops=dict(facecolor='red', shrink=0.05),
horizontalalignment='right', verticalalignment='bottom');
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[20], line 1 ----> 1 ax = df[['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA') 2 ax.annotate( 3 "Let's find this point", xy=(pd.Timestamp("2020-03-23"), 2237), 4 xytext=(0.9, 0.1), textcoords='axes fraction', 5 arrowprops=dict(facecolor='red', shrink=0.05), 6 horizontalalignment='right', verticalalignment='bottom'); File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4108, in DataFrame.__getitem__(self, key) 4106 if is_iterator(key): 4107 key = list(key) -> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1] 4110 # take() does not accept boolean indexers 4111 if getattr(indexer, "dtype", None) == bool: File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['Lower Band', 'Upper Band'] not in index"
We can quickly query all the dates that crossed the lower band (in the period 2020-03-01
to 2020-06-01
)
In [22]:
df.loc['2020-03-01': '2020-06-01'].query("Close < `Lower Band`").head()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/scope.py:231, in Scope.resolve(self, key, is_local) 230 if self.has_resolvers: --> 231 return self.resolvers[key] 233 # if we're here that means that we have no locals and we also have 234 # no resolvers File /usr/local/lib/python3.11/collections/__init__.py:1004, in ChainMap.__getitem__(self, key) 1003 pass -> 1004 return self.__missing__(key) File /usr/local/lib/python3.11/collections/__init__.py:996, in ChainMap.__missing__(self, key) 995 def __missing__(self, key): --> 996 raise KeyError(key) KeyError: 'BACKTICK_QUOTED_STRING_Lower_Band' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/scope.py:242, in Scope.resolve(self, key, is_local) 238 try: 239 # last ditch effort we look in temporaries 240 # these are created when parsing indexing expressions 241 # e.g., df[df > 0] --> 242 return self.temps[key] 243 except KeyError as err: KeyError: 'BACKTICK_QUOTED_STRING_Lower_Band' The above exception was the direct cause of the following exception: UndefinedVariableError Traceback (most recent call last) Cell In[22], line 1 ----> 1 df.loc['2020-03-01': '2020-06-01'].query("Close < `Lower Band`").head() File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4823, in DataFrame.query(self, expr, inplace, **kwargs) 4821 kwargs["level"] = kwargs.pop("level", 0) + 1 4822 kwargs["target"] = None -> 4823 res = self.eval(expr, **kwargs) 4825 try: 4826 result = self.loc[res] File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4949, in DataFrame.eval(self, expr, inplace, **kwargs) 4946 kwargs["target"] = self 4947 kwargs["resolvers"] = tuple(kwargs.get("resolvers", ())) + resolvers -> 4949 return _eval(expr, inplace=inplace, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/eval.py:336, in eval(expr, parser, engine, local_dict, global_dict, resolvers, level, target, inplace) 327 # get our (possibly passed-in) scope 328 env = ensure_scope( 329 level + 1, 330 global_dict=global_dict, (...) 333 target=target, 334 ) --> 336 parsed_expr = Expr(expr, engine=engine, parser=parser, env=env) 338 if engine == "numexpr" and ( 339 is_extension_array_dtype(parsed_expr.terms.return_type) 340 or getattr(parsed_expr.terms, "operand_types", None) is not None (...) 344 ) 345 ): 346 warnings.warn( 347 "Engine has switched to 'python' because numexpr does not support " 348 "extension array dtypes. Please set your engine to python manually.", 349 RuntimeWarning, 350 stacklevel=find_stack_level(), 351 ) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:809, in Expr.__init__(self, expr, engine, parser, env, level) 807 self.parser = parser 808 self._visitor = PARSERS[parser](self.env, self.engine, self.parser) --> 809 self.terms = self.parse() File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:828, in Expr.parse(self) 824 def parse(self): 825 """ 826 Parse an expression. 827 """ --> 828 return self._visitor.visit(self.expr) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:412, in BaseExprVisitor.visit(self, node, **kwargs) 410 method = f"visit_{type(node).__name__}" 411 visitor = getattr(self, method) --> 412 return visitor(node, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:418, in BaseExprVisitor.visit_Module(self, node, **kwargs) 416 raise SyntaxError("only a single expression is allowed") 417 expr = node.body[0] --> 418 return self.visit(expr, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:412, in BaseExprVisitor.visit(self, node, **kwargs) 410 method = f"visit_{type(node).__name__}" 411 visitor = getattr(self, method) --> 412 return visitor(node, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:421, in BaseExprVisitor.visit_Expr(self, node, **kwargs) 420 def visit_Expr(self, node, **kwargs): --> 421 return self.visit(node.value, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:412, in BaseExprVisitor.visit(self, node, **kwargs) 410 method = f"visit_{type(node).__name__}" 411 visitor = getattr(self, method) --> 412 return visitor(node, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:719, in BaseExprVisitor.visit_Compare(self, node, **kwargs) 717 op = self.translate_In(ops[0]) 718 binop = ast.BinOp(op=op, left=node.left, right=comps[0]) --> 719 return self.visit(binop) 721 # recursive case: we have a chained comparison, a CMP b CMP c, etc. 722 left = node.left File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:412, in BaseExprVisitor.visit(self, node, **kwargs) 410 method = f"visit_{type(node).__name__}" 411 visitor = getattr(self, method) --> 412 return visitor(node, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:532, in BaseExprVisitor.visit_BinOp(self, node, **kwargs) 531 def visit_BinOp(self, node, **kwargs): --> 532 op, op_class, left, right = self._maybe_transform_eq_ne(node) 533 left, right = self._maybe_downcast_constants(left, right) 534 return self._maybe_evaluate_binop(op, op_class, left, right) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:454, in BaseExprVisitor._maybe_transform_eq_ne(self, node, left, right) 452 left = self.visit(node.left, side="left") 453 if right is None: --> 454 right = self.visit(node.right, side="right") 455 op, op_class, left, right = self._rewrite_membership_op(node, left, right) 456 return op, op_class, left, right File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:412, in BaseExprVisitor.visit(self, node, **kwargs) 410 method = f"visit_{type(node).__name__}" 411 visitor = getattr(self, method) --> 412 return visitor(node, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/expr.py:545, in BaseExprVisitor.visit_Name(self, node, **kwargs) 544 def visit_Name(self, node, **kwargs) -> Term: --> 545 return self.term_type(node.id, self.env, **kwargs) File /usr/local/lib/python3.11/site-packages/pandas/core/computation/ops.py:91, in Term.__init__(self, name, env, side, encoding) 89 tname = str(name) 90 self.is_local = tname.startswith(LOCAL_TAG) or tname in DEFAULT_GLOBALS ---> 91 self._value = self._resolve_name() 92 self.encoding = encoding File /usr/local/lib/python3.11/site-packages/pandas/core/computation/ops.py:115, in Term._resolve_name(self) 110 if local_name in self.env.scope and isinstance( 111 self.env.scope[local_name], type 112 ): 113 is_local = False --> 115 res = self.env.resolve(local_name, is_local=is_local) 116 self.update(res) 118 if hasattr(res, "ndim") and res.ndim > 2: File /usr/local/lib/python3.11/site-packages/pandas/core/computation/scope.py:244, in Scope.resolve(self, key, is_local) 242 return self.temps[key] 243 except KeyError as err: --> 244 raise UndefinedVariableError(key, is_local) from err UndefinedVariableError: name 'BACKTICK_QUOTED_STRING_Lower_Band' is not defined
And finally, we can zoom in in that period:
In [23]:
df.loc['2020-01-01': '2020-06-01', ['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA | 2020-01-01 to 2020-06-01')
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[23], line 1 ----> 1 df.loc['2020-01-01': '2020-06-01', ['Close', 'Lower Band', 'Upper Band']].plot(figsize=(14, 7), title='Close Price & its SMA | 2020-01-01 to 2020-06-01') File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1184, in _LocationIndexer.__getitem__(self, key) 1182 if self._is_scalar_access(key): 1183 return self.obj._get_value(*key, takeable=self._takeable) -> 1184 return self._getitem_tuple(key) 1185 else: 1186 # we by definition only have the 0th axis 1187 axis = self.axis or 0 File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1377, in _LocIndexer._getitem_tuple(self, tup) 1374 if self._multi_take_opportunity(tup): 1375 return self._multi_take(tup) -> 1377 return self._getitem_tuple_same_dim(tup) File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1020, in _LocationIndexer._getitem_tuple_same_dim(self, tup) 1017 if com.is_null_slice(key): 1018 continue -> 1020 retval = getattr(retval, self.name)._getitem_axis(key, axis=i) 1021 # We should never have retval.ndim < self.ndim, as that should 1022 # be handled by the _getitem_lowerdim call above. 1023 assert retval.ndim == self.ndim File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1420, in _LocIndexer._getitem_axis(self, key, axis) 1417 if hasattr(key, "ndim") and key.ndim > 1: 1418 raise ValueError("Cannot index with multidimensional key") -> 1420 return self._getitem_iterable(key, axis=axis) 1422 # nested tuple slicing 1423 if is_nested_tuple(key, labels): File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1360, in _LocIndexer._getitem_iterable(self, key, axis) 1357 self._validate_key(key, axis) 1359 # A collection of keys -> 1360 keyarr, indexer = self._get_listlike_indexer(key, axis) 1361 return self.obj._reindex_with_indexers( 1362 {axis: [keyarr, indexer]}, copy=True, allow_dups=True 1363 ) File /usr/local/lib/python3.11/site-packages/pandas/core/indexing.py:1558, in _LocIndexer._get_listlike_indexer(self, key, axis) 1555 ax = self.obj._get_axis(axis) 1556 axis_name = self.obj._get_axis_name(axis) -> 1558 keyarr, indexer = ax._get_indexer_strict(key, axis_name) 1560 return keyarr, indexer File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['Lower Band', 'Upper Band'] not in index"
In [ ]: