cenpy-devs / cenpy

Explore and download data from Census APIs
Other
183 stars 44 forks source link

Accept dict of variable names to rename result directly in `from_*` functions #91

Open MaxGhenis opened 4 years ago

MaxGhenis commented 4 years ago

A nice feature of tidycensus is that you can pass a renaming of fields directly to the function. From this example:

vt <- get_acs(geography = "county", 
              variables = c(medincome = "B19013_001"), 
              state = "VT")

One way to do this today with cenpy is to make a dict and then extract the fields accordingly, then rename, e.g.:

FIELDS = {'B25001_001E': 'housing_units',
          'B01003_001E': 'population'}
sf = acs.from_place('San Francisco, CA',
                    variables=list(FIELDS.keys()))
sf.rename(FIELDS, axis=1, inplace=True)

It'd be nice if it were part of the functions themselves, i.e. doing some logic based on

if type(variables) is dict:
knaaptime commented 4 years ago

personally, i'm not sure there's a huge benefit to doing this rename inside the function rather than as a second step... either way you have to set the mapping from variable name to display name, so is it jsut that you want to avoid the explicit rename call?

That said, I also don't see any specific reason we'd want to avoid this option, so PRs welcome :)

ljwolf commented 4 years ago

This is an interesting idea, and it costs us nearly nothing to implement it, since it's just a setup/cleanup operation. Should definitely be doable for the next point release :smile: