Access to accurate and timely earnings data is essential for investors and analysts who want to understand a company’s financial performance. We offer an API service that provides users with valuable tools for retrieving earnings data. Through our Python SDK, users can easily extract and analyze historical earnings data for specific companies. In this guide, we will focus on retrieving Apple Inc.’s (AAPL) historical earnings data and conducting further analysis using Benzinga’s Earnings API.
Earnings API by Benzinga
The Earnings API provides critical information regarding company earnings, including earnings per share (EPS), revenue, and earnings surprises. This data is a key component in understanding how well a company has performed relative to analyst expectations, and it can help investors make informed decisions based on financial trends.
The Benzinga Earnings API can be accessed through their Python SDK, allowing for seamless integration into analytical workflows. In this article, we will walk through the process of importing packages, authenticating the API key, extracting earnings data for AAPL, and performing further analysis on the extracted data.
Importing Packages
To get started, we will import the necessary packages. We will use Benzinga’s Python SDK to access the API, Pandas to format and manipulate the data, and Matplotlib to visualize the earnings data.
from benzinga import financial_data # Benzinga's Python SDK
import pandas as pd # For data formatting
import matplotlib.pyplot as plt # For data visualization
Here, the financial_data module from Benzinga’s Python SDK provides easy access to financial data APIs, including the Earnings API. Pandas is used to manage the data as a DataFrame, which makes it easier to manipulate and analyze. Additionally, Matplotlib is imported to help visualize the extracted data.
Authenticating API Key
Before you can access any data through the Benzinga API, you need to authenticate your API key. This key acts as your access token, allowing you to retrieve data.
api_key = "YOUR_API_KEY"
fin = financial_data.Benzinga(api_key)
Replace “YOUR_API_KEY” with your actual Benzinga API key, which you can obtain by signing up for Benzinga’s API service. This step is essential as it enables you to interact with Benzinga’s financial data API and retrieve earnings data.
Extracting Earnings Data of AAPL
Now that we’ve authenticated the API key, let’s move on to extracting the earnings data for Apple Inc. (AAPL). The code below requests the earnings data from Benzinga’s Earnings API and formats it as a Pandas DataFrame for further analysis.
aapl_earnings = pd.DataFrame(fin.earnings(company_tickers='AAPL')['earnings']).set_index('date')
aapl_earnings = aapl_earnings.iloc[::-1]
aapl_earnings.head()
This code first calls the earnings() method from Benzinga’s API using the AAPL ticker symbol to retrieve Apple’s earnings data. The JSON response from the API is then converted into a Pandas DataFrame. We set the ‘date’ column as the DataFrame’s index and reverse the order of the data to ensure it is arranged chronologically (from oldest to newest).
Here’s a sample output of the DataFrame:
The DataFrame contains important earnings-related metrics such as EPS (Earnings Per Share), revenue, and EPS surprises, allowing us to perform meaningful analysis.
Further Data Analysis
With the earnings data extracted, we can now perform further analysis to gain insights into Apple’s financial performance. We will look at revenue growth over time, visualize EPS surprises, and calculate the average EPS and revenue.
1. Revenue Growth Over Time
Revenue growth is an important metric to evaluate a company’s ability to expand its business. Let’s calculate Apple’s revenue growth from year to year and store it in a separate DataFrame.
aapl_earnings_revenue = aapl_earnings[aapl_earnings['revenue'] != '']['revenue']
aapl_earnings_revenue = aapl_earnings_revenue.astype(float)
aapl_earnings_revenue_growth = aapl_earnings_revenue.pct_change() * 100
revenue_df = pd.DataFrame(columns=['revenue', 'revenue_growth'])
revenue_df['revenue'] = aapl_earnings_revenue
revenue_df['revenue_growth'] = aapl_earnings_revenue_growth
revenue_df.tail()
Output:
Code Explanation:
Here, we extract the revenue data, convert it to a float type (since financial data is numeric), and calculate the percentage change in revenue using the pct_change() method. The revenue growth is expressed as a percentage. The results are stored in a new DataFrame, allowing us to track Apple’s revenue growth over time.
2. EPS Surprises Over Time
Next, we’ll visualize Apple’s EPS (Earnings Per Share) surprises. EPS surprises indicate how much the reported EPS deviated from analysts’ expectations, and they can provide insight into the company’s financial performance.
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (12,6)
aapl_earnings_eps_surprise = aapl_earnings[aapl_earnings.eps_surprise != '']['eps_surprise']
aapl_earnings_eps_surprise = aapl_earnings_eps_surprise.astype(float)
aapl_earnings_eps_surprise.plot(kind='bar', title='Apple EPS Surprises Over Time')
plt.ylabel('EPS Surprise')
plt.show()
Output:
Code Explanation:
We filter out empty values from the eps_surprise column and convert the data to float. Using Matplotlib, we plot the EPS surprises in a bar chart, providing a visual representation of how Apple’s reported earnings have compared to market expectations over time.
3. Average EPS and Revenue
Lastly, we calculate Apple’s average EPS and revenue over the available time period. This provides a broader overview of Apple’s historical performance.
aapl_earnings_eps = aapl_earnings[aapl_earnings.eps != '']['eps'].astype(float)
average_eps = aapl_earnings_eps.mean()
average_revenue = aapl_earnings_revenue.mean()
print(f'Average EPS: {average_eps:.2f}')
print(f'Average Revenue: {average_revenue:.2f}')
Output:
Code Explanation:
By calculating the mean of the EPS and revenue columns, we obtain the average EPS and average revenue for the analyzed period. This gives us a snapshot of Apple’s overall financial performance across multiple earnings reports.
Conclusion
In this article, we demonstrated how to use Benzinga’s Earnings API to extract and analyze historical earnings data for Apple Inc. Using Benzinga’s Python SDK, we fetched detailed earnings data and performed further analysis, including calculating revenue growth, visualizing EPS surprises, and computing average EPS and revenue.
Benzinga’s API service, combined with its Python SDK, makes it easy for investors and analysts to access and analyze financial data. Whether you’re looking to track earnings trends, analyze performance, or develop trading strategies, Benzinga’s Earnings API provides the tools you need for insightful analysis.