Implementation of the SwingingDoor algorithm in Python.
>>> from datetime import datetime
>>> from pandas import read_csv, DataFrame
>>> df = DataFrame(
... [
... {
... "Date": datetime.strptime(date, "%Y-%m-%d"),
... "Price": value
... }
... for date, value in read_csv(
... "https://datahub.io/core/oil-prices/r/wti-daily.csv"
... ).values.tolist()
... ]
... )
>>> print(len(df))
9286
>>> df.plot(x="Date", y="Price")
>>> from swinging_door import swinging_door
>>> compress = DataFrame(
... list(
... {
... "Date": datetime.fromtimestamp(date),
... "Price": value
... }
... for date, value in swinging_door(
... [
... (date.timestamp(), value)
... for date, value in df.values.tolist()
... ], deviation=.5
... )
... )
... )
>>> print(len(compress))
2719
>>> compress.plot(x="Date", y="Price")