I am trying to read apandas dataframe imported from a JSON file.
I get the following error:
The data does not contain a column named 'totalRevenue'.
The data does not contain a column named 'future_revenue'.
Traceback (most recent call last):
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3803, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'future_revenue'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/aidanschmidt/PycharmProjects/Project/main.py", line 66, in <module>
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/frame.py", line 3805, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3805, in get_loc
raise KeyError(key) from err
KeyError: 'future_revenue'
"totalRevenue" is in the JSON and the Pandas dataframe so I'm unsure what the issue is.
I added error handling to for totalRevenue which also fails however, the error handling for future_revenue fails because it doesn't get created until later.
Here is my code :
import requests
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Replace YOUR_API_KEY with your own Alpha Vantage API key
api_key = 'YOUR API KEY'
# Get the ticker symbol from the user
ticker_symbol = input("Enter the ticker symbol of the company: ")
# Gathering all three financial statements
functions = ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']
financial_data = {}
for function in functions:
# Specify the frequency of the data
interval = 'annual' # or 'quarter'
# Send a request to the Alpha Vantage API to gather the financial data
url = f'https://www.alphavantage.co/query?function={function}&symbol={ticker_symbol}&apikey={api_key}&interval={interval}'
response = requests.get(url)
# Check the response status code and the content of the response in case the API returns an error message
if response.status_code != 200:
print("Error: API request failed with status code", response.status_code)
print(response.content)
exit()
try:
financial_data[function] = json.loads(response.text)
except json.decoder.JSONDecodeError as e:
print("Error: Failed to parse JSON data from API response")
print(e)
print(response.content)
exit()
# Load the financial data into a pandas DataFrame
financial_data = pd.DataFrame(financial_data)
# Example expense ratio (expenses as a proportion of revenue)
expense_ratio = 0.6
# Example growth rate
growth_rate = 0.03
# Check if the data contains the column 'revenue'
if 'totalRevenue' not in financial_data.columns:
print("The data does not contain a column named 'totalRevenue'.")
if 'future_revenue' not in financial_data.columns:
print("The data does not contain a column named 'future_revenue'.")
else:
# Create a new column for future expenses by assuming a constant expense ratio
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
else:
# Create a new column for future revenue by assuming a constant growth rate
financial_data['future_revenue'] = financial_data['totalRevenue'].iloc[-1] * (1 growth_rate)**(range(1, len(financial_data) 1))
# Create a new column for future expenses by assuming a constant expense ratio
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
# Assume a discount rate of 10%
discount_rate = 0.1
CodePudding user response:
If there is no totalRevenue
nor future_revenue
, you'll end up in a branch where the future_revenue
column is not created, and you can't use it to compute future_expenses
.
The final part of your program seems to simplify to
if "future_revenue" in financial_data.columns and "future_expenses" not in financial_data.columns:
financial_data["future_expenses"] = financial_data["future_revenue"] * expense_ratio
if "totalRevenue" in financial_data.columns:
financial_data["future_revenue"] = financial_data["totalRevenue"].iloc[-1] * (1 growth_rate) ** (range(1, len(financial_data) 1))
financial_data["future_expenses"] = financial_data["future_revenue"] * expense_ratio
but you'll still need some way to derive future_revenue
so you can compute future_expenses
at all; right now that can't be done if there's no totalRevenue
.