Skip to content

Commit aeb98ff

Browse files
committed
address coderabbit review
1 parent 78a496f commit aeb98ff

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/cytodataframe/frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,10 @@ def _return_cytodataframe(
459459
cdf = self._build_result_cdf(result)
460460
# If the method name is transpose we know that
461461
# the dataframe has been transposed.
462-
if method_name == "transpose" and not self._custom_attrs["is_transposed"]:
463-
cdf._custom_attrs["is_transposed"] = True
462+
if method_name == "transpose":
463+
cdf._custom_attrs["is_transposed"] = not self._custom_attrs[
464+
"is_transposed"
465+
]
464466

465467
return cdf
466468

tests/test_frame.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,9 @@ def test_return_cytodataframe(cytotable_NF1_data_parquet_shrunken: str):
796796
assert isinstance(cdf.tail(), CytoDataFrame)
797797
assert isinstance(cdf.sort_values(by="Metadata_ImageNumber"), CytoDataFrame)
798798
assert isinstance(cdf.sample(n=5), CytoDataFrame)
799+
assert isinstance(cdf[0:2], CytoDataFrame)
800+
assert isinstance(cdf[1:1], CytoDataFrame)
801+
assert isinstance(cdf[0:5:2], CytoDataFrame)
799802
assert isinstance(cdf.iloc[0:2], CytoDataFrame)
800803
assert isinstance(cdf.iloc[1:1], CytoDataFrame)
801804
assert isinstance(cdf.iloc[0:5:2], CytoDataFrame)
@@ -816,9 +819,25 @@ def test_iloc_slice_preserves_cytodataframe_html_formatting():
816819

817820
cdf = CytoDataFrame(pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}))
818821

822+
bracket_sliced = cdf[0:3:2]
823+
bracket_empty_sliced = cdf[1:1]
819824
sliced = cdf.iloc[0:3:2]
820825
empty_sliced = cdf.iloc[1:1]
821826

827+
assert isinstance(bracket_sliced, CytoDataFrame)
828+
assert isinstance(bracket_empty_sliced, CytoDataFrame)
829+
assert bracket_sliced._custom_attrs["_output"] is cdf._custom_attrs["_output"]
830+
assert (
831+
bracket_sliced._custom_attrs["_widget_state"]
832+
is cdf._custom_attrs["_widget_state"]
833+
)
834+
assert (
835+
bracket_empty_sliced._custom_attrs["_output"] is cdf._custom_attrs["_output"]
836+
)
837+
assert (
838+
bracket_empty_sliced._custom_attrs["_widget_state"]
839+
is cdf._custom_attrs["_widget_state"]
840+
)
822841
assert isinstance(sliced, CytoDataFrame)
823842
assert isinstance(empty_sliced, CytoDataFrame)
824843
assert sliced._custom_attrs["_output"] is cdf._custom_attrs["_output"]
@@ -828,10 +847,24 @@ def test_iloc_slice_preserves_cytodataframe_html_formatting():
828847
empty_sliced._custom_attrs["_widget_state"]
829848
is cdf._custom_attrs["_widget_state"]
830849
)
850+
assert "background:#EBEBEB" in bracket_sliced._repr_html_(debug=True)
851+
assert "background:#EBEBEB" in bracket_empty_sliced._repr_html_(debug=True)
831852
assert "background:#EBEBEB" in sliced._repr_html_(debug=True)
832853
assert "background:#EBEBEB" in empty_sliced._repr_html_(debug=True)
833854

834855

856+
def test_transpose_toggles_transposed_state() -> None:
857+
"""Ensure repeated transposes flip the transposed rendering state back."""
858+
859+
cdf = CytoDataFrame(pd.DataFrame({"a": [1, 2], "b": [3, 4]}))
860+
861+
transposed = cdf.T
862+
double_transposed = transposed.T
863+
864+
assert transposed._custom_attrs["is_transposed"] is True
865+
assert double_transposed._custom_attrs["is_transposed"] is False
866+
867+
835868
def test_cytodataframe_dynamic_width_and_height(
836869
cytotable_NF1_data_parquet_shrunken: str,
837870
):

0 commit comments

Comments
 (0)