Pandas · Convert data types
Google Search Analytics
Course overviewGoogle Search · Clean values and create columns

Convert data types

Parse numeric text, identify failed conversions, and preserve missing values with nullable types.

Step 1 of 3 · Learn

Turn numeric text into numbers

pd.to_numeric() parses numeric text. With errors="coerce", text that cannot be parsed becomes missing instead of stopping execution; a missing value remains missing.

indexvalue
0120
1timeout
2NA
indexvalue
0120
1NA
2NA
The valid number is parsed. Invalid text and missing input both become missing.

Keep integers and missing values together

Use .astype("Int64") for nullable integers. The capital I matters: ordinary int64 cannot hold missing values. Convert only whole-number measurements to this integer type.

Review failed conversions

A missing converted value can come from bad text or an already-missing input. Compare the original column's .notna() mask with the converted column's .isna() mask to isolate non-missing text that failed.

▷ Your turn

From raw_searches, return search_id and response_ms in source row order. Convert response_ms to numeric values, coercing invalid text to missing; preserve already-missing input. Save the DataFrame as result.

Available for this lesson: raw_searches, a six-row copy of searches with response_ms stored as text. Its second row contains "timeout" and its third row is missing. The original searches DataFrame is unchanged.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.