Skip to content

ENH: Forward & back fill methods #1651

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

Closed
max-sixty opened this issue Oct 23, 2017 · 7 comments
Closed

ENH: Forward & back fill methods #1651

max-sixty opened this issue Oct 23, 2017 · 7 comments

Comments

@max-sixty
Copy link
Collaborator

I think with np.flip and bn.push, this should be simple. They're both fairly new and so would require version checks / upgrading the minimums.

One small issue, I wonder if anyone has come across this: bottleneck returns the numpy array rather than the DataArray - is that because it's not operating with the correct numpy interface?

Forward fill:

array.values = bn.push(array.values, axis=array.get_axis_num(axis_name))

Backfill:

axis = array.get_axis_num(axis_name)

# reverse for bfill
array = np.flip(array, axis=axis)

# fill 
array.values = bn.push(array.values, axis=axis)

# reverse back to original
result = np.flip(scaling, axis=date_axis)
@shoyer
Copy link
Member

shoyer commented Oct 23, 2017

One small issue, I wonder if anyone has come across this: bottleneck returns the numpy array rather than the DataArray - is that because it's not operating with the correct numpy interface?

Can you explain? I don't think bottleneck is xarray aware, so I'm not surprised by this. NumPy doesn't have a generic interface for external functions (only ufuncs).

@max-sixty
Copy link
Collaborator Author

Right, but numpy functions return the original type?

In [6]: da=xr.DataArray(np.random.rand(2,4))

In [12]: np.sum(da, axis=1)
Out[12]:
<xarray.DataArray (dim_0: 2)>
array([ 0.880766,  2.058156])
Dimensions without coordinates: dim_0


In [14]: np.flip(da, 1)
Out[14]:
<xarray.DataArray (dim_0: 2, dim_1: 4)>
array([[ 0.279932,  0.307569,  0.291855,  0.001411],
       [ 0.681486,  0.556972,  0.083212,  0.736487]])
Dimensions without coordinates: dim_0, dim_1

@shoyer
Copy link
Member

shoyer commented Oct 24, 2017

Right, but numpy functions return the original type?

Not quite -- many numpy functions check for a method of the same name on their argument and call it instead of the numpy routine. If you look at the source of np.sum, it first checks for a sum method on its argument and calls that method instead if found. Otherwise, it coerces the argument to a numpy array and calls the numpy method.

@shoyer
Copy link
Member

shoyer commented Oct 24, 2017

Other numpy functions, e.g., flip check for some attributes and then assume duck type compatibility if found. For example, flip is turned into an indexing call.

@max-sixty
Copy link
Collaborator Author

Ah, that explains a lot. Thanks for the clarification.

@jhamman
Copy link
Member

jhamman commented Oct 24, 2017

@MaximilianR - I'm a big +1 on these features. Pandas has a missing module. I think these methods, combined with the interpolation methods I'm working on in #1640 would cover a large chunk of our use cases.

@max-sixty
Copy link
Collaborator Author

Closed by #1640

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

3 participants