Pandas: Data Analysis & Quant Libraries
1. Pandas DataFrames & Series: Tick Data
While NumPy provides the raw mathematical engine for crunching numbers, Pandas provides the organizational structure. It is the absolute backbone of almost every Data Science pipeline in python.
In quantitative finance, the two main structures are:
pd.Series: A 1-Dimensional column of data. (e.g., A single stock's closing prices over time).pd.DataFrame: A 2-Dimensional table with labeled rows and columns (e.g., A complete Order Book or an Excel spreadsheet).
Unlike raw NumPy arrays, Pandas DataFrames have labels (indices) for rows and columns, allowing you to fetch data intuitively by column name (df['Close']) or date (df.loc['2024-01-01']).
import pandas as pdimport numpy as np # EXPLANATION: Creating a DataFrame representing an Exchange Order Bookorder_book_data = { "Symbol": ["AAPL", "AAPL", "TSLA", "TSLA", "NVDA"], "Type": ["BUY", "SELL", "BUY", "SELL", "BUY"], "Price": [170.50, 171.00, 195.20, 194.50, 850.25], "Volume": [1000, 500, 2000, 1500, 800]} df = pd.DataFrame(order_book_data) print("--- Exchange Order Book ---")print(df.to_string()) # Prints as a formatted table # EXPLANATION: Extracting a single 'Series' (Column)print("\n--- Only Prices ---")print(df["Price"]) # EXPLANATION: Calculating Total Notional Value (Price * Volume) instantly via Vectorizationdf["Notional_Value"] = df["Price"] * df["Volume"] print("\n--- Order Book with Notional Value ---")print(df.to_string())2. Data Cleaning: Nulls, Strings & Outliers
Financial data obtained from APIs (like Binance or Yahoo Finance) is almost never perfectly clean. Systems go down, APIs return numbers as strings ("$1,200.50"), and "Flash Crashes" produce extreme outlier ticks. Passing uncleaned data into a trading algorithm will crash it or result in severe financial losses.
Pandas provides built-in tools for Advanced Data Cleaning:
df.fillna(method='ffill'): Forward-fills missing data using the last known valid price. This is crucial in finance to prevent "look-ahead bias" during backtests.df['Price'].str.replace('$', '').astype(float): Converts messy currency strings into usable math floats.df = df[df['Price'] > 10.0]: Uses a Boolean Mask to filter out impossible anomalies (like a stock flash-crashing to $0.01 for one millisecond).
import pandas as pdimport numpy as np # EXPLANATION: A messy, realistic data feed from an exchangemessy_feed = { "Timestamp": ["10:00", "10:01", "10:01", "10:02", "10:03", "10:04"], "ETH_Price": ["$3,500.00", "$3,505.50", "$3,505.50", np.nan, np.nan, "$0.01"], # Strings, Duplicates, NaNs, and a Flash Crash} df = pd.DataFrame(messy_feed)print("--- 🔴 Messy Raw Feed ---")print(df) # Step 1: Drop accidental double-logs from the exchangedf_cleaned = df.drop_duplicates(subset=["Timestamp"]) # Step 2: Forward-Fill (ffill) the last known price to cover API outagesdf_cleaned["ETH_Price"] = df_cleaned["ETH_Price"].ffill() # Step 3: Convert "$3,500.00" strings into usable math Floatsdf_cleaned["ETH_Price"] = df_cleaned["ETH_Price"].str.replace('$', '').str.replace(',', '').astype(float) # Step 4: Outlier Removal - Drop the $0.01 flash crash anomalydf_cleaned = df_cleaned[df_cleaned["ETH_Price"] > 100.0] print("\n--- 🟢 Cleaned Feed Ready for Algorithm ---")print(df_cleaned)3. Time Series Challenges: Timezones & Resampling
Quantitative algorithmic trading lives entirely on Time Series Data (data indexed by time). However, time introduces massive complications:
- Timezones: A Japanese exchange operates in JST, while a New York algorithm expects EST. Pandas can force standardization using
df.tz_localize('UTC'). - Irregular Ticks: Trades happen randomly. You might get 50 trades in one second, and 0 in the next. To build features for machine learning, you must Resample irregular ticks into fixed intervals (e.g., 1-Minute OHLC bars) using
df.resample('1min').ohlc().
import pandas as pdimport numpy as np # EXPLANATION: Raw irregular "Tick" data (trades happening at arbitrary milliseconds)ticks = pd.DataFrame({ "Price": [100.5, 100.6, 100.4, 101.0, 101.2], "Volume": [10, 5, 20, 100, 50]}, index=pd.to_datetime([ "2024-03-01 09:30:00", "2024-03-01 09:30:15", "2024-03-01 09:30:45", "2024-03-01 09:31:05", "2024-03-01 09:31:40"])) print("--- 🔴 IRREGULAR TICK DATA ---")print(ticks) # Step 1: Resample irregular ticks into fixed 1-Minute chunks# We take the Mean (average) price, and Sum the volume per minute.minute_bars = ticks.resample('1min').agg({'Price': 'mean', 'Volume': 'sum'}) print("\n--- 🟢 CLEAN 1-MINUTE ALGO BARS ---")print(minute_bars)4. Grouping, Merging & Pivoting
In Algorithmic Trading and Banking, you rarely analyze data in isolation. You need to combine it (Merge) and summarize it (Groupby).
df.groupby('Sector').mean(): Calculates the average metrics split by market sector (e.g., Tech vs Finance).pd.merge(df1, df2, on='ID'): Joins two different databases together instantly, similar to a SQLJOIN. (e.g., Merging a user database with their daily trade logs).df.pivot_table(): Restructures data into a multi-index grid for risk analysis.
import pandas as pd # EXPLANATION: Analyzing a Trading Firm's Daily Strategy PnLtrade_logs = pd.DataFrame({ "Date": ["Mon", "Mon", "Tue", "Tue", "Wed", "Wed"], "Strategy": ["Mean-Reversion", "Momentum", "Mean-Reversion", "Momentum", "Mean-Reversion", "Momentum"], "PnL": [1500, -200, 1800, 450, -500, 3000]}) print("--- Raw Trade Logs ---")print(trade_logs) # 1. Groupby Strategy to see which one is actually profitable!# We group by the 'Strategy' column and sum the 'PnL' column.strategy_performance = trade_logs.groupby('Strategy')['PnL'].sum() print("\n--- Total Performance by Strategy ---")print(strategy_performance) # 2. Find the highest single day for each strategystrategy_max = trade_logs.groupby('Strategy')['PnL'].max()print("\n--- Best Single Day for Each ---")print(strategy_max)5. Advanced Quant Libraries (Overview)
While Pandas is the industry standard, quantitative trading at hedge funds often requires specialized or faster tools. You must know these exist:
1. Polars (The Faster Pandas)
Pandas was written in C and Python, operating on a single CPU core. Polars is written in Rust and is multi-threaded. If you are processing 100GB of High-Frequency Tick Data, Polars can be 10x-50x faster by utilizing all cores on your machine simultaneously.
2. TA-Lib (Technical Analysis)
TA-Lib is a C-library widely used in Python to calculate over 150 technical indicators instantly. Instead of writing complex Pandas code to calculate a 14-day RSI (Relative Strength Index) or a MACD crossover, you simply call talib.RSI(close_prices, timeperiod=14).
3. QuantLib (Derivatives Pricing)
The gold standard for Options Pricing (Black-Scholes), Fixed Income (Bonds), and Yield Curve modeling. It is highly complex, math-heavy C++ architecture wrapped in Python.
Note: Because these are compiled C/Rust extensions, they do not currently run native browser REPLs. In the upcoming REPLs, we will simulate TA-Lib's behavior manually using Pandas!
import pandas as pdimport numpy as np # EXPLANATION: Simulating TA-Lib using pure Pandas!# Let's manually calculate a 3-Day Moving Average prices = pd.Series([100, 102, 101, 105, 108, 107, 110])print("--- Daily Closing Prices ---")print(prices.tolist()) # Pandas provides '.rolling()' to look back 'n' days# If we had TA-Lib installed, this would just be: talib.SMA(prices, 3)moving_average_3d = prices.rolling(window=3).mean() print("\n--- 3-Day Simple Moving Average (SMA) ---")print(moving_average_3d.tolist())# Output shows NaN for the first 2 days since we need 3 days of history!Practice Questions
Question 1
Why should you use Forward-Fill (`ffill`) instead of ordinary Mean-Fill when handling missing price ticks (`NaN`) in financial data?
Question 2
Which high-speed library is written in Rust, operates heavily on multiple cores simultaneously, and is replacing Pandas for ultra-high-frequency tick datasets exceeding 50GB?
Question 3
What does `df.groupby('Sector')['PnL'].sum()` achieve in Pandas?
Question 4
To combine two separate DataFrames (e.g. a static `Users` table and a live `Trade_Log` table) using a shared attribute like 'Account_ID', which method is used?