You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importreimportuuidimportnumpyasnpimportpandasaspd## Generate example DataFramet=pd.date_range(start='2023-01-01 00:00', periods=10, freq='10min')
x=np.random.randn(t.size)
y=np.random.randn(t.size)
df=pd.DataFrame({
'Timestamp': t,
'X position (m)': x,
'Y position (m)': y,
'Temperature (degC)': temp,
})
df=pd.concat([
pd.DataFrame(
[dict(
zip(list(df.columns),
['SignalId'] + [str(uuid.uuid4()) foriinrange(df.columns.size-1)]
))]
),
df], ignore_index=True
)
df=df.set_index('Timestamp')
## Change column name inplacefori, cinenumerate(list(df.columns)):
newc=re.sub(r'\s+position\s+', ' ', c)
df.columns.values[i] =newc## Printing DataFrame to screen may generate a segmentation faultdf
Issue Description
When a column name from a DataFrame is changed inplace (at the values), sometimes it leads to a segmentation fault. This seems more likely if the DataFrame contains mixed element types (as per example below).
Hypotheses are:
The change in the name leads to corruption of the data in memory.
NumPy version >2 leads to different data types that may conflict somehow with some operations.
Though the operation may be debatable (the change inplace of the column name via df.column.values[i] = new_name), it is a valid operation without any other warning or error message. The ensuing segmentation fault is completely random (so very hard to diagnose).
Hence the expected behaviour is to either block these operations, or alternatively to fully allow those if these are to be permitted.
Installed Versions
INSTALLED VERSIONS
------------------
commit : 0691c5c
python : 3.11.11
python-bits : 64
OS : Linux
OS-release : 4.19.0-27-amd64
Version : #1 SMP Debian 4.19.316-1 (2024-06-25)
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
This is definitely not supported; you're modifying the internal numpy data inside the Index. The internal data, via .values, is not writeable anymore when using copy-on-write. Thus you can get your requested behavior where write access to Index.values is banned and generates an error by turning on copy-on-write in pandas 2 (see https://pandas.pydata.org/docs/user_guide/copy_on_write.html), or waiting for pandas 3 (when copy-on-write will be on by default).
In the meantime, the supported way to do what you are trying to do in your example would be df.columns = df.columns.str.replace(r'\s+position\s+', ' ', regex=True)
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
Issue Description
When a column name from a DataFrame is changed inplace (at the values), sometimes it leads to a segmentation fault. This seems more likely if the DataFrame contains mixed element types (as per example below).
Hypotheses are:
Example:
Expected Behavior
Though the operation may be debatable (the change inplace of the column name via
df.column.values[i] = new_name
), it is a valid operation without any other warning or error message. The ensuing segmentation fault is completely random (so very hard to diagnose).Hence the expected behaviour is to either block these operations, or alternatively to fully allow those if these are to be permitted.
Installed Versions
pandas : 2.2.3
numpy : 2.0.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : 8.18.1
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 : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.1
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.7.0
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: