wapi-python icon indicating copy to clipboard operation
wapi-python copied to clipboard

Speed up to_pandas function

Open stalehuse opened this issue 3 years ago • 1 comments

The to_pandas function is a bit slow. In wapi/util.py the to_pandas function iterates over each point:

    index = []
    values = []
    for row in self.points:
        if len(row) != 2:
            raise ValueError('Points have unexpected contents')
        dt = datetime.datetime.fromtimestamp(row[0] / 1000.0, self.tz)
        index.append(dt)
        values.append(row[1])
    res = pd.Series(name=name, index=index, data=values)

This can be changed to:

    data = np.array(self.points)
    if data.shape[1] != 2:
        raise ValueError('Points have unexpected contents')
    dt = pd.to_datetime(data[:, 0], unit='ms', utc=True).tz_convert(self.tz)
    res = pd.Series(name=name, index=dt, data=data[:, 1])

With this change, calculation time was reduced by 75% on the small example I tested it on. You might get even better speedup on longer series.

Regards, Ståle Huse Å Energi

stalehuse avatar Jul 04 '23 10:07 stalehuse

Thanks for your suggestion, Ståle!

We are working on a new version, we will consider to do something about this as part of that.

stigalvestad avatar Sep 14 '23 11:09 stigalvestad