Repository navigation
change IGroup __repr__ #447
Copy link
Copy link
Open
Labels
Description
If we want to return IGroup instead of LGroup for stuff like Axis/Group.startingwith,endingwith,... (see #438) we need to change IGroup __repr__ to somehow display the label the position corresponds to. Otherwise it makes it harder than necessary to debug any code using them. The problem is that I haven't found a way to add this without breaking the eval(repr(v)) == v which is unfortunate.
>>> a = Axis('a=a0..a5')
>>> # current situation
>>> a["a1,a3,a4"]
a['a1', 'a3', 'a4']
>>> a.i[1, 3, 4]
a.i[1, 3, 4]
>>> # alternative 1
>>> a.i[1, 3, 4]
a.i['a1' (1), 'a3' (3), 'a4' (4)]
>>> # alternative 2 -- this is more "pure" but looks less pretty
>>> a.i[1, 3, 4]
a.i[1 ('a1'), 3 ('a3'), 4 ('a4')]
>>> # alternative 3
>>> a.i[1, 3, 4]
a.i[1, 3, 4 ('a1', 'a3', 'a4')]
>>> # alternative 4
>>> a.i[1, 3, 4]
a.i[1, 3, 4] ('a1', 'a3', 'a4')
>>> # alternative 5 -- this is even more "pure" (we could make it evaluate correctly) but looks even less pretty
>>> a.i[1, 3, 4]
IGroup([1, 3, 4], axis=a, display=['a1', 'a3', 'a4'])There are potentially many other alternatives. Please suggest them if you have any idea.
Two special problems
- positional slices, for which the stop bound is not inclusive, which means that slices ending at the end of the axis are problematic (and other slices are unintuitive)
>>> a.i[1:6]
a.i[1 ('a1'): 6 (??)]
>>> # having 'a3' displayed (but not included) makes it more confusing
>>> a.i[1:3]
a.i[1 ('a1'): 3 ('a3')]
>>> # maybe doing a special case for slices would make sense (ie do not translate each bound separately but the whole "key":
>>> a.i[1:3]
a.i[1:3 ('a1':'a2')]- slices on axes with integer labels (especially if pos == label) are a bit confusing (but they are probably already confusing even though the confusion is hidden ;-)).
>>> a = Axis('a=0..10')
>>> # current
>>> a.i[1:3]
a.i[1:3]
>>> # each bound separately
>>> a.i[1:3]
a.i[1 (1): 3 (3)]
>>> # the whole key
>>> a.i[1:3]
a.i[1:3 (1:2)]My current preference goes for alternative 4 (because it will be easier to copy-paste than alternative 3) and for slices, to translate the whole key instead of each bound separately.
Reactions are currently unavailable
Activity
gdementen commentedon Sep 25, 2017
Here is a concrete example when you would get an IGroup without explicitly asking for it:
gdementen commentedon Sep 25, 2017
Yet another option is to blur the difference between IGroup and LGroup and only display the position in the presence of duplicate labels:
alixdamman commentedon Sep 25, 2017
My first choice is also
alternative 4(which, indeed, allows easy copy/paste) and to translate the whole key instead of each bound separately for slices.I wouldn't "blur" the difference between PGroup and LGroup. It's good to know when you are dealing with PGroup or LGroup. Explicit is better than implicit.
[-]change PGroup __repr__[/-][+]change IGroup __repr__[/+]gdementen commentedon Mar 31, 2026
I wonder if it wouldn't be simpler to just ensure we have no duplicate label on Axis construction (it would incur a performance penalty but I assume it would be very small -- or even negligible in our models I think) by deduplicating them:
Something like:
or maybe start counting at 1??? Unsure what would be least confusing. See what Pandas does for columns.
Then we can always use IGroups (except for X.) but display them like LGroups