Skip to content
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

How to left pad a string for a new column? #12

Closed
clhenrick opened this issue Oct 8, 2020 · 2 comments · Fixed by #13
Closed

How to left pad a string for a new column? #12

clhenrick opened this issue Oct 8, 2020 · 2 comments · Fixed by #13
Labels
enhancement New feature or request

Comments

@clhenrick
Copy link

For example, in the case when dealing with U.S. Census FIPS values that are stored as numbers in a dataset, how would I convert the numeric values back to string values that are left padded with the correct number of zeros?

When I try doing something like:

table.derive({
  "fips_state_str": d => `${d.fips_state}`.padStart(2, "0")
})

... Arquero throws an error. Looking at the API documentation and docs on Observable I don't see an example of how to accomplish this, but apologies if I missed or overlooked something. Thanks!

@clhenrick
Copy link
Author

Follow up, I was able to add a utility function to accomplish this:

  aq.addFunction(
    "leftPad",
    (value, times, char) => `${value}`.padStart(times, char)
  );

and use it as follows:

table.derive({
  "fips_state_str": d => op.leftPad(d.fipsstate, 2, "0")
})

Though I'm still curious if this is the recommended approach and if Arquero has a built in method for handling this.

@jheer jheer added the enhancement New feature or request label Oct 8, 2020
@jheer
Copy link
Member

jheer commented Oct 8, 2020

Thanks! Arquero currently lacks padding methods as part of the "built-in" functions, but adding padstart and padend (or similar) would be straightforward. I'll look into adding those soon. In the meantime, your use of the addFunction method is spot on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants