Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Clean and Prepare Price Data | Financial Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Traders

bookChallenge: Clean and Prepare Price Data

In trading, you often encounter price data with missing values. Properly cleaning and preparing this data is crucial before any analysis or modeling. You will work with a DataFrame containing daily closing prices, some of which are missing. Your task is to identify missing values, fill them using linear interpolation, and then compute basic statistics on the cleaned data. This process ensures your analysis reflects the true nature of the underlying price series, free from gaps that could distort calculations.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Hardcoded DataFrame of closing prices with missing values data = { "Date": [ "2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07" ], "Close": [ 101.5, None, 102.7, None, 104.2, 105.0, None ] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Detect missing values print("Missing values per day:") print(df.isnull()) # Fill missing values using linear interpolation df_filled = df.interpolate(method="linear") # Calculate mean and standard deviation of the cleaned price series mean_price = df_filled["Close"].mean() std_price = df_filled["Close"].std() # Print cleaned DataFrame and statistics print("\nCleaned closing prices (after interpolation):") print(df_filled) print("\nMean closing price:", mean_price) print("Standard deviation of closing price:", std_price)
copy
Task

Swipe to start coding

  • Use the provided DataFrame with missing values.
  • Identify which entries are missing.
  • Fill missing values using linear interpolation.
  • Calculate and print the mean and standard deviation of the cleaned price series.
  • Print the cleaned DataFrame.

Your solution must use pandas. Do not modify the original data structure.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how linear interpolation works in this context?

What would happen if there were missing values at the start or end of the series?

Can you show how to visualize the cleaned data?

close

bookChallenge: Clean and Prepare Price Data

Swipe to show menu

In trading, you often encounter price data with missing values. Properly cleaning and preparing this data is crucial before any analysis or modeling. You will work with a DataFrame containing daily closing prices, some of which are missing. Your task is to identify missing values, fill them using linear interpolation, and then compute basic statistics on the cleaned data. This process ensures your analysis reflects the true nature of the underlying price series, free from gaps that could distort calculations.

123456789101112131415161718192021222324252627282930313233
import pandas as pd # Hardcoded DataFrame of closing prices with missing values data = { "Date": [ "2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07" ], "Close": [ 101.5, None, 102.7, None, 104.2, 105.0, None ] } df = pd.DataFrame(data) df["Date"] = pd.to_datetime(df["Date"]) df.set_index("Date", inplace=True) # Detect missing values print("Missing values per day:") print(df.isnull()) # Fill missing values using linear interpolation df_filled = df.interpolate(method="linear") # Calculate mean and standard deviation of the cleaned price series mean_price = df_filled["Close"].mean() std_price = df_filled["Close"].std() # Print cleaned DataFrame and statistics print("\nCleaned closing prices (after interpolation):") print(df_filled) print("\nMean closing price:", mean_price) print("Standard deviation of closing price:", std_price)
copy
Task

Swipe to start coding

  • Use the provided DataFrame with missing values.
  • Identify which entries are missing.
  • Fill missing values using linear interpolation.
  • Calculate and print the mean and standard deviation of the cleaned price series.
  • Print the cleaned DataFrame.

Your solution must use pandas. Do not modify the original data structure.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7
single

single

some-alt