Skip to content

Commit 6a3eef7

Browse files
authored
Preserve attrs on Dask DataFrame sample() (#1088)
Add "sample" to the list of wrapped methods in _accessor.py so that calling .sample() on a Dask DataFrame retains its .attrs metadata, consistent with the other wrapped methods.
1 parent 094b869 commit 6a3eef7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/spatialdata/models/_accessor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def __repr__(self) -> str:
124124
"copy",
125125
"drop",
126126
"map_partitions",
127+
"sample",
127128
"set_index",
128129
]:
129130
wrap_method_with_attrs(method_name=method_name, dask_class=DaskDataFrame)

tests/models/test_accessor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ def test_dataframe_set_index_preserves_attrs():
130130
assert isinstance(result.attrs, AttrsAccessor)
131131

132132

133+
def test_dataframe_sample_preserves_attrs():
134+
"""Test that DataFrame.sample preserves attrs."""
135+
df = dd.from_pandas(pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}), npartitions=2)
136+
df.attrs["key"] = "value"
137+
result = df.sample(frac=0.5)
138+
assert result.attrs["key"] == "value"
139+
assert isinstance(result.attrs, AttrsAccessor)
140+
141+
133142
# ============================================================================
134143
# Series wrapped methods tests
135144
# ============================================================================

0 commit comments

Comments
 (0)