Skip to content

Commit 469a7d0

Browse files
Fix for pandas 3 CoW policy (#1106)
1 parent fb90f03 commit 469a7d0

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/squidpy/gr/_niche.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ def _get_nhood_profile_niches(
456456
nhood_profile = weighted_profile
457457

458458
# create AnnData object from neighborhood profile to perform scanpy functions
459-
adata_neighborhood = ad.AnnData(X=nhood_profile)
459+
# Use .to_numpy(copy=True) to ensure the array is writeable (required for pandas CoW compatibility)
460+
# Preserve the DataFrame index for later matching with adata_masked
461+
adata_neighborhood = ad.AnnData(X=nhood_profile.to_numpy(copy=True), obs=pd.DataFrame(index=nhood_profile.index))
460462

461463
# reason for scaling see https://monkeybread.readthedocs.io/en/latest/notebooks/tutorial.html#niche-analysis
462464
if scale:

src/squidpy/tl/_sliding_window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def sliding_window(
152152
else:
153153
col_name = f"{sliding_window_key}_{lib_key}window_{idx}"
154154
sliding_window_df.loc[obs_indices, col_name] = True
155-
sliding_window_df.loc[:, col_name].fillna(False, inplace=True)
155+
# Avoid chained assignment for pandas CoW compatibility
156+
sliding_window_df[col_name] = sliding_window_df[col_name].fillna(False)
156157

157158
if overlap == 0:
158159
# create categorical variable for ordered windows

0 commit comments

Comments
 (0)