Period 9 · Converting Numbers and Dates
[video] Lecture video — Period 9: Converting Numbers and Dates — /api/lesson-media/videos/4d3653ff-a3ca-4f63-bf8e-bb93c553e599-1794892497130-82709a1a2d6951af.mp4
[markdown] content available only on the platform
[markdown] content available only on the platform
Flashcards · Converting Numbers and Dates
pd.to_numeric() — Converts values to numbers; with `errors="coerce"` unreadable values become missing instead of raising an error.
`pd.to_numeric(df["quantity"], errors="coerce")`
errors="coerce" — An option that replaces values that cannot be converted with a missing value.
`"three"` becomes NaN.
Conversion failure — A value that was present in the original column but became missing after conversion.
`df["quantity"].notna() & df["quantity_num"].isna()`
pd.to_datetime() — Converts text to dates; an explicit `format` avoids guessing the meaning of day and month.
`pd.to_datetime(s, format="%d/%m/%Y", errors="coerce")`
NaT — Not a Time: the pandas missing-value marker for datetime values.
`31/02/2025` becomes NaT.
Thousands separator — A symbol grouping digits in large numbers, which must be removed before conversion if confirmed.
`"142,000".replace(",", "")` gives `"142000"`.
Whole-number check — A rule that the remainder after dividing by 1 is zero, rejecting fractions.
`(q > 0) & (q % 1 == 0)` rejects 2.5.
Issue flags — Boolean columns, one per problem type, explaining why a record is held for review.
`issues.any(axis=1)` marks rows with at least one issue.
Int64 (nullable) — A pandas integer dtype that allows missing values, used after quantities are validated as whole.
`clean["quantity_num"].astype("Int64")`
[pdf] Lecture slides — Period 9 (PDF) — /api/lesson-media/pdfs/4d3653ff-a3ca-4f63-bf8e-bb93c553e599-1794873721015-37bf40b59ebe1f64.pdf
