Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Clean and Prepare Price Data | Financial Data Analysis with Python
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
Oppgave

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.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 7
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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

Sveip for å vise menyen

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
Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 7
single

single

some-alt