Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

dtypes-casting

Dtypes and casting

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Dtypes and casting: Pandas tabular manipulation—indexing, dtypes, reshaping, and analysis habits for real-world tables.

Teams apply Dtypes and casting in every serious Pandas project—skipping it leaves blind spots in analysis and reviews.

You will apply Dtypes and casting in contexts like: CSV/Parquet analysis, ETL notebooks, and ad hoc reporting.

Read the narrative, run `import pandas as pd` snippets with in-memory DataFrames (install pandas and numpy with pip if needed), inspect `.head()`, `.dtypes`, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Each column has a dtype controlling memory and valid operations. Cast explicitly with astype, pd.to_numeric, or pd.to_datetime—especially after CSV loads.

Common dtypes

  • int64, float64 — numeric
  • object — Python strings or mixed (often strings from CSV)
  • bool — True/False
  • category — low-cardinality strings (memory efficient)
  • datetime64[ns] — timestamps
  • Int64 (capital I) — nullable integer with NaN support

Safe conversion

import pandas as pd
s = pd.Series(['1', '2', 'bad', '4'])
nums = pd.to_numeric(s, errors='coerce')  # 'bad' → NaN
print(nums)

Downcasting

Use smallest dtype that fits (pd.to_numeric(..., downcast='integer')) on large datasets to save memory. Always validate after casting.

Important interview questions and answers

  1. Q: object dtype?
    A: Often means strings from CSV—convert before math operations.
  2. Q: errors='coerce'?
    A: Invalid values become NaN instead of raising—good for messy real data.

Self-check

  1. Convert a string column to float with bad values coerced.
  2. What dtype supports nullable integers?

Pitfall: astype(int) on floats truncates—round or use errors='coerce' first.

Interview prep

to_numeric?

Converts strings to numbers; errors='coerce' turns bad values to NaN.

Int64?

Nullable integer extension dtype—supports NaN unlike int64.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • astype copy?
  • category dtype when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump