pbPassingBI
/
Wrangling & cleaning beginner 6 min

Converting data types

astype, to_datetime, to_numeric, and handling values that will not convert.

What you'll be able to do
  • Convert between types
  • Parse dates reliably
  • Handle values that fail conversion

astype

df['zip']     = df['zip'].astype(str)
df['count']   = df['count'].astype(int)
df['region']  = df['region'].astype('category')

astype is strict — a single bad value raises and nothing is converted.

category saves real memory

A text column with few distinct values stored as category can use a fraction of the memory. Worth doing on region, status and similar fields in a large frame.

Dates

df['order_date'] = pd.to_datetime(df['order_date'])
df['order_date'] = pd.to_datetime(df['order_date'], format='%d/%m/%Y')
df['order_date'] = pd.to_datetime(df['order_date'], errors='coerce')
Ambiguous date formats

03/04/2024 is 3 April or 4 March depending on where the file came from. pandas guesses, and it can guess differently for different rows in the same column.

Always pass format= when you know it. Silent misparsing is far worse than an error.

Once converted, the .dt accessor opens up:

df['year']    = df['order_date'].dt.year
df['month']   = df['order_date'].dt.month
df['weekday'] = df['order_date'].dt.day_name()
df['month_start'] = df['order_date'].dt.to_period('M').dt.to_timestamp()

Numbers that will not convert

df['revenue'] = pd.to_numeric(df['revenue'], errors='coerce')

errors='coerce' turns anything unparseable into NaN instead of raising, which lets the rest of the column convert.

Then find out what failed:

bad = df[df['revenue'].isnull()]

Usually currency symbols, thousands separators or a stray footnote row. Clean them first:

df['revenue'] = (df['revenue'].astype(str)
                   .str.replace(r'[$,]', '', regex=True)
                   .pipe(pd.to_numeric, errors='coerce'))
Key points
  • astype is strict; to_numeric/to_datetime with errors="coerce" is forgiving
  • Always pass format= to to_datetime when you know the layout
  • Inspect the rows that coerced to NaN — they show what is dirty
Check yourself