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

Split BiocFrame by a column #91

Merged
merged 1 commit into from
Jan 2, 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
46 changes: 46 additions & 0 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,52 @@ def copy(self):
"""Alias for :py:meth:`~__copy__`."""
return self.__copy__()

##########################
######>> split by <<######
##########################

def split(
self, name: str, only_indices: bool = False
) -> Dict[str, Union["BiocFrame", List[int]]]:
"""Split the object by a column.

Args:
group:
Name of the column to split by.

only_indices:
Whether to only return indices.
Defaults to False

Returns:
A dictionary of biocframe objects, with names representing the
group and the value the sliced frames.

if ``only_indices`` is True, the values contain the row indices
that map to the same group.
"""
if name not in self._column_names:
raise ValueError(f"'{name}' is not a valid column name.")

_column = self.get_column(name)

_grps = {}
for i in range(len(self)):
_key = _column[i]
if _key not in _grps:
_grps[_key] = []

_grps[_key].append(i)

if only_indices is True:
return _grps

_sliced_grps = {}
for k, v in _grps.items():
_sliced_grps[k] = self[v,]

return _sliced_grps

################################
######>> pandas interop <<######
################################
Expand Down
30 changes: 30 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,33 @@ def test_set_names():
with pytest.raises(ValueError) as ex:
obj.set_column_names(["A", "A"])
assert str(ex.value).find("duplicate column name") >= 0


def test_bframe_split():
obj = {
"column1": [1, 2, 3],
"nested": [
{
"ncol1": [4, 5, 6],
"ncol2": ["a", "b", "c"],
"deep": {"dcol1": ["j", "k", "l"], "dcol2": ["a", "s", "l"]},
},
{
"ncol2": ["a"],
"deep": {"dcol1": ["j"], "dcol2": ["a"]},
},
{
"ncol1": [5, 6],
"ncol2": ["b", "c"],
},
],
"column2": ["b", "n", "b"],
}

bframe = BiocFrame(obj)
split_frame = bframe.split("column2")

assert split_frame is not None
assert isinstance(split_frame, dict)
assert len(split_frame) == 2
assert len(split_frame["b"]) == 2
Loading