pandas basics · lesson 7 of 9
Parts in this lesson
Adf.dtypes3.2: every column’s type
Bpd.to_numeric(s, errors="coerce")numbers, with anything else missing
Cpd.to_datetime(s)text into dates
Ds.astype("Int64")whole numbers that allow a gap

Types in pandas

Last updated

A column's type decides what you can do with it: add it up, sort it by date, count its categories. Files don't say what type anything is, so pandas guesses, and it often guesses text. In this lesson you'll read each column's type, fix the wrong ones, and meet the nullable types that keep whole numbers whole when a value is missing.

By the end of this lesson you can

  • Name pandas’ main dtypes: int64, float64, bool, str, datetime64 and category
  • Turn text into numbers with to_numeric, and into dates with to_datetime
  • Explain why one missing value turns an integer column into float64, and fix it with Int64

The types you’ll meet

dtypeholdsexample
int64whole numberskilos, counts
float64decimals, and NaNprices, averages
boolTrue or Falseopen that day?
strtextstore names
datetime64dates and timesthe sale date
categorya few values, repeatedstore, region, size
Int64whole numbers that can be missingstep 3

The first three are NumPy's, from lesson 2.3. str is new in pandas 3; older pandas used object for text, and you'll still see that in tutorials. Notice Int64 with a capital I is a different type from int64.

Here's a file where the types come out wrong, and what fixes each one:

Each column’s dtype, fixed one by one. The badges show each column’s dtype, straight after read_csv. Kilos should be numbers, but its badge says str: one value in it is a ?.strstrstrfloat642024-01-02Batangas4245.02024-01-02Lipa?46.02024-01-03Batangas3845.02024-01-03Lipa3346.0rawdatestorekilosprice0123
1 of 4 The badges show each column’s dtype, straight after read_csv. Kilos should be numbers, but its badge says str: one value in it is a ?.

Fixing types

First, the strict way. Lipa's kilos on 2 January is a ?. Try to make the column whole numbers:

strict.ipynb

astype refuses: ? isn't a number. That's useful, because it tells you a bad value exists. But on a file of 50,000 rows you want the rest converted and the bad ones marked. Run the cells:

types.ipynb
  • pd.to_numeric(..., errors="coerce") converts what it can and makes the rest NaN. Count the NaNs before and after, so you know how many values it threw away.
  • .astype("Int64") makes the result whole numbers again, with <NA> for the gap. Step 3 explains why that's needed.
  • pd.to_datetime turns date text into real dates. Then the .dt accessor can tell you the weekday, month or year. (Or read the dates as dates in the first place, with parse_dates from lesson 3.3.)
  • .astype("category") suits a column with a few values repeated many times. pandas stores each store name once, and a small code for each row. On a big table that saves memory, and lesson 4.9 shows what else it does.

Wrong way: one gap makes floats

Kilos are whole numbers. Run the cells.

gap.ipynb

One None in the list, and the whole Series became float64: 42 is now 42.0. The reason is lesson 2.3: missing is NaN, NaN is a float, and a NumPy integer array has no way to say "missing". So pandas makes every value a float to fit the gap in. The same happens when read_csv meets an empty cell in a column of whole numbers.

Usually harmless, but not always: a float keeps only about 16 digits, so a 19-digit transaction number stored as a float has its last digits changed. And 42.0 in a report looks careless.

The fix is pandas' nullable integer type, Int64, which has a real missing value, <NA>. Convert with .astype("Int64"), or ask for it when reading: dtype={"kilos": "Int64"}, as in the last cell.

Check what you learned

A column of prices has dtype str. Which converts it to numbers, making anything unreadable missing?
A column of counts reads as float64, though every count is whole. What’s the most likely reason?
Which column is the best fit for category?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis