Skip to content

BUG: Reindexing pd.Float64Dtype() series gives runtime warnings when passed to np.log #47055

Closed
@SanderBeekhuis

Description

@SanderBeekhuis

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd

s = pd.Series([1.0, pd.NA], dtype=pd.Float64Dtype())

# Works fine
print(np.log(s))
# 0     0.0
# 1    <NA>

reindexed = s.reindex(range(3))

# Raises warning, but output is still correct
print(np.log(reindexed))
#  C:\Users\BeekhuisSJ\AppData\Local\pypoetry\Cache\virtualenvs\pandas-playground-CCJS5VSa-py3.10\lib\site-packages\pandas\core\arrays\masked.py:484: RuntimeWarning: divide by zero encountered in log
#   result = getattr(ufunc, method)(*inputs2, **kwargs)
# 0     0.0
# 1    <NA>
# 2    <NA>

# Bonus
print(reindexed.values._data)
# [ 1. nan  0.]

Issue Description

A RuntimeWarning referring to a 0 value is raised when processing a series with only non-zero values and pd.NA when processing an pd.Series that has been created by reindexing.

Possibly because the underlying .values._data attribute contains a 0 i.s.o. of a np.nan

Expected Behavior

No runtime warning is raised since no actual zero value is present in the array.

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.10.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 62.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions