Skip to content

BUG: DataFrame._get_value not working for IntervalIndex #27927

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
jorisvandenbossche opened this issue Aug 15, 2019 · 5 comments
Closed

BUG: DataFrame._get_value not working for IntervalIndex #27927

jorisvandenbossche opened this issue Aug 15, 2019 · 5 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Interval Interval data type

Comments

@jorisvandenbossche
Copy link
Member

Follow-up on #27926 and #27865

DataFrame._get_value with an IntervalIndex is not working properly:

In [24]:         df = DataFrame( 
    ...:             np.random.randn(5, 2), 
    ...:             columns=["a", "b"], 
    ...:             index=pd.IntervalIndex.from_breaks([-np.inf, 0, 1, 2, 3, np.inf]), 
    ...:         )  

In [26]: df._get_value(df.index[0], 'a')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-ce242a095ada> in <module>
----> 1 df._get_value(df.index[0], 'a')

~/scipy/pandas/pandas/core/frame.py in _get_value(self, index, col, takeable)
   2953 
   2954         try:
-> 2955             return engine.get_value(series._values, index)
   2956         except KeyError:
   2957             # GH 20629

AttributeError: 'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'

This _get_value is used in ix which is deprecated, but also in at which is not deprecated:

In [27]: df.at[df.index[0], 'a']
...
AttributeError: 'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'
@jorisvandenbossche jorisvandenbossche added Bug Indexing Related to indexing on series/frames, not to indexes themselves Interval Interval data type labels Aug 15, 2019
@jorisvandenbossche jorisvandenbossche added this to the Contributions Welcome milestone Aug 15, 2019
@another-green
Copy link
Contributor

I will give a try

@jbrockmendel
Copy link
Member

cc @jschendel i ran into this recently, too. any idea what it would take to implement IntervalTree.get_value?

@jschendel
Copy link
Member

I think IntervalTree.get_value would be non-trivial to implement since it'd have to work for both Interval keys and numeric keys, and the get_loc logic for this is all currently at the IntervalIndex level.

That being said, it looks like adding AttributeError to the except clause in DataFrame._get_value provides a workaround:

diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index 676b78573..30bda9775 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -2898,7 +2898,7 @@ class DataFrame(NDFrame):
             if self.index.nlevels > 1:
                 # partial indexing forbidden
                 raise
-        except (TypeError, ValueError):
+        except (AttributeError, TypeError, ValueError):
             pass
 
         # we cannot handle direct indexing

With the diff applied:

In [2]: df = pd.DataFrame( 
   ...:     np.arange(9).reshape(3,3), 
   ...:     columns=list('ABC'), 
   ...:     index=pd.interval_range(0, 3) 
   ...: )

In [3]: df
Out[3]: 
        A  B  C
(0, 1]  0  1  2
(1, 2]  3  4  5
(2, 3]  6  7  8

In [4]: df.at[df.index[0], 'A']
Out[4]: 0

In [5]: df.at[df.index[1], 'B']
Out[5]: 4

In [6]: df.at[0.5, 'C']
Out[6]: 2

@jbrockmendel
Copy link
Member

Would the issue be resolved if instead of calling index._engine.get_value we used index.get_value?

@jbrockmendel
Copy link
Member

closed by #41846

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Interval Interval data type
Projects
None yet
Development

No branches or pull requests

4 participants