Python Tips & Troubleshooting#

This page provides essential Python skills and troubleshooting tips for the course exercises. Bookmark this page - you’ll refer to it often!

πŸ” Getting Help in Python#

Built-in help system:

help(function_name)            # Detailed documentation
?function_name                 # In Jupyter: popup help (try it!)
function_name?                 # Alternative syntax
dir(object)                    # See all available methods

Examples:

help(print)                    # Learn about the print function
?matplotlib.pyplot.plot        # Help for plotting
dir(list)                      # See all list methods

πŸ“Š Understanding Your Data#

pandas DataFrames (Exercise 1: CTD data)#

# Explore your data:
print(df.head())               # First 5 rows
print(df.tail())               # Last 5 rows  
print(df.columns)              # Column names
print(df.dtypes)               # Data types
print(df.describe())           # Statistical summary
print(df.info())               # Memory usage and data types

# Quick checks:
print(f"Data shape: {df.shape}")  # (rows, columns)
print(f"Missing values: {df.isnull().sum()}")

xarray Datasets (Exercise 2: Bathymetry data)#

# Explore your dataset:
print(dataset)                 # Overview of entire dataset
print(dataset.coords)          # Coordinate variables (lat, lon)
print(dataset.data_vars)       # Data variables (z, temperature, etc.)
print(dataset.attrs)           # Metadata and attributes

# Data inspection:
print(dataset.z.shape)         # Dimensions of arrays
print(dataset.z.min().values, dataset.z.max().values)  # Data range
dataset.z.plot()               # Quick visualization

πŸ› Debugging & Troubleshooting#

Check Your Environment#

# What variables do I have?
print(locals().keys())         # All variable names
'my_variable' in locals()      # Does this variable exist?

# Where am I and what files are here?
import os
print("Current directory:", os.getcwd())
print("Files here:", os.listdir('.'))
print("Python path:", sys.path)

# What type is this object?
print(f"Type: {type(my_variable)}")
print(f"Value: {my_variable}")

File and Path Issues#

import os

# Check if files exist:
os.path.exists('data/myfile.nc')   # True/False
os.path.isfile('data/myfile.nc')   # True if it's a file
os.path.isdir('data/')             # True if it's a directory

# Get absolute paths:
os.path.abspath('data/myfile.nc')  # Full path to file

# List directory contents:
os.listdir('data/')            # Files in data directory

Import Problems#

# Check if a package is installed:
import importlib
importlib.util.find_spec('matplotlib')  # Returns None if not installed

# See where Python looks for packages:
import sys
print(sys.path)

# Add a directory to Python's search path:
sys.path.append('/path/to/my/modules')

Plotting Issues#

import matplotlib.pyplot as plt

# Force plots to show in Jupyter:
%matplotlib inline

# Clear problematic plots:
plt.clf()                      # Clear current figure
plt.close()                    # Close current figure  
plt.close('all')               # Close all figures

# Check figure size and DPI:
plt.rcParams['figure.figsize'] = [10, 8]
plt.rcParams['figure.dpi'] = 150

πŸ”¬ Data Quality Checks#

Always Check Your Data#

# For any dataset, check:
print(f"Shape: {data.shape}")
print(f"Data type: {data.dtype}")
print(f"Min: {data.min()}, Max: {data.max()}")
print(f"Has NaN values: {np.isnan(data).any()}")

# For oceanographic data, check ranges make sense:
print(f"Temperature range: {temp.min():.2f} to {temp.max():.2f} Β°C")
print(f"Salinity range: {salt.min():.2f} to {salt.max():.2f}")
print(f"Latitude range: {lat.min():.2f} to {lat.max():.2f}")
print(f"Longitude range: {lon.min():.2f} to {lon.max():.2f}")

Handling Missing Data#

# Check for missing values:
print(f"Missing values: {data.isnull().sum()}")

# Drop missing values:
clean_data = data.dropna()

# Fill missing values:
filled_data = data.fillna(method='forward')  # Forward fill
filled_data = data.fillna(data.mean())       # Fill with mean

πŸ†˜ Common Error Messages & Solutions#

ModuleNotFoundError: No module named 'xyz'#

Solution:

# Install missing package:
pip install xyz

# Or check if you're in the right environment:
pip list | grep xyz

FileNotFoundError: [Errno 2] No such file or directory#

Solution:

# Check your working directory and file paths:
import os
print("Current directory:", os.getcwd())
print("Looking for:", os.path.abspath('path/to/file'))
print("File exists:", os.path.exists('path/to/file'))

KeyError: 'column_name'#

Solution:

# Check what columns/keys are available:
print(df.columns)              # For pandas
print(list(dataset.keys()))    # For xarray
print(dataset.data_vars)       # For xarray data variables

ValueError: shapes not compatible#

Solution:

# Check array shapes before operations:
print(f"Array 1 shape: {array1.shape}")
print(f"Array 2 shape: {array2.shape}")

# Reshape if needed:
array1_reshaped = array1.reshape(-1, 1)

πŸ’‘ Best Practices#

Code Organization#

  • Test incrementally: Run cells one at a time

  • Use meaningful variable names: temperature not temp or t

  • Add comments: # Load CTD data helps you remember what code does

  • Keep functions short: One function, one purpose

Data Analysis Workflow#

  1. Load data β†’ Inspect data β†’ Clean data β†’ Analyze data β†’ Visualize results

  2. Always check your data ranges - do the numbers make physical sense?

  3. Save intermediate results so you don’t have to recompute everything

  4. Document your assumptions and analysis decisions

Getting Unstuck#

  1. Read the error message carefully - Python tells you exactly what’s wrong

  2. Check the basics: file paths, variable names, data types

  3. Use print() statements to see what your variables contain

  4. Try simple test cases before complex operations

  5. Ask for help if stuck for more than 20 minutes

πŸ”— Useful Resources#

Tip

Bookmark this page! You’ll refer to these tips throughout the course. The debugging section is especially helpful when you encounter errors.