Find "seasonality" in a categorical time series in python I have the following sequence:
states_list = ['H', 'M', 'M', 'M', 'H', 'H', 'H', 'H', 'C', 'C', 'H', 'H', 'C', 'C', 'H', 'A', 'A', 'A', 'A', 'A', 'S', 'S', 'S', 'A', 'S', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'C', 'H', 'H', 'H', 'H', 'H', 'S', 'H', 'S', 'S', 'S', 'H', 'H', 'H', 'H', 'H', 'H', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'H', 'H', 'H', 'H', 'H', 'C', 'C', 'C', 'A', 'C', 'C', 'A', 'A', 'A', 'A', 'A', 'H', 'H', 'H', 'H', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']

Is there a way to find "seasonality" on this time series ?
By "seasonality" I mean, if there is a specific a specific sub-sequence of letters popping up every "n" letters

For every different letter, obtain its position in the series (1st, 2nd, ...). Take the ordered set of positions and second-difference it. If the resultant time series contains only zeros, you have found a letter that pops up every $n$ time periods. To find $n$, look at the first-differenced series; every value in it will be $n$.
I do not speak Python, but in R it would look like this:
y=rep(c("A","B","C","D","E"),20) # generate a seasonal categorical time series
x=which(y=="B")                  # select the season of interest, obtain its positions
d2=diff(x,differences=2)         # 2nd differencing yields zeros only
sum(abs(d2))                     # if the sum of absolute values is zero, every element must be zero; useful for large samples where diff(x,differences=2) is too large to inspect visually
diff(x)[1]                       # the period length is 5

This simple program illustrates the main idea, but it is not bulletproof. It can fail in several cases. E.g. it does not notice if the letter is absent in the beginning or the end of the sequence. Include y=c(rep("A",10),y,rep("A",10)) after the first line to see that. Fixing that should not require much effort, though.
