Skip to content

Commit 2008739

Browse files
committed
fix date typing in arrow tables
uses the same duck type test as observablehq/inputs#263 closes observablehq/framework#1376
1 parent 0e5c684 commit 2008739

File tree

6 files changed

+324
-4
lines changed

6 files changed

+324
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
]
8888
},
8989
"dependencies": {
90+
"apache-arrow": "^16.1.0",
9091
"d3": "^7.9.0",
9192
"interval-tree-1d": "^1.0.0",
9293
"isoformat": "^0.2.0"

src/options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ export function arrayify(values) {
148148
case "Sphere":
149149
return [values];
150150
}
151+
152+
// Duck type Arrow tables to retype date fields to Dates. Note that we only
153+
// need the first non-nullish value to be typed correctly for isTemporal to
154+
// return true.
155+
const fields = values?.schema?.fields;
156+
if (Array.isArray(fields)) {
157+
values = Array.from(values);
158+
for (const f of fields) {
159+
if (String(f).endsWith("<MILLISECOND>"))
160+
values.some((d, i) => d[f.name] != null && (values[i] = {...values[i], [f.name]: new Date(values[i][f.name])}));
161+
}
162+
return values;
163+
}
164+
151165
return Array.from(values);
152166
}
153167

test/output/arrowDates.svg

Lines changed: 121 additions & 0 deletions
Loading

test/plots/arrow-dates.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as Arrow from "apache-arrow";
3+
import * as d3 from "d3";
4+
5+
export async function arrowDates() {
6+
const athletes = await d3.csv<any>("data/athletes.csv", d3.autoType);
7+
const table = Arrow.tableFromJSON(athletes);
8+
return Plot.rectY(table, Plot.binX(undefined, {x: "date_of_birth"})).plot();
9+
}

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from "./aapl-volume.js";
1111
export * from "./anscombe-quartet.js";
1212
export * from "./arc.js";
1313
export * from "./armadillo.js";
14+
export * from "./arrow-dates.js";
1415
export * from "./aspectRatio.js";
1516
export * from "./athletes-bins-colors.js";
1617
export * from "./athletes-birthdays.js";

0 commit comments

Comments
 (0)