Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a friendly message when a str is provided to TensorFrame(col_names_dict) instead of a list[str] #469

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions test/data/test_tensor_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,11 @@ def test_custom_tf_get_col_feat():
assert torch.equal(feat, feat_dict['numerical'][:, 0:1])
feat = tf.get_col_feat('num_2')
assert torch.equal(feat, feat_dict['numerical'][:, 1:2])


def test_non_list_col_names_dict():
feat_dict = {torch_frame.categorical: torch.randint(0, 3, size=(10, 1))}
# Oops, user provided a single column name without wrapping it in a list:
col_names_dict = {torch_frame.categorical: 'cat_1'}
with pytest.raises(ValueError, match='must be a list of column names'):
TensorFrame(feat_dict, col_names_dict)
8 changes: 7 additions & 1 deletion torch_frame/data/tensor_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ def validate(self) -> None:
num_rows = self.num_rows
empty_stypes: list[torch_frame.stype] = []
for stype_name, feats in self.feat_dict.items():
num_cols = len(self.col_names_dict[stype_name])
col_names = self.col_names_dict[stype_name]
if not isinstance(col_names, list):
raise ValueError(
f"col_names_dict[{stype_name}] must be a list of column "
f"names.")

num_cols = len(col_names)
if num_cols == 0:
empty_stypes.append(stype_name)

Expand Down
Loading