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

Fix an issue when the frame is empty but number_of_rows>0 #106

Merged
merged 5 commits into from
May 16, 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
21 changes: 12 additions & 9 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,23 +1162,26 @@ def columns(self) -> ut.Names:
"""Alias for :py:attr:`~get_column_names`, provided for compatibility with **pandas**."""
return self.get_column_names()

def to_pandas(self) -> "pandas.DataFrame":
def to_pandas(self):
"""Convert the ``BiocFrame`` into a :py:class:`~pandas.DataFrame` object.

Returns:
A :py:class:`~pandas.DataFrame` object.
"""
from pandas import DataFrame

_data_copy = OrderedDict()
for col in self.column_names:
_data_copy[col] = self.column(col)
if isinstance(self.column(col), ut.Factor):
_data_copy[col] = _data_copy[col].to_pandas()
if len(self.column_names) > 0:
_data_copy = OrderedDict()
for col in self.column_names:
_data_copy[col] = self.column(col)
if isinstance(self.column(col), ut.Factor):
_data_copy[col] = _data_copy[col].to_pandas()

return DataFrame(
data=_data_copy, index=self._row_names, columns=self._column_names
)
return DataFrame(
data=_data_copy, index=self._row_names, columns=self._column_names
)
else:
return DataFrame(data={}, index=range(self._number_of_rows))

@classmethod
def from_pandas(cls, input: "pandas.DataFrame") -> "BiocFrame":
Expand Down
4 changes: 4 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ def test_export_pandas():
assert len(set(pdf.columns).difference(obj.colnames)) == 0
assert pdf["factor"] is not None

emptyobj = BiocFrame(number_of_rows=100)
pdf = emptyobj.to_pandas()
assert len(pdf) == len(emptyobj)


def test_names_generics():
obj = BiocFrame(
Expand Down
Loading