Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement some way to get the label before or after another label #1122

Open
gdementen opened this issue Nov 5, 2024 · 0 comments
Open

implement some way to get the label before or after another label #1122

gdementen opened this issue Nov 5, 2024 · 0 comments

Comments

@gdementen
Copy link
Contributor

Unsure what the best API for this would be. It would be pretty easy to add this as Axis methods (e.g. before and after).

For vs who needed this functionality (but she is not the first user who needed something like this), I used the following code:

def label_before(axis, label, n=1):
    target_label_idx = axis.index(label) - n
    if target_label_idx < 0:
        plural_suffix = "s" if n > 1 else ""
        raise ValueError(f"{n} position{plural_suffix} before {label!r} is before the start of the '{axis}' axis")
    return axis.i[target_label_idx]

def label_after(axis, label, n=1):
    target_label_idx = axis.index(label) + n
    if target_label_idx >= len(axis):
        plural_suffix = "s" if n > 1 else ""
        raise ValueError(f"{n} position{plural_suffix} after {label!r} is after the end of the '{axis}' axis")
    return axis.i[target_label_idx]

Moving those to axis methods would be trivial. But I am now wondering:

  • couldn't this be achieved using shift? Isn't this the symptom that shift is hard to use?
  • is there a way to express this using the string syntax?
  • how will those new methods behave when using X. syntax?

The last two could come later though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant