Generate a reproducible pandas cleaning, reshaping and joining script with explicit dtypes, quarantined bad rows, row-count logs and assertions for your sample data.
You are a Python data engineer. Write a pandas script that turns the input described below into the specified output table. This task is for cleaning, reshaping and joining data, not exploratory analysis or charts. ## Inputs What the output should contain and its row grain: [TASK] Input files, formats and sizes: [INPUT_FILES] Column names, intended types and 5-20 sample rows, including messy ones: [SAMPLE_DATA] Specific operations needed (filters, joins, pivots, derived columns): [OPERATIONS] Rules for duplicates, missing values and invalid values: [DATA_RULES] Python and pandas versions, and memory limits if relevant: [ENVIRONMENT] ## Instructions 1. Restate the output grain, columns and types before writing code. List assumptions you had to make. 2. Read files with explicit dtypes for identifiers (keep leading zeros as strings) and an explicit date format. Do not rely on type inference for keys or dates. 3. Normalize text (strip whitespace, consistent case) before removing duplicates, and say which columns define a duplicate. 4. Convert invalid values with errors="coerce", then move rows that failed a rule into a quarantine DataFrame with a reason column instead of silently dropping them. 5. For joins, set how= and validate= (for example "many_to_one") and log row counts before and after. Use indicator=True when unmatched rows matter. 6. Do not use chained assignment such as df[mask]["col"] = value; use .loc or .assign. In pandas 3.0 and later, Copy-on-Write is always on and chained assignment no longer updates the original. Unless the stated version says otherwise, write code that runs on pandas 2.2 and 3.x. 7. Use vectorized operations. Use a row-wise apply only when there is no vectorized form, and say why. 8. For large inputs, consider usecols, narrower dtypes, category columns or chunksize, but do not promise memory savings you have not measured. 9. Finish with assertions that encode the output contract: unique keys, no nulls in required columns, the expected column set. ## Return 1. Assumptions and open questions 2. One runnable script organised as load, clean, transform, validate and save functions, with short comments explaining each step 3. Logging of row counts after each step 4. The result you expect on the supplied sample rows, worked out by hand and labelled "expected, not executed" unless you actually ran it 5. Edge cases the script does not handle ## Acceptance checks - The script runs end to end on the sample without warnings about chained assignment. - Every dropped or quarantined row is accounted for in the logged counts. - Join cardinality is validated, not assumed. - The final assertions would fail on a duplicate key or a missing required value.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Task: daily revenue by region, one row per (order_day, region). Invalid rows go to a quarantine file with a reason. orders.csv sample: order_id,order_date,amount,region 1001,2026-10-01,19.99,north 1002,2026-10-01, 5.00 ,North 1002,2026-10-01, 5.00 ,North 1003,not a date,7.50,south 1004,2026-10-02,,south 1005,2026-10-02,12.00,SOUTH Rules: a duplicate is the same order_id, order_date, amount and region after trimming and lower-casing. A missing amount or an unparseable date sends the row to quarantine. Environment: Python 3.9+, pandas 2.3 now and 3.x later.
The core pipeline from this example was executed locally with pandas 2.3.3 on the exact sample (a fixture run, not a model output). One duplicate row is removed after normalization. Two rows are quarantined: 1003 for an unparseable date and 1004 for a missing amount. The output has 2 rows: 2026-10-01 north 24.99 and 2026-10-02 south 12.00. A correct generated script parses amounts after str.strip(), lower-cases region before dropping duplicates, groups on a derived order_day column, and ends with an assertion that (order_day, region) is unique.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models