-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: indexing support for reversed is_monotonic #7860
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
Comments
always you can use:
its not clear what:
For an integer index, its clear, because the end-points are included. |
Sorry, here's In [4]: pd.show_versions() INSTALLED VERSIONScommit: None pandas: 0.14.1 Thanks for the solution. I'll use it for sure, but the ix behavior should work right? This is such a common index type in spectral data, I'd hate to require a seperate slice call for this use case. Although, if it's not likely to be changed in the future, I could probably just add my own slice functions that bury this under the hood unbeknownst to users. What do you recommend? |
pls review docs here as well: http://pandas.pydata.org/pandas-docs/stable/indexing.html#float64index I think this was not implemented because its not 'cheap'. In the sense that it would work if you knew that the index was monotonic, but reversed (iow would need to have a |
Well, that makes sense, thanks. I'll either make my own slice wrapper, or On Mon, Jul 28, 2014 at 3:33 PM, jreback [email protected] wrote:
Adam Hughes |
we'll put it on the enhancement list. if you are interested in implemented, step up! |
Alright, thanks. I would take a crack, but really feel like I don't know the pandas code base well enough to guarantee my solution will do more good than harm. |
I'm considering taking a crack at this, but there's one edge case I would like to clarify first. In particular: how do we want to handle slices with mis-matched ordering, e.g., Keeping track of whether an index is descending or ascending is one of those details that's nice to keep track of for the user, so it would be nice if these "just work" by switching Can anyone think of unfortunate consequences to this sort of interchanging? |
@shoyer you can add to the Then I think you could easily just swap the start stop in those caes. |
@jreback Excellent, I'll take a look. I'd like this to work for |
you can do for all types - just change the template |
I just wanted to point out that I did use @jreback suggestion for the boolean experssion and just put that into my getitem() indexer calls somewhere, and haven't encountered any problems since. This is probably a hacky solution, but for my use case, works fine. Can I ask how monotonicity is determined? Are all values inspected, or just the start and final? And does |
Here is where This should be an easy fix to extend to identify descending indexes. It does indeed check all values (when necessary). The advantage to using slice syntax is it uses numpy.ndarray views instead of making copies, so it's much faster. Also, various scientific file formats (e.g., netCDF, HDF5, OpenDAP) support reading slices directly but are much slower or have more limited support for array indexing. The later will be handy for xray, and it will get that for free when I add this to pandas. |
I'd expect the first example to work basically as x.iloc[x.index.searchsorted(10): x.index.searchsorted(30, side='right') + 1] with an obvious optimization potential of doing |
The logic would go like this: if the integer indexers (This would probably end up in I doubt there many cases where users are relying on slicing returning a size 0 object due to enforcement of this ordering, but I could certainly be wrong. |
Hmm... this could get pretty complex/unpredictable depending on whether |
It may be me, but I see slicing as selecting values by position between lbound and ubound, with pandas being so kind to enable me writing bounds as labels rather than actual positions. If OTOH you need all values between lbound and ubound value-wise you should either write the condition |
@immerrr OK, I think I am convinced. +1 for the idea of |
conceptually not hard (and you can look at a the slices to figure this out, e.g. if start >= end or start>last_endpoint, you can just do a reversed is_monotonic), to avoid a perf hit I think, then just reverse the searching operations for slices would need to do
is_monotonic_decreasing
here : https://github.com/pydata/pandas/blob/master/pandas/core/index.py#L1764Hello,
I am working with spectral data, which for various spectral units such as
wavenumber, is often presented with decreasing spectral values along the
index. For example:
http://www.chemguide.co.uk/analysis/ir/irpropanone.GIF
In my dataframe, the index is stored in descending order (eg 500, 499,
498... 2, 1); however, when I try to slice using .ix[]; it becomes
impossible, giving me a long key error.
Likewise, df.plot() is sorting the xvalues from low to high, so I need to
reverse the plot axis after the fact. Not really a big deal, but wondered
if there's a better workaround.
Any suggestions?
Note: This behavior works fine for int64 index:
But fails for float index
With error:
The text was updated successfully, but these errors were encountered: