-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert `BiocFrame` objects to polars `DataFrame` and vice-verse. * Add method to flatten a nested `BiocFrame` object. * Update tests and documentation.
- Loading branch information
Showing
6 changed files
with
203 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
import pytest | ||
import pandas as pd | ||
from biocframe.BiocFrame import BiocFrame | ||
from biocutils import Factor | ||
|
||
__author__ = "jkanche" | ||
__copyright__ = "jkanche" | ||
__license__ = "MIT" | ||
|
||
|
||
def test_export_pandas(): | ||
obj = BiocFrame( | ||
{ | ||
"column1": [1, 2, 3], | ||
"nested": BiocFrame( | ||
{ | ||
"ncol1": [4, 5, 6], | ||
"ncol2": ["a", "b", "c"], | ||
"deep": ["j", "k", "l"], | ||
} | ||
), | ||
"column2": np.array([1, 2, 3]), | ||
} | ||
) | ||
|
||
pdf = obj.to_pandas() | ||
assert pdf is not None | ||
assert isinstance(pdf, pd.DataFrame) | ||
assert len(pdf) == len(obj) | ||
assert len(set(pdf.columns).difference(obj.colnames)) == 3 | ||
|
||
obj["factor"] = Factor([0, 2, 1], levels=["A", "B", "C"]) | ||
pdf = obj.to_pandas() | ||
assert pdf is not None | ||
assert isinstance(pdf, pd.DataFrame) | ||
assert len(pdf) == len(obj) | ||
assert len(set(pdf.columns).difference(obj.colnames)) == 3 | ||
assert pdf["factor"] is not None | ||
|
||
emptyobj = BiocFrame(number_of_rows=100) | ||
pdf = emptyobj.to_pandas() | ||
assert len(pdf) == len(emptyobj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pytest | ||
|
||
import polars as pl | ||
from biocframe import BiocFrame | ||
from biocutils import Names | ||
|
||
__author__ = "jkanche" | ||
__copyright__ = "jkanche" | ||
__license__ = "MIT" | ||
|
||
|
||
def test_from_polars(): | ||
obj = pl.DataFrame( | ||
{ | ||
"a": [None, 2, 3, 4], | ||
"b": [0.5, None, 2.5, 13], | ||
"c": [True, True, False, None], | ||
} | ||
) | ||
|
||
bframe = BiocFrame.from_polars(obj) | ||
assert bframe is not None | ||
assert isinstance(bframe.get_column_names(), Names) | ||
assert list(bframe.get_column_names()) == ["a", "b", "c"] | ||
|
||
|
||
def test_to_polars(): | ||
obj = BiocFrame( | ||
{ | ||
"a": [None, 2, 3, 4], | ||
"b": [0.5, None, 2.5, 13], | ||
"c": [True, True, False, None], | ||
} | ||
) | ||
|
||
plframe = obj.to_polars() | ||
assert plframe is not None | ||
assert isinstance(plframe, pl.DataFrame) | ||
assert plframe.columns == ["a", "b", "c"] | ||
|
||
|
||
def test_to_polars_nested(): | ||
obj = BiocFrame( | ||
{ | ||
"a": [None, 2, 3], | ||
"b": [0.5, None, 2.5], | ||
"c": [True, True, False], | ||
"nested": BiocFrame( | ||
{ | ||
"ncol1": [4, 5, 6], | ||
"ncol2": ["a", "b", "c"], | ||
"deep": ["j", "k", "l"], | ||
} | ||
), | ||
} | ||
) | ||
|
||
plframe = obj.to_polars() | ||
assert plframe is not None | ||
assert isinstance(plframe, pl.DataFrame) | ||
assert len(plframe.columns) == 6 | ||
assert plframe.columns == [ | ||
"a", | ||
"b", | ||
"c", | ||
"nested.ncol1", | ||
"nested.ncol2", | ||
"nested.deep", | ||
] |