From e8b3c8e336c54c6a02ca6907655345c9882130c5 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 6 Jul 2021 21:32:34 -0400 Subject: [PATCH 1/2] chore: bump boost-histogram --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 02e843fe..2bfc1c03 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,7 +42,7 @@ project_urls = [options] packages = find: install_requires = - boost-histogram~=1.0.2 + boost-histogram~=1.1.0 histoprint>=1.6 numpy>=1.13.3 typing_extensions;python_version<"3.8" From ddecb2433a214c752c61e7ab76d468fc59b1f363 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 6 Jul 2021 23:11:17 -0400 Subject: [PATCH 2/2] feat: support list selection from boost-histogram 1.1 --- src/hist/basehist.py | 10 +++++++--- tests/test_general.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/hist/basehist.py b/src/hist/basehist.py index 004e61f1..3b34d894 100644 --- a/src/hist/basehist.py +++ b/src/hist/basehist.py @@ -225,7 +225,9 @@ def _loc_shortcut(self, x: Any) -> Any: Convert some specific indices to location. """ - if isinstance(x, slice): + if isinstance(x, list): + return [self._loc_shortcut(each) for each in x] + elif isinstance(x, slice): return slice( self._loc_shortcut(x.start), self._loc_shortcut(x.stop), @@ -256,7 +258,9 @@ def _step_shortcut(self, x: Any) -> Any: else: return bh.rebin(int(x.imag)) - def _index_transform(self, index: IndexingExpr) -> bh.IndexingExpr: + def _index_transform( + self, index: Union[List[IndexingExpr], IndexingExpr] + ) -> bh.IndexingExpr: """ Auxiliary function for __getitem__ and __setitem__. """ @@ -274,7 +278,7 @@ def _index_transform(self, index: IndexingExpr) -> bh.IndexingExpr: ) return new_indices - elif not hasattr(index, "__iter__"): + elif not isinstance(index, tuple): index = (index,) # type: ignore return tuple(self._loc_shortcut(v) for v in index) # type: ignore diff --git a/tests/test_general.py b/tests/test_general.py index bd185121..21fd3172 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -867,3 +867,25 @@ def test_sum_empty_axis_hist(): assert h.sum().value == 0 assert "Str" in repr(h) h._repr_html_() + + +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") +def test_select_by_index(): + h = Hist( + axis.StrCategory(["a", "two", "3"]), + storage=storage.Weight(), + ) + + assert tuple(h[["a", "3"]].axes[0]) == ("a", "3") + assert tuple(h[["a"]].axes[0]) == ("a",) + + +@pytest.mark.filterwarnings("ignore:List indexing selection is experimental") +def test_select_by_index_imag(): + h = Hist( + axis.IntCategory([7, 8, 9]), + storage=storage.Int64(), + ) + + assert tuple(h[[2, 1]].axes[0]) == (9, 8) + assert tuple(h[[8j, 7j]].axes[0]) == (8, 7)