title: How to use a function onto np.roll() like DataFrame.rolling().apply(function) or dataframe.rolling().agg(function)?
body: <p>I have the below function and would like to apply it onto a numpy.roll. I have searched the internet to no avail, and most np.roll solutions on stackoverflow tend to show how to do the rolling function but that's not what I am looking for.</p>
  <pre><code>from sklearn.decomposition import PCA
  
  df = pd.DataFrame({&quot;A&quot;: [1, 2, 3, 4, 5, 6, 7, 8], &quot;B&quot;: [2, 3, 4, 5, 6, 7, 8, 9]})
  df_a = np.array(df)
  
  def first_variance(X):
      pca = PCA(n_components=1)
      pca.fit(X)
      return pca.explained_variance_
  
  np.roll(df_a, 10)
  </code></pre>
  <p>Is there a way to employ a function on this rolling numpy function like you would on a pandas df?</p>
  <p><strong>EDIT</strong></p>
  <p>The expected outcome for this would be a timeseries dataframe for the outcome. Please ignore the fact the numbers are the same. In actuality this will not be the case, had to whip something up to use as a demonstration.</p>
  <pre><code>         PCA 1 Expl Var PCA 2 Expl Var  PCA 3 Expl Var  PCA 4 Expl Var  PCA 5 Expl Var
  2021-10-01  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-10-11  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-10-21  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-10-31  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-11-11  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-11-21  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-12-01  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-12-11  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-12-21  0.011044    0.004083    0.003389    0.002877    0.002368
  2021-12-31  0.011044    0.004083    0.003389    0.002877    0.002368
  </code></pre>
  <p>I was going to turn the array into a df. When I tried the df.rolling(3).apply(first_variance), I got this error:</p>
  <p><code>ValueError: Expected 2D array, got 1D array instead: array=[1. 2. 3.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.</code></p>
  <p>So I decided to turn it into an array and then run the rolling function on it and then turn the array into a df to look like the above.</p>