How to Extract Historical Data from Yfinance using Python
Extracting historical data from Yahoo Finance (yfinance) is a popular way for traders and investors to analyze the performance of stocks, commodities, and other financial assets over time. This article will guide you through the process of extracting historical data from yfinance using Python.

Step 1: Install yfinance
Firstly, you need to install yfinance. You can install yfinance using pip, a package installer for Python. Open your terminal and type the following command to install yfinance.
pip install yfinance
Step 2: Import Required Libraries
After installing yfinance, you need to import the required libraries in your Python script. You can use the following libraries:
import yfinance as yf
import pandas as pdStep 3: Create a Ticker Object
The next step is to create a Ticker object using yfinance. You can create a Ticker object for any financial asset by passing the ticker symbol as a parameter to the Ticker function.
ticker = yf.Ticker('AAPL')
In the above example, we created a Ticker object for Apple Inc. with the ticker symbol AAPL.
Step 4: Extract Historical Data
After creating a Ticker object, you can extract historical data using the history function. The history function returns a Pandas DataFrame containing historical data for the specified asset. You can specify the period parameter to determine the duration of historical data you want to retrieve.
historical_data = ticker.history(period='max')In the above example, we set the period parameter to ‘max’ to retrieve historical data for the maximum available duration.
Step 5: Clean and Export Data
After retrieving the historical data, you can clean and export the data as per your requirements. You can use the Pandas library to clean and manipulate the data.
historical_data.reset_index(inplace=True)
historical_data.to_csv('AAPL.csv', index=False)In the above example, we reset the index of the DataFrame and exported it as a CSV file named AAPL.csv.
Step 6 : Code exemple
from cryptocmd import CmcScraper
import pandas as pd
import yfinance as yf
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Using the Ticker function to create a ticker object.
# ticker symbol of tesla is TSLA
df = yf.Ticker('AF.PA')
# history function helps to extract stock information.
# setting period parameter to max to get information for the maximum amount of time.
df2 = df.history(period='730D',interval='1wk')
# Resetting the index
df2.reset_index(inplace=True)
# display the first five rows
df2.head()
# initialise scraper without time interval for max historical data
#del df2['Date']
df2['close'] = pd.to_numeric(df2['Close'])
df2['high'] = pd.to_numeric(df2['High'])
df2['low'] = pd.to_numeric(df2['Low'])
df2['open'] = pd.to_numeric(df2['Open'])
df2['volume'] = pd.to_numeric(df2['Volume'])
del df2['Open']
del df2['Close']
del df2['High']
del df2['Low']
del df2['Volume']
# Pandas dataFrame for the same data
df2.to_csv('AFW.csv',index=False)In this article, we have discussed how to extract historical data from yfinance using Python. By following the above steps, you can retrieve historical data for any financial asset and use it for further analysis and research. Remember to always check the data before using it for making investment decisions.






