如何合并长度不相等的数据帧

时间:2019-10-24 02:25:30

标签: python pandas time-series

我们对数据进行每日预测,如下所示:

import * as functions from 'firebase-functions';

import * as admin from 'firebase-admin';

admin.initializeApp();

exports.newTopicNotification = functions.firestore
    .document('topics/{id}/topic/{doc}/chat/{chat}')
    .onWrite( async event => {
        const allMessages = event.after.data();
        const db = admin.firestore();
        let data: any;

        if (allMessages) { data = allMessages; }
        const title = data ? data.title : '';
        const topicId = data ? data.topicId : '';
        const groupId = data ? data.groupId : '';

        console.log('incomingData', data);

        const payload = {
            notification: {
                title: 'New group topic post',
                body: `${title}`
            }
        };

        let users: any = [];
        let devices: any = [];
        const tokens: any = [];

        users = await db.collection('topics')
                            .doc(`${groupId}`)
                            .collection('topic')
                            .doc(`${topicId}`)
                            .get();

        console.log('users', users.data().subscribers);

        for (let i = 0; i < users.data().subscribers.length; i++) {
            const devicesRef = db.collection('devices').where('userId', '==', users.data().subscribers[i]);
            const device = await devicesRef.get();
            devices.push(device);
            console.log('device', devices);
        }

// here the result keeps showing the error 
        devices.forEach(result => {
            const token = result.data().token;
            tokens.push(token);
          });
        return admin.messaging().sendToDevice(tokens, payload);
    });

我们还有小时平均比率(0-23小时)。

df_test_daily['prediction'].head()

Datetime
2014-09-26    343.434258
2014-09-27    346.512980
2014-09-28    349.591701
2014-09-29    352.670422
2014-09-30    355.749144

如何使用平均小时比率与每日数据进行小时预测。

假设2014-09-26的预测为343。现在必须将平均小时比率乘以343才能生成24小时的数据或预测。

预期输出:


hourly_frac.head()
        Hour  ratio
0       0  0.044287
1       1  0.035343
2       2  0.029911
3       3  0.024714
4       4  0.020802

1 个答案:

答案 0 :(得分:0)

您将需要合并两个数据框,以获取具有所有Datetime-Hr可能组合的新数据框:

df_preds = df_test_daily.assign(key=1).merge(df_hours.assign(key=1)).drop('key', axis=1)

然后您可以使用以下命令轻松计算每天每一小时的预测:

df_preds['hourly_prediction'] = df_preds['prediction'] * df_preds['ratio']