0

getting a float division by zero error when I'm expecting inf where the divisor is 0

df['percent'] = df['value1'] / df['value2']

Here is the df:

enter image description here

ZeroDivisionError: float division by zero

It's absolutely acceptable for the df['percent'] to read infinity, but instead I have this error so the code doesn't run.

Derek O
  • 16,770
  • 4
  • 24
  • 43
cm2115
  • 51
  • 1
  • 8
  • What pandas version is this? The DataFrame copied from the answer returns the output provided in the answer, **without** the inefficient `apply` and simply using `df.value1 / df.value2`. – S3DEV Nov 30 '21 at 19:03
  • @S3DEV yeah that was my bad - i should have checked that pandas supports division by 0 before posting an answer. i'll change my answer to reflect this – Derek O Nov 30 '21 at 19:13
  • Does this answer your question? [handling zeros in pandas DataFrames column divisions in Python](https://stackoverflow.com/questions/16244180/handling-zeros-in-pandas-dataframes-column-divisions-in-python) – Derek O Nov 30 '21 at 19:17
  • 1
    @DerekO - For clarity the box I tested on has pandas 0.25.1 installed. So quite old really. – S3DEV Nov 30 '21 at 19:38
  • Yeah, that's about two years out of date. did upgrading pandas resolve the issue? – Derek O Nov 30 '21 at 20:57
  • upgraded pandas and it did not solve the issue, I will try the solution below as well – cm2115 Nov 30 '21 at 21:18
  • hmm that's a bit surprising. what does `pd.__version__` return? – Derek O Nov 30 '21 at 21:23

2 Answers2

4

NOTE: pandas behaves differently for columns of different dtypes. It supports division by zero for columns with numeric dtype (such as float and int64) by returning a result of inf, but for columns of object type, it raises a ZeroDivisionError exception.

See my answer to a related question for examples.

constantstranger
  • 9,176
  • 2
  • 5
  • 19
2

If you are using the latest version of pandas, you should be able to divide by 0. I can run your code without getting a division by zero error.

If for some reason you are using a version of pandas that doesn't support division by 0, and cannot upgrade pandas for this particular problem, you can get around this error by using numpy.inf whenever df['value2'] = 0, and calculating the percentage normally otherwise:

import pandas as pd
import numpy as np

df = pd.DataFrame({'value1':[4,5,2,0.1],'value2':[8,7,0,0]})
df["percent"] = df.apply(lambda x: x['value1']/x['value2'] if x['value2'] != 0 else np.inf, axis=1)

Result:

>>> df
   value1  value2   percent
0     4.0       8  0.500000
1     5.0       7  0.714286
2     2.0       0       inf
3     0.1       0       inf
Derek O
  • 16,770
  • 4
  • 24
  • 43