Last active
July 10, 2023 01:35
-
-
Save hillkim7/ff852d3a5d6810a582d62ef7a1140f37 to your computer and use it in GitHub Desktop.
It shows how to resample time series data within javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code show how to resample dataframe within javascript. | |
// It is based on the following link: | |
// https://gist.github.com/edouardswiac/0782cfb1fc66aba6b0ffca06c01c03b2 | |
var moment = require('moment') | |
// our time precision | |
let timeUnit = 'minute' | |
let startTime = moment().startOf(timeUnit) | |
let sampleTime = moment(startTime) | |
let samples = [] | |
for (var i = 0; i < 10; i++) { | |
samples.push({x: moment(sampleTime), y: i}) | |
sampleTime.add(20, 'seconds') | |
} | |
let endTime = moment(sampleTime) | |
console.log('samples-->') | |
samples.forEach((value, key) => { | |
console.log('[', key, '] ', value.x.format("YY-MM-DD hh:mm:ss"), value.y) | |
}) | |
let datesResampled = samples.map(value => { | |
let resampled = value.x.startOf(timeUnit) | |
return {x: resampled, y: value.y} | |
}) | |
console.log('datesResampled-->') | |
datesResampled.forEach((value, key) => { | |
console.log('[', key, '] ', value.x.format("YY-MM-DD hh:mm:ss"), value.y) | |
}) | |
// create a map object with time key for lookup | |
let datesReduced = datesResampled.reduce((result, value, key) => { | |
result[value.x.valueOf()] = value | |
return result | |
}, {}) | |
console.log('datesReduced-->') | |
for (const [key, value] of Object.entries(datesReduced)) { | |
console.log('[', key, '] ', value.x.format("YY-MM-DD hh:mm:ss"), value.y) | |
} | |
let filled = [] | |
while (startTime.isBefore(endTime)) { | |
if (datesReduced[startTime.valueOf()]) | |
filled.push(datesReduced[startTime.valueOf()]) | |
else | |
filled.push({x: moment(startTime), y: null }) | |
startTime.add(20, 'seconds') | |
} | |
console.log('filled-->') | |
filled.forEach((value, key) => { | |
console.log('[', key, '] ', value.x.format("YY-MM-DD hh:mm:ss"), value.y) | |
}) | |
/* OUTPUT: | |
samples--> | |
[ 0 ] 22-09-17 05:37:00 0 | |
[ 1 ] 22-09-17 05:37:20 1 | |
[ 2 ] 22-09-17 05:37:40 2 | |
[ 3 ] 22-09-17 05:38:00 3 | |
[ 4 ] 22-09-17 05:38:20 4 | |
[ 5 ] 22-09-17 05:38:40 5 | |
[ 6 ] 22-09-17 05:39:00 6 | |
[ 7 ] 22-09-17 05:39:20 7 | |
[ 8 ] 22-09-17 05:39:40 8 | |
[ 9 ] 22-09-17 05:40:00 9 | |
datesResampled--> | |
[ 0 ] 22-09-17 05:37:00 0 | |
[ 1 ] 22-09-17 05:37:00 1 | |
[ 2 ] 22-09-17 05:37:00 2 | |
[ 3 ] 22-09-17 05:38:00 3 | |
[ 4 ] 22-09-17 05:38:00 4 | |
[ 5 ] 22-09-17 05:38:00 5 | |
[ 6 ] 22-09-17 05:39:00 6 | |
[ 7 ] 22-09-17 05:39:00 7 | |
[ 8 ] 22-09-17 05:39:00 8 | |
[ 9 ] 22-09-17 05:40:00 9 | |
datesReduced--> | |
[ 1663403820000 ] 22-09-17 05:37:00 2 | |
[ 1663403880000 ] 22-09-17 05:38:00 5 | |
[ 1663403940000 ] 22-09-17 05:39:00 8 | |
[ 1663404000000 ] 22-09-17 05:40:00 9 | |
filled--> | |
[ 0 ] 22-09-17 05:37:00 2 | |
[ 1 ] 22-09-17 05:37:20 null | |
[ 2 ] 22-09-17 05:37:40 null | |
[ 3 ] 22-09-17 05:38:00 5 | |
[ 4 ] 22-09-17 05:38:20 null | |
[ 5 ] 22-09-17 05:38:40 null | |
[ 6 ] 22-09-17 05:39:00 8 | |
[ 7 ] 22-09-17 05:39:20 null | |
[ 8 ] 22-09-17 05:39:40 null | |
[ 9 ] 22-09-17 05:40:00 9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment