Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] 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
Comment From: Allan0820
take
Comment From: yuanx749
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?
Comment From: allmight05
Does anyone working on this? any improvements?
Comment From: jbrockmendel
Can you post an example that is copy-pastable without having to load an unknown zip file? See https://matthewrocklin.com/minimal-bug-reports.html
Comment From: mroeschke
Closing as this issue needs more information to be actionable