From b0b254c90b349afe6e8963936ad88b67549afa68 Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Thu, 14 May 2026 21:11:23 -0700 Subject: [PATCH] Support creating biocframes with 0 rows but column names are specified --- CHANGELOG.md | 3 ++- src/biocframe/BiocFrame.py | 4 ++++ tests/test_initialize.py | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf135e..8cf7018 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## Version 0.7.0 - 0.7.2 +## Version 0.7.0 - 0.7.3 - Major update to type hints throughout the module for better type safety and consistency. - Fixed bug in slice operations where column indices might be incorrectly initialized. @@ -9,6 +9,7 @@ - Implement empty, contains, head, tail, - Coercions to list and `NamedList` from bioctuls. - `BiocFrame` now extends `BiocObject`, with metadata attribute now a `NamedList` from the biocutils package. +- Support creating frame with 0 rows but has column names. ## Version 0.6.3 diff --git a/src/biocframe/BiocFrame.py b/src/biocframe/BiocFrame.py index 6770a0c..2a36eaa 100644 --- a/src/biocframe/BiocFrame.py +++ b/src/biocframe/BiocFrame.py @@ -212,6 +212,10 @@ def __init__( else: self._column_names = column_names if isinstance(column_names, ut.Names) else ut.Names(column_names) + if self._number_of_rows == 0 and len(self._column_names) > 0 and len(self._data) == 0: + for col in self._column_names: + self._data[col] = [] + self._column_data = column_data if _validate: diff --git a/tests/test_initialize.py b/tests/test_initialize.py index ecce83b..311fa23 100644 --- a/tests/test_initialize.py +++ b/tests/test_initialize.py @@ -73,6 +73,14 @@ def test_empty_obj_with_size(): assert bframe is not None +def test_empty_with_column_names(): + bframe = BiocFrame(column_names=["A", "B"]) + assert bframe.shape == (0, 2) + assert list(bframe.column_names) == ["A", "B"] + assert len(bframe.column("A")) == 0 + assert len(bframe.column("B")) == 0 + + def test_should_fail(): with pytest.raises(Exception): df_gr = pd.DataFrame(