Dataype Downcasting
Sat 17 May 2025
title: "Datatype Downcasting" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false
import numpy as np
import pandas as pd
ds = pd.Series([1, np.nan, 3, 4, 5])
ds
0 1.0
1 NaN
2 3.0
3 4.0
4 5.0
dtype: float64
# ds = ds.astype('int') # will throw ValueError: Cannot convert non-finite values (NA or inf) to integer
ds = ds.astype('float')
ds
0 1.0
1 NaN
2 3.0
3 4.0
4 5.0
dtype: float64
ds = pd.to_numeric(ds, downcast='float')
ds
0 1.0
1 NaN
2 3.0
3 4.0
4 5.0
dtype: float32
# You could see the dtype chagend fro float64 to float32
Score: 10
Category: data-wrangling