Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subtract two pandas datetime series DataFrames from each other when the index is a datetime?

I am trying to subtract the values of two pandas datetime series DataFrames from each other in which the index for both DataFrames is a datetime value.

The two DataFrames in question have the same amount of columns. I want to subtract the value in the column in the second DataFrame from the value in the of the column in the first DataFrame to create a new DataFrame with the new subtracted values for the column represented in the second DataFrame.

Here is what my data looks like:

import pandas as pd
import numpy as np
import statsmodels.api as sm

In[]: y
Out[]:            price
        Date
     2010-08-29   .0648
     2010-08-30   .0751
               ...
    2897 rows × 1 columns

In[]: type(y)
Out[]: pandas.core.frame.DataFrame

These are the values I want to subtract from y values above:

In[]: test = pd.DataFrame(est.predict(X))
      test
Out[]:                     0

         2010-08-29    -5.851237
         2010-08-30    -6.835347
         2010-08-31    -9.949124
                   ...
          2897 rows × 1 columns

When I attempt the pd.sub() operations on the data above I am returned a DataFrame full of NaN values as such:

In[]: vals = y.sub(test)
      vals 
Out[]:                price    0    
           Date
        2010-08-29     NaN    NaN
        2010-08-30     NaN    NaN
                   ...
         2897 rows × 2 columns

How can I subtract the values from the two columns to get an output such as the output below?

1      1.544535
2     -1.945362
3     -3.037018
4      0.882884
    ...
Name: newFrame, Length: 200, dtype: float64
like image 484
lopezdp Avatar asked Sep 14 '25 00:09

lopezdp


1 Answers

Using sub between two dataframes will work if columns have the same name so here for example, create the dataframe test by:

test = pd.DataFrame(est.predict(X),columns=['price'])
like image 115
Ben.T Avatar answered Sep 16 '25 15:09

Ben.T