Note that pandas supports division by zero for columns with numeric dtype (such as float and int64) by returning a result of inf
. However, for columns of object type, it raises a ZeroDivisionError exception.
Example:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4,5], 'B':[0,1,2,3,4]})
print(df)
print('', 'result for:', f'{df.dtypes}:', sep='\n')
print(df['A'] / df['B'])
df = df.astype('float')
print('', 'result for:', f'{df.dtypes}:', sep='\n')
print(df['A'] / df['B'])
df = df.astype('object')
try:
print('', 'result for:', f'{df.dtypes}:', sep='\n')
print(df['A'] / df['B'])
except (ZeroDivisionError):
print('raised ZeroDivisionError exception')
Output:
A B
0 1 0
1 2 1
2 3 2
3 4 3
4 5 4
result for:
A int64
B int64
dtype: object:
0 inf
1 2.000000
2 1.500000
3 1.333333
4 1.250000
dtype: float64
result for:
A float64
B float64
dtype: object:
0 inf
1 2.000000
2 1.500000
3 1.333333
4 1.250000
dtype: float64
result for:
A object
B object
dtype: object:
raised ZeroDivisionError exception
One possible solution is to set the dtype of the columns you plan to divide to a numeric type such as float:
try:
print('', 'result for:', f'{df.dtypes}:', sep='\n')
print('first change column types to float')
df.A = df.A.astype('float')
df.B = df.B.astype('float')
print(df['A'] / df['B'])
except (ZeroDivisionError):
print('raised ZeroDivisionError exception')
Output:
result for:
A object
B object
dtype: object:
first change column types to float
0 inf
1 2.000000
2 1.500000
3 1.333333
4 1.250000
dtype: float64