trappy.utils module

Generic functions that can be used in multiple places in trappy

trappy.utils.handle_duplicate_index(data, max_delta=1e-06)[source]

Handle duplicate values in index

Parameters:
  • data (pandas.Series) – The timeseries input
  • max_delta (float) – Maximum interval adjustment value that will be added to duplicate indices

Consider the following case where a series needs to be reindexed to a new index (which can be required when different series need to be combined and compared):

import pandas
values = [0, 1, 2, 3, 4]
index = [0.0, 1.0, 1.0, 6.0, 7.0]
series = pandas.Series(values, index=index)
new_index = [0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0]
series.reindex(new_index)

The above code fails with:

ValueError: cannot reindex from a duplicate axis

The function handle_duplicate_axis() changes the duplicate values to

>>> import pandas
>>> from trappy.utils import handle_duplicate_index

>>> values = [0, 1, 2, 3, 4]
index = [0.0, 1.0, 1.0, 6.0, 7.0]
series = pandas.Series(values, index=index)
series = handle_duplicate_index(series)
print series.index.values
>>> [ 0.        1.        1.000001  6.        7.      ]
trappy.utils.listify(to_select)[source]

Utitlity function to handle both single and list inputs