Skip to content

ENH: allow dataframe get to take an axis argument #11550

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
wants to merge 3 commits into from
Closed

ENH: allow dataframe get to take an axis argument #11550

wants to merge 3 commits into from

Conversation

nbonnotte
Copy link
Contributor

closes #6703

I would have like to have a solution also working for panels, but I've never used them, so I'm not sure how it should behave.

@@ -577,6 +577,30 @@ def _repr_html_(self):
else:
return None

def get(self, key, default=None, axis='columns'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just modify the one in core/generic.py

@jreback jreback added Enhancement Indexing Related to indexing on series/frames, not to indexes themselves labels Nov 8, 2015
@nbonnotte
Copy link
Contributor Author

Ok, I used your suggestion and put the tests in tests/test_generic.py... but it still is the same test. I should probably make more tests, in particular to test what happens for Panel.

(Sorry, Atom couldn't help but by itself remove some spaces that were at the end of their lines)

@nbonnotte
Copy link
Contributor Author

I don't understand, why is the build failing?

@max-sixty
Copy link
Contributor

If you go to 'Details', you can get the build's output. In this case:

======================================================================
FAIL: test_getitem_get (pandas.tests.test_series.TestSeries)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/pydata/pandas/pandas/tests/test_series.py", line 1339, in test_getitem_get
    self.series.get(-1), self.series.get(self.series.index[-1]))
AssertionError: None != -0.2267325702945841

@nbonnotte
Copy link
Contributor Author

Uh, yes, thanks.

I think I've identified what is going on, but I'm not sure on how to proceed:

In [56]: pd.Series([0,1], index=[0,1]).get(-1)

In [57]: pd.Series([0,1], index=['a','b']).get(-1)
Out[57]: 1

So, basically, the result of the get method for a series depends on the type of the index... Which is not the case for a dataframe:

In [63]: pd.DataFrame([[0,1],[2,3]], columns=['a','b']).get(-1) is None
Out[63]: True

I could redefine Series.get to supersede NDFrame.get... but I don't know if that's a good solution. Maybe there are other classes inheriting from NDFrame that work like Series?

Here is what I'm proposing: if the solution based on the slice and loc fail, and axis is None, we revert to the old implementation. Thus we maintain a backward compatibility...

"""
# special case (GH 5652)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, this just generates a ValueError which by definition returns default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That generates a TypeError ("cannot use label indexing with a null key"). But you're right, I can also catch it below...

@shoyer
Copy link
Member

shoyer commented Nov 10, 2015

I'm not sure this is a good idea. Get is a standard method for mapping types and we already have a lot of indexing methods.

@jreback
Copy link
Contributor

jreback commented Nov 13, 2015

yeh, this is getting into too many edge cases. Lets just keep .get simple.

@jreback jreback closed this Nov 13, 2015
@nbonnotte nbonnotte deleted the get-axis-arg branch November 16, 2015 09:31
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Jul 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: allow get to take an axis argument
5 participants