PolicyEngine / microdf

Analysis tools for working with survey microdata as DataFrames.
http://pslmodels.github.io/microdf
MIT License
12 stars 10 forks source link

Add __getattr__ override to MicroDataFrame #220

Open nikhilwoodruff opened 3 years ago

nikhilwoodruff commented 3 years ago

This would allow:

df = mdf.MicroDataFrame(dict(first_column=[1, 2, 3]), weights=[2, 2, 1])
df.first_column

Currently, we can only do:

df["first_column"]

We just would need to add a getattr override to MicroDataFrame, call the super function and wrap the results in the same MicroDataFrame configuration that it's called from.

hdoupe commented 3 years ago

Hey @nikhilwoodruff, I've been passively lurking in the Github notifications for this project and have enjoyed seeing the progress y'all are making!

I did something kinda similar in Tax-Calculator a few months ago if it's helpful: https://github.com/hdoupe/Tax-Calculator/blob/df-based/taxcalc/data.py#L313-L342

Although judging from the work you've done already, you don't need my help haha.

nikhilwoodruff commented 3 years ago

Thanks @hdoupe, that tax-calc example is pretty cool, and interesting that it uses setattr too. Not sure if there's any Pandas uses for setattr we need to cover @MaxGhenis?

MaxGhenis commented 3 years ago

pandas lets you overwrite an existing value with setattr, but only if the column already exists:

import pandas as pd
df = pd.DataFrame({"x": [1]})
df.x = 2
df

yields:

    x
0   2

Where:

df.y = 2
df

still yields:

    x
0   2