Skip to content

BUG: replace value failed #61643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
peng256 opened this issue Jun 13, 2025 · 2 comments
Open
2 of 3 tasks

BUG: replace value failed #61643

peng256 opened this issue Jun 13, 2025 · 2 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@peng256
Copy link

peng256 commented Jun 13, 2025

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

start_time = '2025-06-06'
end_time = '2025-06-09'

sig1 = pd.read_parquet('data1.par')
sig1 = sig1[(sig1.tradeDate >= start_time) & (sig1.tradeDate <= end_time)]
sig1 = sig1.pivot(index='tradeDate', columns='ticker', values='signal_value').fillna(0)

sig2 = pd.read_parquet('data2.par')
sig2 = sig2[(sig2.tradeDate >= start_time) & (sig2.tradeDate <= end_time)]
sig2 = sig2.pivot(index='tradeDate', columns='ticker', values='signal_value').fillna(0)

sig = sig1 + sig2

filt = pd.read_feather('filter.fea').set_index('tradeDate')
filt.index = pd.to_datetime(filt.index)
filt = filt.reindex(sig.index, columns=sig.columns)

# method 1: make a copy then filter
s1 = sig.copy()
s1.values[:] = np.where(filt == 1, s1, np.nan)
print(s1.count(axis=1))

# method 2: directly filter
sig.values[:] = np.where(filt == 1, sig, np.nan)
print(sig.count(axis=1))

Issue Description

Why not work:

sig.values[:] = np.where(filt == 1, sig, np.nan)

If using a copy, the sentence above works:

s1 = sig.copy()
s1.values[:] = np.where(filt == 1, s1, np.nan)

Expected Behavior

Both methods should work.

Installed Versions

data.zip

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.7.final.0
python-bits : 64
OS : Darwin
OS-release : 24.5.0
Version : Darwin Kernel Version 24.5.0: Tue Apr 22 19:48:46 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8103
machine : arm64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 75.1.0
pip : 24.2
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 7.3.7
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.2.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.27.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.1
gcsfs : None
matplotlib : 3.9.2
numba : 0.60.0
numexpr : 2.8.7
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
pyarrow : 16.1.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.6.1
scipy : 1.13.1
sqlalchemy : 2.0.34
tables : 3.10.1
tabulate : 0.9.0
xarray : 2023.6.0
xlrd : None
zstandard : 0.23.0
tzdata : 2023.3
qtpy : 2.4.1
pyqt5 : None

@peng256 peng256 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 13, 2025
@peng256 peng256 changed the title BUG: inplace value failed BUG: replace value failed Jun 13, 2025
@Allan0820
Copy link

take

@yuanx749
Copy link
Contributor

I think the underlying values array is read-only, see https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html#read-only-numpy-arrays

Could you use pd.where instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants