Skip to content

BUG: replace value failed #61643

@peng256

Description

@peng256

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issuereplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions