Contextualized Data-Wrangling Code Generation in Computational Notebooks

Junjie Huang, Daya Guo, Chenglong Wang, Jiazhen Gu, Shuai Lu, Jeevana Priya Inala, Cong Yan, Jianfeng Gao, Nan Duan, Michael R. Lyu

Introduction

Data wrangling—the process of cleaning, transforming, and structuring raw data—is a cornerstone of data science, often consuming up to 80% of a practitioner's time. While Large Language Models (LLMs) have revolutionized general code generation, they frequently stumble in the messy, non-linear environment of computational notebooks like Jupyter. This paper addresses these limitations by introducing CoCoNote, a large-scale dataset of over 58,000 wrangling examples, and DataCoder, a model designed to leverage the full multi-modal context of a notebook session.

The Challenge of Context in Notebooks

Automating data wrangling is difficult because code generation models often lack the necessary context. Standard text-to-code models face three primary hurdles: ambiguous user intent, missing information about data structures (like column names and types), and tangled dependencies within disorganized notebooks.

To bridge this gap, the authors argue that models must move beyond simple text prompts. Effective wrangling requires a "multi-modal" context that includes preceding code, markdown explanations, and—crucially—data context in the form of input and output dataframe snapshots.

Figure 1: An example of the multi-modal context used for code generation, including textual, code, and data (input/output dataframe) contexts.

Human verification underscores this necessity: a study of 50 samples revealed that while humans could only solve 34% of wrangling tasks using just code and text, their success rate jumped to 76% when provided with the data context.

Methodology: Mining and Modeling

The researchers developed a two-pronged approach to solve the context problem: CoCoMine and DataCoder.

CoCoMine: An Automated Mining Pipeline

CoCoMine is a pipeline designed to extract high-quality wrangling examples from GitHub. It uses data-flow analysis to identify "wrangling" cells—those positioned between data initialization (e.g., loading a CSV) and data utilization (e.g., training a model). To capture the ground-truth data context, the pipeline replays these notebooks in a controlled sandbox, recording the state of dataframes before and after the execution of target cells.

Figure 2: The CoCoMine pipeline for identifying wrangling cells and replaying notebooks to extract multi-modal context.

DataCoder: Dual-Encoder Architecture

Building on this data, the authors proposed DataCoder, a novel encoder-decoder model. Unlike traditional architectures that process all inputs as a single sequence, DataCoder employs dual encoders. One encoder processes unstructured text and code, while the other handles structured tabular data (flattened input/output dataframes). This separation allows the model to more effectively map relationships between specific data values and the transformation logic required in the code.

The CoCoNote Benchmark

The resulting dataset, CoCoNote, contains 58,221 examples of contextualized data wrangling. Unlike previous benchmarks such as JuiCe or DS-1000, CoCoNote provides aligned code, markdown, and actual runtime dataframe snapshots for every entry.

Key insights from the dataset analysis include:

  • The Power of the "After" Picture: The output dataframe is often more informative than the input, as it explicitly defines the transformation goal.
  • Schema over Values: While row values are necessary for filtering, column names provide the strongest signal for general transformation logic.
  • Structural Modality: Treating tables and text as distinct modalities prevents the model from being overwhelmed by the different patterns inherent in structured vs. unstructured data.

Evaluation and Results

The models were evaluated using Execution Accuracy (EA), which verifies if the generated code produces the exact expected output dataframe.

  • Performance Gains: DataCoder (based on CodeT5) achieved 42.2% EA, significantly outperforming the standard CodeT5 baseline (38.1%).
  • LLM Comparison: While GPT-4 remains the leader with 50.6% EA, smaller models showed surprising potential. When a 6.7B parameter model (DeepSeek-Coder) was fine-tuned on CoCoNote using LoRA, it achieved 50.5% EA, effectively matching GPT-4's performance at a much lower computational cost.

Conclusion

This research demonstrates that providing models with a rich, multi-modal context—specifically runtime data snapshots—is essential for automating data wrangling in computational notebooks. By providing both a massive benchmark (CoCoNote) and a specialized architecture (DataCoder), this work paves the way for more robust and context-aware AI assistants in the data science workflow. Future work will look to expand this framework beyond tabular data to include other notebook artifacts like dictionaries and visualizations.

Summary updated

BibTeX
@inproceedings{huang2024contextualized,
  author = {Junjie Huang and Daya Guo and Chenglong Wang and Jiazhen Gu and Shuai Lu and Jeevana Priya Inala and Cong Yan and Jianfeng Gao and Nan Duan and Michael R. Lyu},
  title = {{Contextualized Data-Wrangling Code Generation in Computational Notebooks}},
  booktitle = {ASE},
  year = {2024},
}
Figure