Skip to content

RuntimeWarning in DataFrame.div with fill_value=0 #26793

Closed
@TomAugspurger

Description

@TomAugspurger

Typically we silence these

import pandas as pd
import numpy as np


a = pd.DataFrame({"A": [0, 2], "B": [0, 4]})
b = pd.DataFrame({"A": [0, 2], "C": [0, 4]}, index=[0, 2])

pdf1 = pd.DataFrame({'A': np.arange(10),
                     'B': [np.nan, 1, 2, 3, 4] * 2,
                     'C': [np.nan] * 10,
                     'D': np.arange(10)},
                    index=list('abcdefghij'), columns=list('ABCD'))
pdf2 = pd.DataFrame(np.random.randn(10, 4),
                    index=list('abcdefghjk'), columns=list('ABCX'))
pdf1.div(pdf2, fill_value=0)

ouput

/Users/taugspurger/sandbox/pandas/pandas/core/frame.py:5138: RuntimeWarning: divide by zero encountered in true_divide
  return func(left, right)
/Users/taugspurger/sandbox/pandas/pandas/core/frame.py:5138: RuntimeWarning: invalid value encountered in true_divide
  return func(left, right)
Out[46]:
           A          B    C    D    X
a  -0.000000   0.000000 -0.0  NaN  0.0
b  -1.381770  -0.870061  0.0  inf -0.0
c  -7.686931  -5.446084  0.0  inf  0.0
d  -5.396100   1.323770 -0.0  inf  0.0
e   4.114989  -4.120187 -0.0  inf  0.0
f -15.748104   0.000000  0.0  inf  0.0
g  -7.147032   0.990337 -0.0  inf  0.0
h  -3.421108  -3.465087  0.0  inf  0.0
i        inf        inf  NaN  inf  NaN
j  27.476268  40.169326 -0.0  inf -0.0
k  -0.000000   0.000000  0.0  NaN -0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Numeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions