Skip to content

Commit 31f3a16

Browse files
committed
FEAT: changed the warning when targeting a label in an aggregated array using the group that created it to an error.
1 parent 2506e10 commit 31f3a16

File tree

3 files changed

+86
-69
lines changed

3 files changed

+86
-69
lines changed

doc/source/changes/version_0_35.rst.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Backward incompatible changes
2727
:py:obj:`CheckedArray` now requires installing pydantic >= 2
2828
(closes :issue:`1075`).
2929

30+
* the warning introduced in LArray 0.34 (see :issue:`994`) when targeting a
31+
label in an aggregated array using the group that created it, has been
32+
changed to an error. The aggregated array label should be used instead.
33+
This was a seldom used feature which was complex to keep working and had a
34+
significant performance cost in some cases, even when the feature is not
35+
used.
36+
3037

3138
Future breaking changes
3239
^^^^^^^^^^^^^^^^^^^^^^^

larray/core/axis.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ def _retarget_warn_msg(key, real_axis, current_eval=None, future_eval=None):
111111
)
112112

113113

114+
_GROUP_AS_AGGREGATED_LABEL_MSG_TEMPLATE = \
115+
"Using a Group object which was used to create an aggregate to " \
116+
"target its aggregated label is deprecated. " \
117+
"Please use the aggregated label directly instead. " \
118+
"In this case, you should use {potential_tick!r} instead of " \
119+
"using {key!r}."
120+
121+
122+
def _group_as_aggregated_label_msg(key, potential_tick=None):
123+
if potential_tick is None:
124+
potential_tick = _to_tick(key)
125+
return _GROUP_AS_AGGREGATED_LABEL_MSG_TEMPLATE.format(
126+
potential_tick=potential_tick,
127+
key=key
128+
)
129+
130+
114131
class Axis(ABCAxis):
115132
r"""
116133
Represents an axis. It consists of a name and a list of labels.
@@ -933,16 +950,7 @@ def isscalar(k):
933950
if (not isinstance(self, AxisReference)
934951
and key.axis.name == self.name
935952
and key.name in self):
936-
msg = (
937-
"Using a Group object which was used to create an "
938-
"aggregate to target its aggregated label is deprecated. "
939-
"Please use the aggregated label directly instead. "
940-
f"In this case, you should use {key.name!r} instead of "
941-
f"using {key!r}."
942-
)
943-
# let us hope the stacklevel does not vary by codepath
944-
warnings.warn(msg, FutureWarning, stacklevel=7)
945-
return LGroup(key.name, None, self)
953+
raise ValueError(_group_as_aggregated_label_msg(key, key.name))
946954
# retarget a Group from another axis to this axis
947955
# TODO: uncomment this code once we actually retarget groups from
948956
# other axes in LGroup.__init__
@@ -1027,14 +1035,12 @@ def index(self, key) -> Union[int, np.ndarray, slice]:
10271035
try:
10281036
res_idx = mapping[potential_tick]
10291037
if potential_tick != key.key:
1030-
# only warn if no KeyError was raised (potential_tick is in mapping)
1031-
msg = "Using a Group object which was used to create an aggregate to " \
1032-
"target its aggregated label is deprecated. " \
1033-
"Please use the aggregated label directly instead. " \
1034-
f"In this case, you should use {potential_tick!r} instead of " \
1035-
f"using {key!r}."
1036-
# let us hope the stacklevel does not vary by codepath
1037-
warnings.warn(msg, FutureWarning, stacklevel=8)
1038+
raise ValueError(
1039+
_group_as_aggregated_label_msg(
1040+
key,
1041+
potential_tick
1042+
)
1043+
)
10381044
return res_idx
10391045
except KeyError:
10401046
pass

larray/tests/test_array.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from_lists, from_string, from_frame, from_series,
2121
zip_array_values, zip_array_items, nan_to_num)
2222
from larray.core.axis import (
23-
_to_ticks, _to_tick, _to_key, _retarget_warn_msg
23+
_to_ticks, _to_key, _retarget_warn_msg, _group_as_aggregated_label_msg
2424
)
2525
from larray.util.misc import LHDFStore
2626

@@ -33,16 +33,6 @@
3333
# E241: multiple spaces after ','
3434

3535

36-
GROUP_AS_AGGREGATED_LABEL_MSG_TEMPLATE = "Using a Group object which was used to create an aggregate to " \
37-
"target its aggregated label is deprecated. " \
38-
"Please use the aggregated label directly instead. " \
39-
"In this case, you should use {potential_tick!r} instead of " \
40-
"using {key!r}."
41-
42-
def group_as_aggregated_label_msg(key):
43-
return GROUP_AS_AGGREGATED_LABEL_MSG_TEMPLATE.format(potential_tick=_to_tick(key), key=key)
44-
45-
4636
# ================== #
4737
# Test Value Strings #
4838
# ================== #
@@ -2471,26 +2461,32 @@ def test_getitem_on_group_agg(array):
24712461
# using an anonymous LGroup
24722462
lg1 = b[b_group1]
24732463
agg_arr = array.sum(a, c).sum(b=(lg1, b_group2, b_group2, all_b))
2474-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(lg1), num_expected=5):
2475-
# the following should all be equivalent
2476-
assert agg_arr[lg1].shape == (6,)
2477-
assert agg_arr[(lg1,)].shape == (6,)
2478-
# these last three are only syntactic sugar differences
2479-
# (__getitem__ receives the *exact* same key)
2480-
assert agg_arr[(lg1, slice(None))].shape == (6,)
2481-
assert agg_arr[lg1, slice(None)].shape == (6,)
2482-
assert agg_arr[lg1, :].shape == (6,)
2464+
msg = _group_as_aggregated_label_msg(lg1)
2465+
# the following should all be equivalent
2466+
with must_raise(ValueError, msg=msg):
2467+
agg_arr[lg1]
2468+
with must_raise(ValueError, msg=msg):
2469+
agg_arr[(lg1,)]
2470+
with must_raise(ValueError, msg=msg):
2471+
agg_arr[(lg1, slice(None))]
2472+
# only syntactic sugar differences (__getitem__ gets *exact* same key)
2473+
# agg_arr[lg1, slice(None)]
2474+
# agg_arr[lg1, :]
24832475

24842476
# using a named LGroup
24852477
lg1 = b[b_group1] >> 'g1'
24862478
agg_arr = array.sum(a, c).sum(b=(lg1, b_group2, b_group2, all_b))
2487-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(lg1), num_expected=5):
2488-
# the following are all equivalent
2489-
assert agg_arr[lg1].shape == (6,)
2490-
assert agg_arr[(lg1,)].shape == (6,)
2491-
assert agg_arr[(lg1, slice(None))].shape == (6,)
2492-
assert agg_arr[lg1, slice(None)].shape == (6,)
2493-
assert agg_arr[lg1, :].shape == (6,)
2479+
msg = _group_as_aggregated_label_msg(lg1)
2480+
# the following should all be equivalent
2481+
with must_raise(ValueError, msg=msg):
2482+
agg_arr[lg1]
2483+
with must_raise(ValueError, msg=msg):
2484+
agg_arr[(lg1,)]
2485+
with must_raise(ValueError, msg=msg):
2486+
agg_arr[(lg1, slice(None))]
2487+
# only syntactic sugar differences (__getitem__ gets *exact* same key)
2488+
# agg_arr[lg1, slice(None)]
2489+
# agg_arr[lg1, :]
24942490

24952491

24962492
def test_getitem_on_group_agg_nokw(array):
@@ -2512,24 +2508,32 @@ def test_getitem_on_group_agg_nokw(array):
25122508
# using an anonymous LGroup
25132509
lg1 = b[b_group1]
25142510
agg_arr = array.sum(a, c).sum((lg1, b_group2, b_group3, all_b))
2515-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(lg1), num_expected=5):
2516-
# the following are all equivalent
2517-
assert agg_arr[lg1].shape == (6,)
2518-
assert agg_arr[(lg1,)].shape == (6,)
2519-
assert agg_arr[(lg1, slice(None))].shape == (6,)
2520-
assert agg_arr[lg1, slice(None)].shape == (6,)
2521-
assert agg_arr[lg1, :].shape == (6,)
2511+
# the following are all equivalent
2512+
msg = _group_as_aggregated_label_msg(lg1)
2513+
with must_raise(ValueError, msg=msg):
2514+
agg_arr[lg1]
2515+
with must_raise(ValueError, msg=msg):
2516+
agg_arr[(lg1,)]
2517+
with must_raise(ValueError, msg=msg):
2518+
agg_arr[(lg1, slice(None))]
2519+
# only syntactic sugar differences (__getitem__ gets *exact* same key)
2520+
# agg_arr[lg1, slice(None)]
2521+
# agg_arr[lg1, :]
25222522

25232523
# using a named LGroup
25242524
lg1 = b[b_group1] >> 'g1'
25252525
agg_arr = array.sum(a, c).sum((lg1, b_group2, b_group3, all_b))
2526-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(lg1), num_expected=5):
2527-
# the following are all equivalent
2528-
assert agg_arr[lg1].shape == (6,)
2529-
assert agg_arr[(lg1,)].shape == (6,)
2530-
assert agg_arr[(lg1, slice(None))].shape == (6,)
2531-
assert agg_arr[lg1, slice(None)].shape == (6,)
2532-
assert agg_arr[lg1, :].shape == (6,)
2526+
msg = _group_as_aggregated_label_msg(lg1)
2527+
# the following are all equivalent
2528+
with must_raise(ValueError, msg=msg):
2529+
agg_arr[lg1]
2530+
with must_raise(ValueError, msg=msg):
2531+
agg_arr[(lg1,)]
2532+
with must_raise(ValueError, msg=msg):
2533+
agg_arr[(lg1, slice(None))]
2534+
# only syntactic sugar differences (__getitem__ gets *exact* same key)
2535+
# agg_arr[lg1, slice(None)]
2536+
# agg_arr[lg1, :]
25332537

25342538

25352539
def test_filter_on_group_agg(array):
@@ -2543,8 +2547,8 @@ def test_filter_on_group_agg(array):
25432547
# using a named LGroup
25442548
g1 = b[b_group1] >> 'g1'
25452549
agg_arr = array.sum(a, c).sum(b=(g1, b_group2, b_group3, all_b))
2546-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(g1)):
2547-
assert agg_arr.filter(b=g1).shape == (6,)
2550+
with must_raise(ValueError, msg=_group_as_aggregated_label_msg(g1)):
2551+
agg_arr.filter(b=g1)
25482552

25492553
# Note that agg_arr.filter(b=(g1,)) does NOT work. It might be a
25502554
# little confusing for users, because agg_arr[(g1,)] works but it is
@@ -2567,8 +2571,9 @@ def test_filter_on_group_agg(array):
25672571
# assert bya.filter(a=':17').shape == (12, 2, 6)
25682572

25692573
bya = array.sum(a=(a0to5_named, 5, a6to13, a14plus))
2570-
with must_warn(FutureWarning, msg=group_as_aggregated_label_msg(a0to5_named)):
2571-
assert bya.filter(a=a0to5_named).shape == (12, 2, 6)
2574+
msg = _group_as_aggregated_label_msg(a0to5_named)
2575+
with must_raise(ValueError, msg=msg):
2576+
bya.filter(a=a0to5_named)
25722577

25732578

25742579
def test_sum_several_lg_groups(array):
@@ -2583,12 +2588,11 @@ def test_sum_several_lg_groups(array):
25832588

25842589
# the result is indexable
25852590
# 1.a) by LGroup
2586-
msg1 = group_as_aggregated_label_msg(lg1)
2587-
with must_warn(FutureWarning, msg=msg1):
2588-
assert agg_arr.filter(b=lg1).shape == (19, 2, 6)
2589-
msg2 = group_as_aggregated_label_msg(lg2)
2590-
with must_warn(FutureWarning, msg=(msg1, msg2), check_file=False):
2591-
assert agg_arr.filter(b=(lg1, lg2)).shape == (19, 2, 2, 6)
2591+
msg = _group_as_aggregated_label_msg(lg1)
2592+
with must_raise(ValueError, msg=msg):
2593+
agg_arr.filter(b=lg1)
2594+
with must_raise(ValueError, msg=msg):
2595+
agg_arr.filter(b=(lg1, lg2))
25922596

25932597
# 1.b) by string (name of groups)
25942598
assert agg_arr.filter(b='lg1').shape == (19, 2, 6)

0 commit comments

Comments
 (0)