Hi, I use jupyter notebook to learn Python code,
I am writing a code
"Using the adjusted closing price, calculate the percentage daily return for JPMorgan Chase & Co. stock starting from July 14th, 2017 until December 16th, 2022."
Here is my code
cell 1:
import pandas as pd
import numpy as np
import yfinance as yf
import datetime
cell 2:
JPM_df = pd.read_csv('JPM.csv')
JPM_df['Daily Return']= JPM_df['Adj Close'].pct_change(1)*100
JPM_df
cell 3:
JPM_df['Daily Return'].replace(np.nan, 0, inplace = True)
JPM_df
The result until cell 3 is correct. (see attachment)
however, when I would like to choose date from 2017,07,14 to 2022,12,16 as below
cell 4:
JPM_df['Date'] = pd.to_datetime(JPM_df['Date'])
start_date = datetime.datetime(2017, 7, 14)
end_date = datetime.datetime(2022, 12, 16)
print(JPM_df[(JPM_df['Date'] >= start_date), (JPM_df['Date'] <= end_date)])
and it shows:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:3803, in Index.get_loc(self, key, method, tolerance)
3802 try:
-> 3803 return self._engine.get_loc(casted_key)
3804 except KeyError as err:
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/_libs/index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/_libs/index.pyx:144, in pandas._libs.index.IndexEngine.get_loc()
TypeError: '(0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool, 0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool)' is an invalid key
During handling of the above exception, another exception occurred:
InvalidIndexError Traceback (most recent call last)
Cell In [23], line 5
3 start_date = datetime.datetime(2017, 7, 14)
4 end_date = datetime.datetime(2022, 12, 16)
----> 5 print(JPM_df[(JPM_df['Date'] >= start_date), (JPM_df['Date'] <= end_date)])
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/frame.py:3804, in DataFrame.__getitem__(self, key)
3802 if self.columns.nlevels > 1:
3803 return self._getitem_multilevel(key)
-> 3804 indexer = self.columns.get_loc(key)
3805 if is_integer(indexer):
3806 indexer = [indexer]
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:3810, in Index.get_loc(self, key, method, tolerance)
3805 raise KeyError(key) from err
3806 except TypeError:
3807 # If we have a listlike key, _check_indexing_error will raise
3808 # InvalidIndexError. Otherwise we fall through and re-raise
3809 # the TypeError.
-> 3810 self._check_indexing_error(key)
3811 raise
3813 # GH#42269
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:5966, in Index._check_indexing_error(self, key)
5962 def _check_indexing_error(self, key):
5963 if not is_scalar(key):
5964 # if key is not a scalar, directly raise an error (the code below
5965 # would convert to numpy arrays and raise later any way) - GH29926
-> 5966 raise InvalidIndexError(key)
InvalidIndexError: (0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool, 0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool)
Could any one tell me which part I did wrong?
Thank you!
I am writing a code
"Using the adjusted closing price, calculate the percentage daily return for JPMorgan Chase & Co. stock starting from July 14th, 2017 until December 16th, 2022."
Here is my code
cell 1:
import pandas as pd
import numpy as np
import yfinance as yf
import datetime
cell 2:
JPM_df = pd.read_csv('JPM.csv')
JPM_df['Daily Return']= JPM_df['Adj Close'].pct_change(1)*100
JPM_df
cell 3:
JPM_df['Daily Return'].replace(np.nan, 0, inplace = True)
JPM_df
The result until cell 3 is correct. (see attachment)
however, when I would like to choose date from 2017,07,14 to 2022,12,16 as below
cell 4:
JPM_df['Date'] = pd.to_datetime(JPM_df['Date'])
start_date = datetime.datetime(2017, 7, 14)
end_date = datetime.datetime(2022, 12, 16)
print(JPM_df[(JPM_df['Date'] >= start_date), (JPM_df['Date'] <= end_date)])
and it shows:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:3803, in Index.get_loc(self, key, method, tolerance)
3802 try:
-> 3803 return self._engine.get_loc(casted_key)
3804 except KeyError as err:
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/_libs/index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/_libs/index.pyx:144, in pandas._libs.index.IndexEngine.get_loc()
TypeError: '(0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool, 0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool)' is an invalid key
During handling of the above exception, another exception occurred:
InvalidIndexError Traceback (most recent call last)
Cell In [23], line 5
3 start_date = datetime.datetime(2017, 7, 14)
4 end_date = datetime.datetime(2022, 12, 16)
----> 5 print(JPM_df[(JPM_df['Date'] >= start_date), (JPM_df['Date'] <= end_date)])
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/frame.py:3804, in DataFrame.__getitem__(self, key)
3802 if self.columns.nlevels > 1:
3803 return self._getitem_multilevel(key)
-> 3804 indexer = self.columns.get_loc(key)
3805 if is_integer(indexer):
3806 indexer = [indexer]
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:3810, in Index.get_loc(self, key, method, tolerance)
3805 raise KeyError(key) from err
3806 except TypeError:
3807 # If we have a listlike key, _check_indexing_error will raise
3808 # InvalidIndexError. Otherwise we fall through and re-raise
3809 # the TypeError.
-> 3810 self._check_indexing_error(key)
3811 raise
3813 # GH#42269
File /opt/conda/envs/saturn/lib/python3.9/site-packages/pandas/core/indexes/base.py:5966, in Index._check_indexing_error(self, key)
5962 def _check_indexing_error(self, key):
5963 if not is_scalar(key):
5964 # if key is not a scalar, directly raise an error (the code below
5965 # would convert to numpy arrays and raise later any way) - GH29926
-> 5966 raise InvalidIndexError(key)
InvalidIndexError: (0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool, 0 True
1 True
2 True
3 True
4 True
...
1363 True
1364 True
1365 True
1366 True
1367 True
Name: Date, Length: 1368, dtype: bool)
Could any one tell me which part I did wrong?
Thank you!
buran write Dec-01-2024, 02:01 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.