Get month name in Arquero table

I’m having trouble creating a column containing the month name in an Arquero table. It seems as if the Date.toLocaleDateString("default", { month: "long" }) method can’t be called when using derive to create a new column.

Similarly, it seems as if I can’t reference a separate array with month names:

// Tried to use a separate array, but can't seem to reference it properly
month_name = protests
  .derive({ month_num: d => op.month(d.Date) })
  .derive({ month_name: d => monthNames[d.month_num] })
  .select("Date", "month_num")
  .view()

More details in this notebook. Thanks!

I think you need to use table.params() (see https://uwdata.github.io/arquero/api/table#params).

You might be interested by the conversation we add with @bmschmidt : https://observablehq.com/@bmschmidt/exploring-changing-us-college-majors-with-arquero#comment-858c591ca4faecc0

2 Likes

Amazing, thanks! FWIW, I added the solution to the notebook:

protests
  .params({ monthNames: monthNames })
  .derive({ month: (d, $) => $.monthNames[op.month(d.Date)] })
  .select("Date", "month")
2 Likes

I would like to filter an Arquero table from an observable input selector.
But I am stuck, the following is a snapshot of a reproducible example I created:
Screen Shot 2021-02-07 at 12.24.19

Any suggestions is super welcome
Thanks in advance!

Maybe it’s the same problem as in Get month name in Arquero table - #2 by severo

2 Likes

@severo Thanks, that was exactly what was needed!
Super grateful for your feedback!

1 Like

FYI: More than likely, multiple date/time formatting will be done on Arquero tables (eg: date, day of week, year, hours, minutes, etc.) In that event, using d3’s date formatting (or a library-independent formatting) through Arquero’s extended functions is a viable option:

The following pattern is library-independent, will provide multi-formatting flexibility and perform well:

{
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
const dateFormatting = new Intl.DateTimeFormat("en-US",{month: "long"});

// https://uwdata.github.io/arquero/api/extensibility#addFunction
aq.addFunction("getMonth", x => dateFormatting.format(x));

return protests
          .derive({ month: d => op.getMonth(d.Date)})
          .select("Date", "month")
          .view()

}