Skip to content

Commit e2fe1ec

Browse files
neuralsorcerermeta-codesync[bot]
authored andcommitted
Add full warning assertions for Sample.from_frame id inference (#337)
Summary: Added full warning assertions for `Sample.from_frame` id inference Pull Request resolved: #337 Differential Revision: D93365012 Pulled By: talgalili fbshipit-source-id: dd3327c85429a9882a89f0d580b15282db0f84aa
1 parent 9f0c383 commit e2fe1ec

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
to their first column before computation, matching validation behavior and
2727
returning scalar/Series outputs consistently.
2828

29+
## Tests
30+
31+
- **Expanded warning coverage for `Sample.from_frame()` ID inference**
32+
- Added assertions that validate all three expected warnings are emitted when inferring an `id` column and default weights, including ID guessing, ID string casting, and automatic weight creation.
33+
2934
# 0.16.0 (2026-02-09)
3035

3136
## New Features

tests/test_sample.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,18 @@ def test_Sample_from_frame_id_column_detection(self) -> None:
174174
"""
175175
# Test automatic id column detection
176176
df = pd.DataFrame({"id": (1, 2), "a": (1, 2)})
177-
self.assertWarnsRegexp(
178-
"Guessed id column name id for the data", Sample.from_frame, df
179-
)
180-
# TODO: add tests for the two other warnings:
181-
# - self.assertWarnsRegexp("Casting id column to string", Sample.from_frame, df)
182-
# - self.assertWarnsRegexp("No weights passed, setting all weights to 1", Sample.from_frame, df)
183-
# Using the above would fail since the warnings are sent sequentially and using self.assertWarnsRegexp
184-
# only catches the first warning.
185-
self.assertEqual(
186-
Sample.from_frame(df).id_column, pd.Series((1, 2), name="id").astype(str)
177+
with self.assertLogs("balance", level="WARNING") as captured_logs:
178+
sample = Sample.from_frame(df)
179+
180+
warning_messages = "\n".join(captured_logs.output)
181+
182+
self.assertIn("Guessed id column name id for the data", warning_messages)
183+
self.assertIn("Casting id column to string", warning_messages)
184+
self.assertIn(
185+
"No weights passed. Adding a 'weight' column and setting all values to 1",
186+
warning_messages,
187187
)
188+
self.assertEqual(sample.id_column, pd.Series((1, 2), name="id").astype(str))
188189

189190
# Test explicit id column specification
190191
df = pd.DataFrame({"b": (1, 2), "a": (1, 2)})

0 commit comments

Comments
 (0)