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

add possibility to write ts[start:end] = v to change value on an interval #40

Closed
sdementen opened this issue Sep 27, 2016 · 4 comments
Closed

Comments

@sdementen
Copy link
Contributor

I have a use case where I need to change the value of a timeseries on an interval without changing the value outside of the interval, ie do something like ts[start:end] = value.
Just setting

ts[end] = ts[end]   # freezing/anchoring the current value of ts as of [end, ...)
ts[start] = value      # changing the value as of [start, ...)

may fail as intermediate points in [start,end) may exist ==> we need to remove all intermediate points (which is easy as ts.iterperiods(start,end) provides them nicely).

I think the function below does it properly (but it would be better integrated in the item to use the slice notation)

def set_slice(ts, start, end, value):
    """
   ts[start:end] = value ==> call set_slice(ts, start, end, value)
    Set the value of the ts so that
    - on the interval [start, end) we have the new value
    - on [end, ...) we haven't change the value
    - on (..., start) we haven't change the value neither
    We replace the value of the ts on an interval.

    :param ts: 
    :param start: 
    :param end: 
    :param value: 
    :return: 
    """
    # for each interval to render
    for i, (s, e, v) in enumerate(list(ts.iterperiods(start, end))):
        # look at all intervals included in the current interval
        # (always at least 1)
        if i == 0:
            # if the first, set initial value to new value of range
            ts[s] = value
        else:
            # otherwise, remove intermediate key
            del ts[s]
    # finish by setting the end of the interval to the previous value
    ts[end] = v
@stringertheory
Copy link
Owner

Good idea! I can see how that could be useful in some cases. I'd welcome a pull request (especially if you write a couple of accompanying tests) — I can also add something like this in, though it might not be for a week or two.

I think it might be good to include a compact=False keyword argument, too, to keep consistent with set and to specify whether the endpoints of the interval will always be included as measurements.

@sdementen
Copy link
Contributor Author

I made PR #41

@sdementen
Copy link
Contributor Author

I have cleaned the PR to fix pep8 issues. Any chance for merge any time soon ?

@stringertheory
Copy link
Owner

Thanks @sdementen! I'll merge it in sometime this week when I get a chance

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

No branches or pull requests

2 participants