Python EMA与Pandas在不一致的时间序列上

时间:2015-05-28 15:37:59

标签: python pandas dataframe

我试图在Pandas数据框中的给定数据集上计算EMA。我想要的alpha是1分钟,所以在一个完美的世界里,我会将60的跨度传递给EWMA函数。

问题是,我的时间序列是不一致的 - 从某种意义上来说,它并没有顺利地和#34;从一秒钟到下一秒钟。例如 -

(Date | Value)
2015-05-27 05:14:35 | 5
2015-05-27 05:14:59 | 5.5
2015-05-27 05:15:30 | 5.2
2015-05-27 05:15:40 | 5.1

所以60这样的跨度显然不适用于此,因为Pandas会将其解释为每60个数据点而不是每60秒。有没有明显的解决方案? "显而易见的"在间隙中每隔一秒插入数据点,并外推值。我应该注意Date列是一个合适的Python datetime64对象。

我的基本代码......

protected void showReport(string fileName)
    {
        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        DataTable DataTable1 = new DataTable
        report.LocalReport.Refresh();
        report.Reset();
        report.LocalReport.EnableExternalImages = true;
        this.report.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
        ReportDataSource rds2 = new ReportDataSource("DataSet1", DataTable1);
        report.LocalReport.DataSources.Add(rds2);
        report.LocalReport.ReportPath = "YourReport.rdlc";
        ReportParameter rptParam = new ReportParameter("your_parameter");       
        report.LocalReport.SetParameters(rptParam);
        byte[] bytes = report.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = mimeType;
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
        try
        {
            Response.BinaryWrite(bytes);
        }
        catch (Exception ex)
        { 
           Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('Error while generating PDF.');", true);
           Console.WriteLine(ex.StackTrace);
        }
        Response.Flush();
        report.LocalReport.Refresh();
    }

1 个答案:

答案 0 :(得分:1)

想出来。 @EdChum推荐了Pandas中的resample方法,这就是我所寻求的。

import pandas

df = pandas.read_csv("data.csv")
dff = df.resample("S", fill_method='pad')

'fill_method'选项可防止“新”值为NaN。

现在这样的数据框......

2015-05-27 05:14:35 | 5
2015-05-27 05:14:41 | 5.5

看起来像这样......

2015-05-27 05:14:35 | 5
2015-05-27 05:14:36 | 5
2015-05-27 05:14:37 | 5
2015-05-27 05:14:38 | 5
2015-05-27 05:14:39 | 5
2015-05-27 05:14:40 | 5
2015-05-27 05:14:41 | 5.5