# Using KPSS test in Python with statsmodels

17.7736 17.7736 17.7638 17.7638 17.754 17.754 17.7834 17.7834 17.7834 17.7834 17.7834 17.7834 17.7834 17.7834 17.8324 17.8324 17.8324 17.852 17.9304 17.9304 17.9304 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1362 18.146 18.146 18.1656 18.1754 18.1656 18.1656 18.1656 18.1656 18.1656 18.146 18.1362 18.1362 18.1656 18.1656 18.1656 18.1264 18.1264 18.1264 18.1264 18.1264 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1166 18.1264 18.1264 18.1264 18.1264 18.1

I think this dataset is stationary because the values very near with each other But the surprise when using the Kpss test with this code show that this is not stationary . What is the wrong please I am very confuse

# KPSS test
from statsmodels.tsa.stattools import kpss
#57358

def kpss_test(data, **kw):
statistic, p_value, n_lags, critical_values = kpss(data, **kw)
# Format Output
print(f'KPSS Statistic: {statistic}')
print(f'p-value: {p_value}')
# print(f'num lags: {n_lags}')
print('Critial Values:')

for key, value in critical_values.items():
print(f'   {key} : {value}')

print(f'Result: The series is {"not " if p_value < 0.05 else ""}stationary')

kpss_test(data)


Have you plotted your data? I did. Does it look stationary or “almost stationary” to you?

• +1 skimming the numbers, it looked like this kind of trend was present.
– Dave
Commented Aug 31, 2022 at 23:00
• @oleva Do these tests have the same null hypothesis? Commented Sep 1, 2022 at 11:27
• @dipetkov.No, it have not!
– ayla
Commented Sep 1, 2022 at 11:56
• Could you check the update please? why this difference? I used another dataset
– ayla
Commented Sep 1, 2022 at 12:15
• @oleva You are focusing on the numbers returned by the tests rather than on what the tests are supposed to test. It will help to start with theory and understand it first. Commented Sep 1, 2022 at 12:18

I didn’t count how many points you have, but it looks like “a lot” is a good description.

When you have “a lot” of points, hypothesis tests have the to reject small deviations from the null hypothesis, even deviations so small that they are not of any practical consequence.

This could be what you’re seeing: you’re right that the time series is just about stationary, and the formal test is catching that the time series is not quite exactly stationary.

(I’m not actually convinced that “almost stationary” applies to your time series, but if it does, the test is catching that it is almost stationary, not perfectly stationary.)

• Could you check the update please? why this difference?
– ayla
Commented Sep 1, 2022 at 12:15
• @oleva It’s really not fair to answerers (or future readers) to make the question a moving target. If you have a new question (and yours is totally legitimate), please post it
– Dave
Commented Sep 1, 2022 at 12:38
• Yes, that's right, I modified it, I know it is not permissible, but I asked before and did not get an answer, so I thought I would get the answer here.
– ayla
Commented Sep 1, 2022 at 12:51