Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Wrangle / Preprocess lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Types, dates & missingness · WR3 · 12 MIN

Review · Gaps and duplicates

Task 1 · Retrieve and combine

Exercises within this concept

  1. Task 1Retrieve and combineCurrent exercise
  2. Task 2Retrieve and combine
  3. Task 3Retrieve and combine
  4. Task 4Retrieve and combine
Task 1 · Messy pet supplies

Create a separate clean copy of df with parsed dates.

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Your requirements

  1. Keep only records dated on or after 2026-08-04 and sort them by date ascending.
  2. Display clean; preserve df.

Your inputs

Messy pet supplies · 6 synthetic rows
orderitempackagepricediscountdate
301" hay-bale "BULK8.5012026-08-01
302FOODsmall1222026-08-02
303" toy-ball "SINGLEbadNoneinvalid
304BowlSmallNone0.52026-08-04
304BowlSmallNone0.52026-08-04
305" food "BULK61.52026-08-06

Revisit the concept lesson →

Remember the idea

Concept sketch: text → datetime; invalid → NaTtext2026-06-01not a datedatetime2026-06-01NaTtext → datetime; invalid → NaT

Parse date strings before comparing calendar dates. An explicit format tells pandas which part is the year, month and day.

A small example

"2026-08-04" with "%Y-%m-%d" means 4 August 2026. Invalid text becomes NaT with errors="coerce".

What each choice does
Code or choiceMeaning
%Y / %m / %dFour-digit year / month number / day number; literal hyphens match the input separators.
errors="coerce"Make invalid dates missing (NaT); the default raises a parsing error.
parsed >= "2026-08-04"Keep that date and later dates. Missing dates do not pass this comparison.
Hint

Parse before chronological comparison; invalid dates will not pass.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

clean = df.copy()
clean["date"] = pd.to_datetime(clean["date"], format="%Y-%m-%d", errors="coerce")
clean = clean[clean["date"] >= "2026-08-04"].sort_values("date")
clean
Your task · Task 1
  1. Create a separate clean copy of df with parsed dates.
  2. Keep only records dated on or after 2026-08-04 and sort them by date ascending.
  3. Display clean; preserve df.

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display a value by leaving it on the final line.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.