Skip to content

Commit

Permalink
fix date typing in arrow tables
Browse files Browse the repository at this point in the history
uses the same duck type test as observablehq/inputs#263

closes observablehq/framework#1376
  • Loading branch information
Fil committed Jun 21, 2024
1 parent 0e5c684 commit 2008739
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
]
},
"dependencies": {
"apache-arrow": "^16.1.0",
"d3": "^7.9.0",
"interval-tree-1d": "^1.0.0",
"isoformat": "^0.2.0"
Expand Down
14 changes: 14 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ export function arrayify(values) {
case "Sphere":
return [values];
}

// Duck type Arrow tables to retype date fields to Dates. Note that we only
// need the first non-nullish value to be typed correctly for isTemporal to
// return true.
const fields = values?.schema?.fields;
if (Array.isArray(fields)) {
values = Array.from(values);
for (const f of fields) {
if (String(f).endsWith("<MILLISECOND>"))
values.some((d, i) => d[f.name] != null && (values[i] = {...values[i], [f.name]: new Date(values[i][f.name])}));
}
return values;
}

return Array.from(values);
}

Expand Down
121 changes: 121 additions & 0 deletions test/output/arrowDates.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/plots/arrow-dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Plot from "@observablehq/plot";
import * as Arrow from "apache-arrow";
import * as d3 from "d3";

export async function arrowDates() {
const athletes = await d3.csv<any>("data/athletes.csv", d3.autoType);
const table = Arrow.tableFromJSON(athletes);
return Plot.rectY(table, Plot.binX(undefined, {x: "date_of_birth"})).plot();
}
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./aapl-volume.js";
export * from "./anscombe-quartet.js";
export * from "./arc.js";
export * from "./armadillo.js";
export * from "./arrow-dates.js";
export * from "./aspectRatio.js";
export * from "./athletes-bins-colors.js";
export * from "./athletes-birthdays.js";
Expand Down
Loading

0 comments on commit 2008739

Please sign in to comment.