Skip to content

QST: Use object property as key? #36947

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
DorianCzichotzki opened this issue Oct 7, 2020 · 2 comments
Closed

QST: Use object property as key? #36947

DorianCzichotzki opened this issue Oct 7, 2020 · 2 comments
Labels
Needs Triage Issue that has not been reviewed by a pandas team member Usage Question

Comments

@DorianCzichotzki
Copy link

Is there a way to use a property of an object as the key when providing the object to a DataFrame?
I think the code makes clearer what I am looking for.

class Key:
  def __init__(self, name: str):
    self.name = name
  
  # Something like that
  def __pandas_key__(self):
    return self.name

df = DataFrame({"col": [1,2,3,4]})
key = Key("col")

df[key] # is [1,2,3,4]
@DorianCzichotzki DorianCzichotzki added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Oct 7, 2020
@jbrockmendel
Copy link
Member

No, but I think if you do df = pd.DataFrame({key: [1, 2, 3, 4]}) then df[key] works just fine

Longer term, #11723 might make this feasible.

@TomAugspurger
Copy link
Contributor

This would be subsumed by #11723, so let's have the discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Triage Issue that has not been reviewed by a pandas team member Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants