Published
Edited
Apr 11, 2022
9 stars
Insert cell
Insert cell
// This reads in everything in as strings
raw = FileAttachment("penguins.csv").csv()
Insert cell
// can specify typed, but how can I make the NAs not be strings?
typed = FileAttachment("penguins.csv").csv({typed: true})
Insert cell
// some values are "NA" rather than null/undefined, etc.
typed[3]
Insert cell
Insert cell
fixed = typed.map((d) =>
Object.fromEntries(
Object.keys(d).map((key) => [key, d[key] === "NA" ? null : d[key]])
)
)
Insert cell
// "NA" is now null
fixed[3]
Insert cell
Insert cell
fixed2 = typed.map(d => {
d = {...d};
for (const key of Object.keys(d)) {
if (d[key] === "NA") d[key] = null;
}
return d;
})
Insert cell
// "NA" is now null
fixed2[3]
Insert cell
Insert cell
function autoType(nullString) {
return function (object) {
for (var key in object) {
var value = object[key].trim(),
number,
m;
if (!value || (nullString && value === nullString)) value = null;
else if (value === "true") value = true;
else if (value === "false") value = false;
else if (value === "NaN") value = NaN;
else if (!isNaN((number = +value))) value = number;
else if (
(m = value.match(
/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/
))
) {
if (fixtz && !!m[4] && !m[7])
value = value.replace(/-/g, "/").replace(/T/, " ");
value = new Date(value);
} else continue;
object[key] = value;
}
return object;
};
}
Insert cell
// https://github.com/d3/d3-dsv/issues/45
fixtz = new Date("2019-01-01T00:00").getHours() || new Date("2019-07-01T00:00").getHours();
Insert cell
Insert cell
// autoType mutates the objects in the array so i clone the array
fixed3 = structuredClone(raw).map(autoType("NA"))
Insert cell
fixed3[3]
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more