This paper concerns statistical inference on unit roots and cointegration for time series taking values in a Hilbert space of an arbitrarily large dimension or a subspace of possibly unknown dimension. When such a time series is given, an important first step is to estimate the number of stochastic trends, which indicates how many linearly independent unit root processes are embedded in the time series. We develop statistical inference on the number of stochastic trends that remains asymptotically valid even when the time series of interest takes values in a subspace of an arbitrary and indefinite dimension. This has wide applicability in practice; for example, in the case of cointegrated vector time series of finite dimension, in a high-dimensional factor model that includes a finite number of nonstationary factors, in the case of cointegrated curve-valued (or function-valued) time series, and nonstationary dynamic functional factor models.