wapi-python
wapi-python copied to clipboard
Speed up to_pandas function
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
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.