# Can we use Box-Ljung as a stationarity test for time series?

It's all in the title, I know that we usually use Box-Ljung to test the randomness in a time series (independence of residuals), but I found this post about how to tell if a time series is stationary or not. If this is correct, can someone explain how stationarity is checked using this test?

You are quite right that the LB-test should be used for testing lack of serial correlation. In case of a rejection, there is evidence for serial correlation. The rejection however does not allow us to conclude that the process is stationary under the alternative - the LB-test certainly also has power when the series under test is nonstationary.

In that sense, the statement in the linked post

The Ljung-Box test examines whether there is significant evidence for non-zero correlations at lags 1-20. Small p-values (i.e., less than 0.05) suggest that the series is stationary.

This is easy to see from the test statistic $$LB=T(T+2)\sum_{j=1}^p\frac{\hat\rho_j^2}{T-j}$$ For a random walk as one simple example of a nonstationary process we have that $$Y_T = \sum_{s=1}^{T} \epsilon_s$$ and so $$\gamma_{Tj} = E\left(\sum_{s=1}^T \epsilon_s \sum_{s=1}^{T-j}\epsilon_s\right) = (T-j)\sigma^2$$ Thus, $$\rho_{jT} = \frac{\gamma_{jT}}{\sqrt{\gamma_{0T}}\sqrt{\gamma_{0 (T-j)}}}=\frac{T-j}{\sqrt{T}\sqrt{T-j}}=\frac{\sqrt{T-j}}{\sqrt{T}}= \sqrt{1-\frac{j}{T}}$$ This just recalls the strong persistence properties of a random walk. If the process was "born" long ago ($T\to\infty$), all autocorrelations even tend to one.
Hence, the autocorrelations will also be estimated as $\hat\rho_j\approx1$, $j=1,\ldots,p$, for sufficiently large $T$. Therefore, $LB\to_p\infty$, so that the test will have power equal to 1 in large samples as the test statistic will then virtually always exceed its $\chi^2_p$-critical value. Correspondingly, of course, the $p$-value will be close to zero.