import pandas as pd
import numpy as np
import yfinance as yf
from pathlib import Path
import logging
import os
from datetime import datetime
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import pandas_ta as ta # Install using: pip install pandas_ta
from tqdm import tqdm # For progress bars
Setup Logging
def setup_logging(log_file='logs/data_processing.log'):
"""
Configure logging settings to log INFO and higher-level messages to a file.
"""
# Create the logs directory if it doesn't exist
log_path = Path(log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
code
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(message)s',
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler() # Also output to the console
]
)
def create_directories(directories):
"""
Create directories for storing processed data.
"""
for directory in directories:
Path(directory).mkdir(parents=True, exist_ok=True)
logging.info(f"Directory created or already exists: {directory}")
def fetch_macro_data():
"""
Fetch macroeconomic indicators from FRED and save to CSV files.
"""
logger.info("Fetching macroeconomic data from FRED.")
start_date = '2010-01-01'
end_date = datetime.today().strftime('%Y-%m-%d')
code
# Define the indicators and their FRED codes
macro_indicators = {
'FEDFUNDS': 'Federal_Funds_Rate',
'GDP': 'GDP',
'CPIAUCSL': 'CPI' # Consumer Price Index for All Urban Consumers
}
for code, name in macro_indicators.items():
try:
logger.info(f"Fetching {name}")
data = web.DataReader(code, 'fred', start_date, end_date)
data.reset_index(inplace=True)
file_path = MACRO_DATA_PATH / f"{name}.csv"
data.to_csv(file_path, index=False)
logger.info(f"Saved {name} data to {file_path}")
except Exception as e:
logger.error(f"Error fetching {name}: {e}")
Assistant
Function to Read and Process Price Data
def process_price_data():
"""
Read raw price data CSVs, clean data, calculate technical indicators,
and save processed data.
"""
logger.info("Processing price data and calculating technical indicators.")
timeframes = ['1d', '1wk', '1mo']
data_types = ['in_sample', 'out_sample']
code
for data_type in data_types:
for timeframe in timeframes:
raw_data_dir = RAW_DATA_PATH / data_type / timeframe
processed_data_dir = TECHNICAL_DATA_PATH / data_type / timeframe
processed_data_dir.mkdir(parents=True, exist_ok=True)
csv_files = list(raw_data_dir.glob('*.csv'))
logger.info(f"Processing {len(csv_files)} files in {raw_data_dir}")
for csv_file in tqdm(csv_files, desc=f"{data_type}/{timeframe}"):
try:
ticker = csv_file.stem
df = pd.read_csv(csv_file, parse_dates=['Date'])
df.sort_values('Date', inplace=True)
# Handle missing values
df.fillna(method='ffill', inplace=True)
df.dropna(inplace=True)
# Verify adjusted close prices
if 'Adj Close' not in df.columns:
df['Adj Close'] = df['Close']
else:
# Adjust Close should already account for splits/dividends
pass # Additional verification can be added if necessary
# Calculate Technical Indicators
df.set_index('Date', inplace=True)
# Moving Averages
df['SMA_20'] = df['Adj Close'].rolling(window=20).mean()
df['SMA_50'] = df['Adj Close'].rolling(window=50).mean()
df['EMA_20'] = df['Adj Close'].ewm(span=20, adjust=False).mean()
# Relative Strength Index (RSI)
df['RSI_14'] = ta.rsi(df['Adj Close'], length=14)
# Moving Average Convergence Divergence (MACD)
macd = ta.macd(df['Adj Close'])
df['MACD'] = macd['MACD_12_26_9']
df['MACD_signal'] = macd['MACDs_12_26_9']
# Bollinger Bands
bb = ta.bbands(df['Adj Close'], length=20, std=2)
df['BB_upper'] = bb['BBU_20_2.0']
df['BB_middle'] = bb['BBM_20_2.0']
df['BB_lower'] = bb['BBL_20_2.0']
# Average True Range (ATR)
df['ATR_14'] = ta.atr(df['High'], df['Low'], df['Close'], length=14)
# Reset index to save Date as a column
df.reset_index(inplace=True)
# Save processed data
output_file = processed_data_dir / f"{ticker}.csv"
df.to_csv(output_file, index=False)
logger.info(f"Processed data saved for {ticker} at {output_file}")
except Exception as e:
logger.error(f"Error processing {csv_file}: {e}")
Function to Validate Data
def validate_data():
"""
Perform data validation checks to ensure data integrity.
"""
logger.info("Validating processed data.")
timeframes = ['1d', '1wk', '1mo']
data_types = ['in_sample', 'out_sample']
code
for data_type in data_types:
for timeframe in timeframes:
processed_data_dir = TECHNICAL_DATA_PATH / data_type / timeframe
csv_files = list(processed_data_dir.glob('*.csv'))
for csv_file in tqdm(csv_files, desc=f"Validating {data_type}/{timeframe}"):
try:
df = pd.read_csv(csv_file, parse_dates=['Date'])
ticker = csv_file.stem
# Check for anomalies
if df.isnull().values.any():
logger.warning(f"Missing values detected in {ticker} data.")
if df.duplicated(subset=['Date']).any():
logger.warning(f"Duplicate dates found in {ticker} data.")
# Additional validation checks can be added here
except Exception as e:
logger.error(f"Error validating {csv_file}: {e}")
Main Function to Execute All Steps
def main():
"""
Main function to execute data collection, processing, and validation.
"""
# Step 1: Fetch Macroeconomic Data
fetch_macro_data()
code
# Step 2: Process Price Data and Calculate Technical Indicators
process_price_data()
# Step 3: Validate Processed Data
validate_data()
Execute the Main Function
if name == "main":
main()
"""
Based on the provided log, your data processing pipeline encountered several issues that could impact the integrity and completeness of your processed datasets. Here's a detailed analysis of the potential errors and warnings observed in the log:
1. Critical Errors During Data Processing
a. Error Processing DUSL.csv
Log Entry:
2024-09-19 08:49:40,830:ERROR:Error processing data/raw_data/in_sample/1mo/DUSL.csv: unsupported operand type(s) for -: 'float' and 'NoneType'
Explanation:
Issue: The error message unsupported operand type(s) for -: 'float' and 'NoneType' indicates that the code attempted to perform a subtraction operation between a float and a NoneType object.
Possible Cause: This typically occurs when a value expected to be a numerical (float) is missing or undefined (None). In the context of processing DUSL.csv, it's likely that one of the columns or data points intended for numerical operations contains missing or None values.
Impact: The processing of DUSL.csv failed, meaning that any technical indicators or processed data derived from this file are unavailable or incomplete. This could affect downstream analyses or models that rely on this specific dataset.
b. Error Processing XLC.csv
Log Entry:
2024-09-19 08:49:40,907:ERROR:Error processing data/raw_data/in_sample/1mo/XLC.csv: 'NoneType' object is not subscriptable
Explanation:
Issue: The error 'NoneType' object is not subscriptable suggests that the code attempted to access elements (using indexing) of a variable that is None.
Possible Cause: During the processing of XLC.csv, a function or operation expected to return a data structure (like a list, tuple, or dictionary) returned None instead. When the code tried to access elements of this None object (e.g., none_object[0]), it resulted in the error.
Impact: Similar to the previous error, the processing of XLC.csv was interrupted, leading to missing or incomplete processed data for this file. This omission can have repercussions on any analyses or models that incorporate XLC.csv data.
2. Warnings About Missing Values in Validated Data
After processing, the pipeline performs a validation step where it checks for missing values in the processed datasets. The log records numerous warnings indicating the presence of missing values across various data files.
a. General Pattern of Warnings
Log Entries:
WARNING:Missing values detected in FNGU data.
WARNING:Missing values detected in TQQQ data.
WARNING:Missing values detected in SDP data.
...
Explanation:
Issue: The consistent WARNING:Missing values detected in <Symbol> data. messages indicate that several datasets contain missing (NaN or None) values.
Possible Causes:
Data Quality Issues: The raw CSV files might have incomplete records, missing entries, or formatting issues that led to missing data during processing.
Processing Errors: Earlier processing steps, such as feature calculations or data transformations, might have inadvertently introduced NaN or None values if certain conditions weren't handled properly (e.g., division by zero, invalid operations).
Incomplete Data Retrieval: If data fetching from external sources (like FRED for macroeconomic data) was interrupted or incomplete, it could result in missing entries in the processed datasets.
Impact:
Analytical Accuracy: Missing values can skew statistical analyses, machine learning model training, and other data-driven processes, leading to inaccurate or unreliable results.
Downstream Processes: Subsequent steps that rely on complete datasets (e.g., data visualization, reporting, further data transformations) might fail or produce incomplete outputs.
Out-of-Sample Data (1d, 1wk, 1mo): Similar range of symbols showing missing values.
4. Recommendations to Address the Issues
a. Addressing Critical Errors
Debug the Processing Code:
For DUSL.csv: Investigate the specific processing function or formula that involves subtraction operations. Ensure that all operands are validated for None or NaN values before performing arithmetic operations.
For XLC.csv: Examine where the NoneType error originates. Ensure that all data structures expected to contain data are properly initialized and populated before subscripting.
Implement Robust Error Handling:
Incorporate try-except blocks around critical operations to gracefully handle unexpected None or NaN values.
Log more detailed error messages or stack traces to pinpoint the exact location and cause of failures.
Data Validation Prior to Processing:
Perform preliminary checks on raw data to ensure completeness and correctness before initiating processing routines.
Fill or impute missing values where appropriate, or exclude incomplete records based on the use case.
b. Resolving Missing Values in Validated Data
Data Cleaning:
Imputation: Use statistical methods (e.g., mean, median, mode) or model-based techniques to estimate and fill missing values.
Removal: Exclude records with missing values if imputation isn't feasible or could introduce significant bias.
Enhance Data Processing Pipelines:
Ensure that all data transformations handle missing data gracefully. For instance, using functions that can operate on NaN values without breaking (e.g., pandas' functions which inherently handle NaN).
Validate intermediate outputs to catch and address missing data early in the pipeline.
Improve Data Quality at Source:
If missing values stem from external data sources (like FRED), consider adding checks to verify data completeness after retrieval.
Automate alerts or flags when fetched data has unexpected gaps or anomalies.
Documentation and Logging Enhancements:
Clearly document how missing data is handled within the pipeline to ensure transparency.
Enhance logging to include counts or proportions of missing values for each dataset, aiding in comprehensive monitoring and troubleshooting.
5. Potential Long-Term Solutions
Automated Testing: Implement unit and integration tests to verify that data processing functions handle edge cases, including missing or malformed data.
Monitoring and Alerts: Set up monitoring tools that can alert the team when critical errors or high levels of missing data are detected during processing.
Data Integrity Checks: Regularly audit processed data to ensure consistency, completeness, and correctness, adapting the pipeline as data sources or requirements evolve.
By addressing the identified errors and warnings, you can enhance the reliability and quality of your data processing pipeline, ensuring more accurate and dependable outcomes for your analyses and models.
Dear User,
Thank you for providing detailed information about your data processing pipeline, the issues encountered, and the analysis of potential errors and warnings. I'll help you diagnose these issues step by step and suggest solutions to enhance the reliability and quality of your pipeline.
Summary of Issues
1. Critical Errors During Data Processing
Error Processing DUSL.csv
Error Message:
code
ERROR:Error processing data/raw_data/in_sample/1mo/DUSL.csv: unsupported operand type(s) for -: 'float' and 'NoneType'
Explanation:
An arithmetic operation is being attempted between a float and a NoneType.
Likely occurs during calculations where missing values (None or NaN) are involved.
Error Processing XLC.csv
Error Message:
code
ERROR:Error processing data/raw_data/in_sample/1mo/XLC.csv: 'NoneType' object is not subscriptable
Explanation:
Attempting to index or access elements of a NoneType object.
Occurs when a function expected to return data returns None.
2. Warnings About Missing Values in Validated Data
Multiple Datasets with Missing Values
Warnings indicate missing values detected in many processed data files.
Affects both in-sample and out-of-sample data across different timeframes.
Diagnosis and Solutions
1. Error Processing DUSL.csv
Diagnosis:
The error indicates that an operation involves a float and NoneType, suggesting that there's a missing value in one of the operands during a calculation.
Possible Causes:
Missing or NaN values in the data not handled properly before calculations.
Data columns expected to have numerical values contain None or NaN.
Solution:
Check for Missing Values Before Calculations:
Ensure that all columns involved in calculations do not contain missing values.
python
# Handle missing values
df.fillna(method='ffill', inplace=True) # Forward fill
df.dropna(inplace=True) # Drop remaining NaNs
Verify Data Columns:
Make sure that critical columns like 'Adj Close' are populated.
python
if 'Adj Close' not in df.columns or df['Adj Close'].isnull().any():
df['Adj Close'] = df['Close']
Update Calculations to Handle Missing Values:
Use functions that can handle missing data or check for None before operations.
python
# Example for safe subtraction
result = value1 - value2 if value1 is not None and value2 is not None else None
2. Error Processing XLC.csv
Diagnosis:
The error 'NoneType' object is not subscriptable suggests that a function returned None, and the code attempted to subscript it.
Possible Causes:
Technical indicator functions returning None due to insufficient data or errors.
Misuse of functions leading to unexpected None returns.
Solution:
Check Return Values of Functions:
Verify that the functions used for calculating technical indicators return valid data.
python
macd = ta.macd(df['Adj Close'])
if macd is not None:
df['MACD'] = macd['MACD_12_26_9']
df['MACD_signal'] = macd['MACDs_12_26_9']
else:
logger.error(f"MACD calculation failed for {ticker}")
df['MACD'] = np.nan
df['MACD_signal'] = np.nan
Ensure Sufficient Data for Calculations:
Some indicators require a minimum number of data points.
python
if len(df) < 50:
logger.error(f"Not enough data to calculate indicators for {ticker}")
continue # Skip to the next file
Handle Functions Returning None:
Update your code to handle cases where functions might return None.
3. Warnings About Missing Values
Diagnosis:
Missing values in processed data can result from:
Initial periods in rolling calculations (e.g., moving averages) producing NaN.
Missing data in the raw dataset not properly handled.
Errors in calculations leading to NaN results.
Solution:
Handle Initial NaN Values:
Recognize that rolling calculations produce NaN for the initial periods.
python
# After calculations, drop rows with NaN in indicator columns
df.dropna(subset=['SMA_20', 'SMA_50', 'EMA_20', 'RSI_14', 'MACD', 'MACD_signal', 'BB_upper', 'BB_middle', 'BB_lower', 'ATR_14'], inplace=True)
Ensure Data Quality Before Processing:
Clean the raw data to address missing values.
python
# Inspect for missing values
if df.isnull().values.any():
logger.warning(f"Missing values detected in {ticker} raw data.")
df.fillna(method='ffill', inplace=True)
df.dropna(inplace=True)
Adjust Calculations with Parameters to Handle Missing Data:
Use parameters in technical indicator functions to handle missing values gracefully.
python
# Example with fillna parameter
df['RSI_14'] = ta.rsi(df['Adj Close'], length=14, fillna=True)
Review Data Alignment and Merging:
Ensure that data merging or alignment does not introduce missing values.
General Recommendations
1. Implement Robust Error Handling
Use Try-Except Blocks:
Wrap critical sections in try-except blocks to catch and log exceptions without stopping the entire process.
python
try:
# Critical code
except Exception as e:
logger.error(f"Error processing {ticker}: {e}")
continue # Proceed to the next ticker
2. Enhance Logging
Provide Detailed Error Messages:
Include variable values and context in error messages.
python
logger.error(f"Error processing {ticker} at {step}: {e}")
Log Warnings for Suspicious Data:
Flag any data anomalies for further inspection.
3. Validate Data at Each Step
After Data Loading:
Check for duplicates, missing values, and correct data types.
After Each Processing Step:
Confirm that the expected transformations have occurred correctly.
4. Update Dependencies
Ensure Libraries Are Up-to-Date:
Use the latest versions of libraries like pandas_ta to benefit from bug fixes and improvements.
bash
pip install --upgrade pandas_ta
5. Test with a Subset of Data
Isolate Problematic Files:
Run the processing code on the files that caused errors individually to debug.
Use Debugging Tools:
Utilize debuggers or insert print statements to trace variable values.
Revised Code Snippets
Handling Technical Indicator Calculations Safely:
python
try:
# Moving Averages
df['SMA_20'] = df['Adj Close'].rolling(window=20).mean()
df['SMA_50'] = df['Adj Close'].rolling(window=50).mean()
df['EMA_20'] = df['Adj Close'].ewm(span=20, adjust=False).mean()
# Relative Strength Index (RSI)
df['RSI_14'] = ta.rsi(df['Adj Close'], length=14, fillna=True)
# MACD
macd = ta.macd(df['Adj Close'], fast=12, slow=26, signal=9, fillna=True)
if macd is not None and not macd.isnull().any().any():
df['MACD'] = macd['MACD_12_26_9']
df['MACD_signal'] = macd['MACDs_12_26_9']
else:
df['MACD'] = np.nan
df['MACD_signal'] = np.nan
# Bollinger Bands
bb = ta.bbands(df['Adj Close'], length=20, std=2, fillna=True)
if bb is not None and not bb.isnull().any().any():
df['BB_upper'] = bb['BBU_20_2.0']
df['BB_middle'] = bb['BBM_20_2.0']
df['BB_lower'] = bb['BBL_20_2.0']
else:
df['BB_upper'] = np.nan
df['BB_middle'] = np.nan
df['BB_lower'] = np.nan
# Average True Range (ATR)
df['ATR_14'] = ta.atr(df['High'], df['Low'], df['Close'], length=14, fillna=True)
except Exception as e:
logger.error(f"Error calculating technical indicators for {ticker}: {e}")
continue # Skip to the next ticker
Ensuring Sufficient Data for Calculations:
python
# Minimum data length required for longest window
min_data_length = 50 # For SMA_50
if len(df) >= min_data_length:
# Proceed with calculations
else:
logger.warning(f"Not enough data for {ticker}. Required: {min_data_length}, Available: {len(df)}")
continue # Skip to the next ticker
Next Steps
Implement the Suggested Changes:
Update your code with the modifications mentioned above.
Test Thoroughly:
Run your data processing pipeline on the problematic files (DUSL.csv, XLC.csv) to verify that the errors are resolved.
Monitor Logs:
Keep an eye on the logs for any new errors or warnings.
Handle Remaining Missing Values:
Decide on a strategy for handling any remaining missing values in your datasets, such as imputation or exclusion.
Optimize Performance:
If processing time becomes an issue, consider optimizing code or processing in parallel.
Conclusion
By addressing the identified issues with proper error handling, data validation, and careful handling of technical indicator calculations, you can enhance the integrity and completeness of your processed datasets. This will lead to more accurate analyses and reliable models.
Feel free to ask if you need further assistance or clarification on any of these points. I'm here to help!