From 27fc78baea72115fc1828d0a391dba16b005d2dc Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Thu, 16 May 2024 08:05:08 -0700 Subject: [PATCH 1/4] Fix an issue when the frame is empty but number_of_rows>0 --- src/biocframe/BiocFrame.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/biocframe/BiocFrame.py b/src/biocframe/BiocFrame.py index e1ec4f1..4a69997 100644 --- a/src/biocframe/BiocFrame.py +++ b/src/biocframe/BiocFrame.py @@ -1162,7 +1162,7 @@ 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: @@ -1170,15 +1170,20 @@ def to_pandas(self) -> "pandas.DataFrame": """ 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: + DataFrame( + data={}, index=range(self._number_of_rows) + ) @classmethod def from_pandas(cls, input: "pandas.DataFrame") -> "BiocFrame": From 65f0d5d5847393317253d4b1629504a55d439898 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 May 2024 15:05:56 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/biocframe/BiocFrame.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/biocframe/BiocFrame.py b/src/biocframe/BiocFrame.py index 4a69997..4d516c7 100644 --- a/src/biocframe/BiocFrame.py +++ b/src/biocframe/BiocFrame.py @@ -1181,9 +1181,7 @@ def to_pandas(self): data=_data_copy, index=self._row_names, columns=self._column_names ) else: - DataFrame( - data={}, index=range(self._number_of_rows) - ) + DataFrame(data={}, index=range(self._number_of_rows)) @classmethod def from_pandas(cls, input: "pandas.DataFrame") -> "BiocFrame": From 3f36def5abf97270f708995c91bdad320ff579da Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Thu, 16 May 2024 08:08:31 -0700 Subject: [PATCH 3/4] missing return and adding tests --- src/biocframe/BiocFrame.py | 2 +- tests/test_methods.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/biocframe/BiocFrame.py b/src/biocframe/BiocFrame.py index 4a69997..2288b25 100644 --- a/src/biocframe/BiocFrame.py +++ b/src/biocframe/BiocFrame.py @@ -1181,7 +1181,7 @@ def to_pandas(self): data=_data_copy, index=self._row_names, columns=self._column_names ) else: - DataFrame( + return DataFrame( data={}, index=range(self._number_of_rows) ) diff --git a/tests/test_methods.py b/tests/test_methods.py index e9da191..c1b9ca2 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -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( From 5b1f050d859a47e8ed4b4b06918f3af0d9037445 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 May 2024 15:09:59 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_methods.py b/tests/test_methods.py index c1b9ca2..269f2b0 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -565,7 +565,7 @@ 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) + emptyobj = BiocFrame(number_of_rows=100) pdf = emptyobj.to_pandas() assert len(pdf) == len(emptyobj)