我需要对node.js
中的时间序列进行重新采样。因此,我想知道javascript
中是否有一个工具与Python中的pandas
类似?
让我们说我有一些类似于以下示例的数据:
[{
"time": "28-09-2018 21:29:04",
"value1": 1280,
"value2": 800
},
{
"time": "28-09-2018 21:38:56",
"value1": 600,
"value2": 700
},
{
"time": "29-09-2018 10:40:00",
"value1": 1100,
"value2": 300
},
{
"time": "29-09-2018 23:50:48",
"value1": 140,
"value2": 300
}]
在Python
中,我会将这些数据放入pandas
数据帧中,然后以不同的采样率将其重新采样到新的数据帧中。在此示例中,以每日数据为例:
import pandas
df = pandas.DataFrame(...)
df_days = df.resample('1440min').apply({'value1':'sum', 'value2':'sum'}).fillna(0)
所以我的新数据看起来像这样:
[{
"time": "28-09-2018 00:00:00",
"value1": 1880,
"value2": 1500
},
{
"time": "29-09-2018 00:00:00",
"value1": 1240,
"value2": 600
}]
在node.js
/ javascript
中一般最好的方法是什么?
答案 0 :(得分:0)
简单方法
flask
应用,可以为您执行pandas
处理JQuery
AJAX即可。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<main id="main">
<section id="data-section">
<h2>Data</h2>
<div id="data"/>
</section>
</main>
</body>
<script>
function apicall(url, data) {
$.ajax({
type:"POST", url:url, data:{data:JSON.stringify(data)},
success: (data) => { $("#data").text(JSON.stringify(data)); }
});
}
data = [{"time": "28-09-2018 21:29:04","value1": 1280,"value2": 800},{"time": "28-09-2018 21:38:56","value1": 600,"value2": 700},{"time": "29-09-2018 10:40:00","value1": 1100,"value2": 300},
{"time": "29-09-2018 23:50:48","value1": 140,"value2": 300}];
window.onload = function () {
apicall("/handle_data", data);
}
</script>
</html>
import pandas as pd, json
from flask import Flask, redirect, url_for, request, render_template, Response
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/handle_data', methods=["POST"])
def handle_data():
df = pd.DataFrame(json.loads(request.form.get("data")))
df["time"] = pd.to_datetime(df["time"])
df.set_index("time", inplace=True)
df = df.resample('1440min').apply({'value1':'sum', 'value2':'sum'}).fillna(0)
return Response(json.dumps(df.to_dict(orient="records")),
mimetype="text/json")
if __name__ == '__main__':
app.run(debug=True, port=3000)