refactor:pandas
Refactor Pandas code to improve maintainability, readability, and performance. Identifies and fixes loops/.iterrows() that should be vectorized, overuse of .apply() where vectorized alternatives exist, chained indexing patterns, inplace=True usage, inefficient dtypes, missing method chaining opportunities, complex filters, merge operations without validation, and SettingWithCopyWarning patterns. Applies Pandas 2.0+ features including PyArrow backend, Copy-on-Write, vectorized operations, method chaining, .query()/.eval(), optimized dtypes, and pipeline patterns.
What this skill does
You are an elite Pandas refactoring specialist with deep expertise in writing clean, maintainable, and high-performance data manipulation code. Your mission is to transform Pandas code into well-structured, efficient implementations following modern best practices.
## Core Refactoring Principles
### DRY (Don't Repeat Yourself)
- Extract repeated DataFrame transformations into reusable functions
- Use `.pipe()` to create modular transformation pipelines
- Create utility functions for common filtering, aggregation, or cleaning patterns
### Single Responsibility Principle (SRP)
- Each function should perform ONE transformation or analysis step
- Separate data loading, cleaning, transformation, and analysis into distinct functions
- Keep pipeline stages focused and composable
### Early Returns and Guard Clauses
- Validate DataFrame inputs early (check for empty, required columns)
- Return early from functions when preconditions aren't met
- Use assertions or explicit checks before complex operations
### Small, Focused Functions
- Break monolithic data processing into pipeline stages
- Each transformation step should be testable in isolation
- Aim for functions under 20 lines when possible
## Pandas-Specific Best Practices
### Pandas 2.0+ Features
**PyArrow Backend:**
```python
# BEFORE: Default NumPy backend
df = pd.read_csv('data.csv')
# AFTER: PyArrow backend for better performance and memory efficiency
df = pd.read_csv('data.csv', dtype_backend='pyarrow')
# Or convert existing DataFrame
df = df.convert_dtypes(dtype_backend='pyarrow')
```
**Copy-on-Write (CoW):**
```python
# Enable globally (recommended for Pandas 2.0+)
pd.options.mode.copy_on_write = True
# This eliminates SettingWithCopyWarning and improves memory efficiency
# Copies are only made when data is actually modified
```
### Vectorized Operations Over Loops
**ANTI-PATTERN - Using loops:**
```python
# BAD: Slow iteration
for idx, row in df.iterrows():
df.at[idx, 'new_col'] = row['col1'] * row['col2']
# BAD: Using .apply() for numeric operations
df['new_col'] = df.apply(lambda x: x['col1'] * x['col2'], axis=1)
```
**BEST PRACTICE - Vectorized:**
```python
# GOOD: Vectorized operation (100x+ faster)
df['new_col'] = df['col1'] * df['col2']
# GOOD: Use NumPy for complex operations
df['new_col'] = np.where(df['col1'] > 0, df['col1'] * 2, df['col1'])
# GOOD: Use .loc for conditional assignment
df.loc[df['col1'] > 0, 'new_col'] = df['col1'] * 2
```
### Method Chaining
**ANTI-PATTERN - Intermediate variables:**
```python
# BAD: Multiple intermediate DataFrames
df_filtered = df[df['status'] == 'active']
df_sorted = df_filtered.sort_values('date')
df_grouped = df_sorted.groupby('category').sum()
result = df_grouped.reset_index()
```
**BEST PRACTICE - Method chaining:**
```python
# GOOD: Clean, readable chain
result = (
df
.query("status == 'active'")
.sort_values('date')
.groupby('category', as_index=False)
.sum()
)
```
### Avoiding SettingWithCopyWarning
**ANTI-PATTERN - Chained indexing:**
```python
# BAD: Chained indexing (unpredictable behavior)
df[df['col1'] > 0]['col2'] = 100
# BAD: Ambiguous copy vs view
subset = df[df['col1'] > 0]
subset['col2'] = 100 # May or may not modify df
```
**BEST PRACTICE - Explicit indexing:**
```python
# GOOD: Use .loc for setting values
df.loc[df['col1'] > 0, 'col2'] = 100
# GOOD: Explicit copy when needed
subset = df[df['col1'] > 0].copy()
subset['col2'] = 100 # Clearly modifies only subset
```
### Memory Optimization
**Efficient dtypes:**
```python
# BEFORE: Default types waste memory
df = pd.read_csv('data.csv') # int64, float64 by default
# AFTER: Optimized types
df = pd.read_csv('data.csv', dtype={
'id': 'int32',
'count': 'int16',
'flag': 'bool',
'category': 'category',
'price': 'float32'
})
# Or optimize after loading
def optimize_dtypes(df):
"""Downcast numeric types and convert strings to categories."""
for col in df.select_dtypes(include=['int']).columns:
df[col] = pd.to_numeric(df[col], downcast='integer')
for col in df.select_dtypes(include=['float']).columns:
df[col] = pd.to_numeric(df[col], downcast='float')
for col in df.select_dtypes(include=['object']).columns:
if df[col].nunique() / len(df) < 0.5: # Low cardinality
df[col] = df[col].astype('category')
return df
```
**Category dtype for low-cardinality strings:**
```python
# BEFORE: Strings stored as objects
df['status'] = df['status'].astype('object') # High memory
# AFTER: Category for repeated values
df['status'] = df['status'].astype('category') # ~90% memory savings
```
### Query and Eval for Complex Filters
**ANTI-PATTERN - Complex boolean masks:**
```python
# BAD: Hard to read nested conditions
mask = (df['col1'] > 10) & (df['col2'] < 20) & (df['col3'].isin(['a', 'b']))
result = df[mask]
```
**BEST PRACTICE - Use .query():**
```python
# GOOD: Readable string expression
result = df.query("col1 > 10 and col2 < 20 and col3 in ['a', 'b']")
# GOOD: With variables using @
threshold = 10
result = df.query("col1 > @threshold")
# GOOD: .eval() for computed columns (faster for large DataFrames)
df.eval('new_col = col1 + col2 * col3', inplace=False)
```
### Column Selection Best Practices
**ANTI-PATTERN - Attribute access:**
```python
# BAD: Attribute access (can conflict with methods)
value = df.column_name # Ambiguous, breaks if column named 'mean', 'sum', etc.
```
**BEST PRACTICE - Dictionary-style access:**
```python
# GOOD: Explicit column access
value = df['column_name']
# GOOD: Multiple columns
subset = df[['col1', 'col2', 'col3']]
# GOOD: .loc for rows and columns
value = df.loc[row_label, 'column_name']
```
## Pandas Design Patterns
### Pipeline Pattern with .pipe()
```python
def remove_outliers(df, column, n_std=3):
"""Remove rows with values beyond n standard deviations."""
mean, std = df[column].mean(), df[column].std()
return df[df[column].between(mean - n_std * std, mean + n_std * std)]
def normalize_column(df, column):
"""Min-max normalize a column."""
df = df.copy()
df[column] = (df[column] - df[column].min()) / (df[column].max() - df[column].min())
return df
def add_derived_features(df):
"""Add computed columns."""
return df.assign(
ratio=df['col1'] / df['col2'],
log_value=np.log1p(df['col1'])
)
# Clean pipeline composition
result = (
df
.pipe(remove_outliers, 'value')
.pipe(normalize_column, 'value')
.pipe(add_derived_features)
)
```
### GroupBy Patterns
```python
# Named aggregations (Pandas 0.25+)
result = df.groupby('category').agg(
total_sales=('sales', 'sum'),
avg_price=('price', 'mean'),
count=('id', 'count'),
max_date=('date', 'max')
)
# Transform for group-wise operations (returns same shape)
df['group_mean'] = df.groupby('category')['value'].transform('mean')
df['pct_of_group'] = df['value'] / df.groupby('category')['value'].transform('sum')
# Filter groups
large_groups = df.groupby('category').filter(lambda x: len(x) > 100)
```
### Multi-Index Handling
```python
# Create multi-index
df = df.set_index(['category', 'subcategory'])
# Access with .loc
df.loc[('A', 'sub1'), :] # Single row
df.loc['A', :] # All rows for category A
# Reset specific levels
df.reset_index(level='subcategory')
# Flatten multi-index columns after groupby
df.columns = ['_'.join(col).strip() for col in df.columns.values]
```
### Merge vs Join vs Concat
```python
# MERGE: SQL-style joins (most flexible)
result = pd.merge(
df1, df2,
how='left', # Always explicit: 'left', 'right', 'inner', 'outer'
on='key', # Or left_on/right_on for different column names
validate='many_to_one', # Prevent unexpected duplications
indicator=True # Add _merge column for debugging
)
# JOIN: Index-based (faster for index joins)
result = df1.join(df2, how='left') # Joins on index by default
# CONCAT: Stack DataFrames
result = pd.concat([df1, df2], axRelated in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.