This question has been edited for clarity based on the comments
Objective: Flag variable number of stuck/repetitive values in the columns of a data frame based on a group.
Desired Solution: The desired solution should meet the following criteria:
- Speed and Brevity are important
- Solution should accept as input variable number of thresholds for flagging stuck/repetitive values for each zone. For e.g. : {"Zone1": 4 , "Zone2":2} -> indicating that in Zone1, there need to be 4 consecutive repeated values for the logic to flag, while in Zone2, having atleast 2 or more repeated values should trigger the flag
- Please add comments wherever possible to make it easier to understand
- Preferably use Pandas and/or Numpy
- Time your solution on a test data frame with 87600 values (generate a data frame with 87,600 values to time)
Input Data/ Problem Formulation:
import pandas as pd
import numpy as np
from random import randint
# Generate some random data
ts_index = pd.date_range("1/1/2019", periods=24, freq="1H")
v1 = [randint(1, 100) for i in range(24)]
v2 = [2] * 24
v3 = [2, 2, 2, 2, 4, 4, 0, 2, 2, 1, 9, 2, 4, 1, 2, 2, 0, 2, 1, 8, 1, 7, 3, 5]
test_df = pd.DataFrame({"v1": v1, "v2": v2, "v3": v3}, index=ts_index)
print(test_df)

