$npx -y skills add Jeffallan/claude-skills --skill pandas-proPerforms pandas DataFrame operations for data analysis, manipulation, and transformation. Use when working with pandas DataFrames, data cleaning, aggregation, merging, or time series analysis. Invoke for data manipulation tasks such as joining DataFrames on multiple keys, pivotin
| 1 | # Pandas Pro |
| 2 | |
| 3 | Expert pandas developer specializing in efficient data manipulation, analysis, and transformation workflows with production-grade performance patterns. |
| 4 | |
| 5 | ## Core Workflow |
| 6 | |
| 7 | 1. **Assess data structure** — Examine dtypes, memory usage, missing values, data quality: |
| 8 | ```python |
| 9 | print(df.dtypes) |
| 10 | print(df.memory_usage(deep=True).sum() / 1e6, "MB") |
| 11 | print(df.isna().sum()) |
| 12 | print(df.describe(include="all")) |
| 13 | ``` |
| 14 | 2. **Design transformation** — Plan vectorized operations, avoid loops, identify indexing strategy |
| 15 | 3. **Implement efficiently** — Use vectorized methods, method chaining, proper indexing |
| 16 | 4. **Validate results** — Check dtypes, shapes, null counts, and row counts: |
| 17 | ```python |
| 18 | assert result.shape[0] == expected_rows, f"Row count mismatch: {result.shape[0]}" |
| 19 | assert result.isna().sum().sum() == 0, "Unexpected nulls after transform" |
| 20 | assert set(result.columns) == expected_cols |
| 21 | ``` |
| 22 | 5. **Optimize** — Profile memory, apply categorical types, use chunking if needed |
| 23 | |
| 24 | ## Reference Guide |
| 25 | |
| 26 | Load detailed guidance based on context: |
| 27 | |
| 28 | | Topic | Reference | Load When | |
| 29 | |-------|-----------|-----------| |
| 30 | | DataFrame Operations | `references/dataframe-operations.md` | Indexing, selection, filtering, sorting | |
| 31 | | Data Cleaning | `references/data-cleaning.md` | Missing values, duplicates, type conversion | |
| 32 | | Aggregation & GroupBy | `references/aggregation-groupby.md` | GroupBy, pivot, crosstab, aggregation | |
| 33 | | Merging & Joining | `references/merging-joining.md` | Merge, join, concat, combine strategies | |
| 34 | | Performance Optimization | `references/performance-optimization.md` | Memory usage, vectorization, chunking | |
| 35 | |
| 36 | ## Code Patterns |
| 37 | |
| 38 | ### Vectorized Operations (before/after) |
| 39 | |
| 40 | ```python |
| 41 | # ❌ AVOID: row-by-row iteration |
| 42 | for i, row in df.iterrows(): |
| 43 | df.at[i, 'tax'] = row['price'] * 0.2 |
| 44 | |
| 45 | # ✅ USE: vectorized assignment |
| 46 | df['tax'] = df['price'] * 0.2 |
| 47 | ``` |
| 48 | |
| 49 | ### Safe Subsetting with `.copy()` |
| 50 | |
| 51 | ```python |
| 52 | # ❌ AVOID: chained indexing triggers SettingWithCopyWarning |
| 53 | df['A']['B'] = 1 |
| 54 | |
| 55 | # ✅ USE: .loc[] with explicit copy when mutating a subset |
| 56 | subset = df.loc[df['status'] == 'active', :].copy() |
| 57 | subset['score'] = subset['score'].fillna(0) |
| 58 | ``` |
| 59 | |
| 60 | ### GroupBy Aggregation |
| 61 | |
| 62 | ```python |
| 63 | summary = ( |
| 64 | df.groupby(['region', 'category'], observed=True) |
| 65 | .agg( |
| 66 | total_sales=('revenue', 'sum'), |
| 67 | avg_price=('price', 'mean'), |
| 68 | order_count=('order_id', 'nunique'), |
| 69 | ) |
| 70 | .reset_index() |
| 71 | ) |
| 72 | ``` |
| 73 | |
| 74 | ### Merge with Validation |
| 75 | |
| 76 | ```python |
| 77 | merged = pd.merge( |
| 78 | left_df, right_df, |
| 79 | on=['customer_id', 'date'], |
| 80 | how='left', |
| 81 | validate='m:1', # asserts right key is unique |
| 82 | indicator=True, |
| 83 | ) |
| 84 | unmatched = merged[merged['_merge'] != 'both'] |
| 85 | print(f"Unmatched rows: {len(unmatched)}") |
| 86 | merged.drop(columns=['_merge'], inplace=True) |
| 87 | ``` |
| 88 | |
| 89 | ### Missing Value Handling |
| 90 | |
| 91 | ```python |
| 92 | # Forward-fill then interpolate numeric gaps |
| 93 | df['price'] = df['price'].ffill().interpolate(method='linear') |
| 94 | |
| 95 | # Fill categoricals with mode, numerics with median |
| 96 | for col in df.select_dtypes(include='object'): |
| 97 | df[col] = df[col].fillna(df[col].mode()[0]) |
| 98 | for col in df.select_dtypes(include='number'): |
| 99 | df[col] = df[col].fillna(df[col].median()) |
| 100 | ``` |
| 101 | |
| 102 | ### Time Series Resampling |
| 103 | |
| 104 | ```python |
| 105 | daily = ( |
| 106 | df.set_index('timestamp') |
| 107 | .resample('D') |
| 108 | .agg({'revenue': 'sum', 'sessions': 'count'}) |
| 109 | .fillna(0) |
| 110 | ) |
| 111 | ``` |
| 112 | |
| 113 | ### Pivot Table |
| 114 | |
| 115 | ```python |
| 116 | pivot = df.pivot_table( |
| 117 | values='revenue', |
| 118 | index='region', |
| 119 | columns='product_line', |
| 120 | aggfunc='sum', |
| 121 | fill_value=0, |
| 122 | margins=True, |
| 123 | ) |
| 124 | ``` |
| 125 | |
| 126 | ### Memory Optimization |
| 127 | |
| 128 | ```python |
| 129 | # Downcast numerics and convert low-cardinality strings to categorical |
| 130 | df['category'] = df['category'].astype('category') |
| 131 | df['count'] = pd.to_numeric(df['count'], downcast='integer') |
| 132 | df['score'] = pd.to_numeric(df['score'], downcast='float') |
| 133 | print(df.memory_usage(deep=True).sum() / 1e6, "MB after optimization") |
| 134 | ``` |
| 135 | |
| 136 | ## Constraints |
| 137 | |
| 138 | ### MUST DO |
| 139 | - Use vectorized operations instead of loops |
| 140 | - Set appropriate dtypes (categorical for low-cardinality strings) |
| 141 | - Check memory usage with `.memory_usage(deep=True)` |
| 142 | - Handle missing values explicitly (don't silently drop) |
| 143 | - Use method |