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,datetime64andcategory - Turn text into numbers with
to_numeric, and into dates withto_datetime - Explain why one missing value turns an integer column into
float64, and fix it withInt64
The types you’ll meet
| dtype | holds | example |
|---|---|---|
int64 | whole numbers | kilos, counts |
float64 | decimals, and NaN | prices, averages |
bool | True or False | open that day? |
str | text | store names |
datetime64 | dates and times | the sale date |
category | a few values, repeated | store, region, size |
Int64 | whole numbers that can be missing | step 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:
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:
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:
pd.to_numeric(..., errors="coerce")converts what it can and makes the restNaN. Count theNaNs 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_datetimeturns date text into real dates. Then the.dtaccessor can tell you the weekday, month or year. (Or read the dates as dates in the first place, withparse_datesfrom 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.
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
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.
- pandas: Essential basic functionality, dtypes: every dtype pandas uses
- pandas: Nullable integer data type: Int64 and pd.NA
- pandas: pandas.to_numeric: errors="coerce"
- pandas: pandas.to_datetime: text into dates, and the format argument
- pandas: Categorical data: when a category type saves memory and helps