pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.72k stars 17.93k forks source link

BUG: np.nan_to_num change pandas DataFrame in place when copy = True #59932

Closed lil-blue-curve closed 3 days ago

lil-blue-curve commented 1 month ago

Pandas version checks

Reproducible Example

import numpy as np
import pandas as pd

x = pd.DataFrame([np.nan])
np.nan_to_num(x, copy=True)
print(x)

Issue Description

Result x is changed to 0.0 even when nan_to_num is called with copy=True.

This issue is originally reported to numpy here. Looks like pandas ignores copy=True NumPy passes?

Expected Behavior

x should not be changed when copy=True.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0691c5cf90477d3503834d983f69350f250a6ff7 python : 3.11.8 python-bits : 64 OS : Linux OS-release : 5.10.0-smp-1100.466.602.1 Version : #1 [v5.10.0-1100.466.602.1] SMP @1708156698 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : None LOCALE : en_US.UTF-8 pandas : 2.2.3 numpy : 2.1.1 pytz : 2024.1 dateutil : 2.8.1 pip : None Cython : None sphinx : None IPython : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : None lxml.etree : 5.3.0 matplotlib : None numba : None numexpr : 2.8.7 odfpy : None openpyxl : 3.1.2 pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.13.1 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : 2.0.1 xlsxwriter : None zstandard : None tzdata : None qtpy : None pyqt5 : None
asishm commented 1 month ago

Thanks, this seems to be related to https://github.com/pandas-dev/pandas/issues/57739

chaoyihu commented 3 weeks ago

This issue no longer exists on the main branch as of 2a10e04a099d5f1633abcdfbb2dd9fdf09142f8d. The script returns:

    0
0 NaN
asishm commented 3 weeks ago

This issue no longer exists on the main branch as of 2a10e04. The script returns:

    0
0 NaN

I saw in one of your edits that you have numpy 1.26.4 This issue only should only occur with numpy 2.0+

chaoyihu commented 3 weeks ago

I saw in one of your edits that you have numpy 1.26.4 This issue only should only occur with numpy 2.0+

I see. Sorry for the noise.