diedro
diedro

Reputation: 613

Apply a function to all value belonging to the same day

I have this dataframe:

dates,rr.price,ax.price,be.price
2018-01-01 00:00:00,45.73,45.83,47.63
2018-01-01 01:00:00,44.16,44.59,44.42
2018-01-01 02:00:00,42.24,40.22,42.34
2018-01-01 03:00:00,39.29,37.31,38.36
2018-01-01 04:00:00,36,32.88,36.87
2018-01-01 05:00:00,41.99,39.27,39.79
2018-01-01 06:00:00,42.25,43.62,42.08
2018-01-01 07:00:00,44.97,49.69,51.19
2018-01-01 08:00:00,45,49.98,59.69
2018-01-01 09:00:00,44.94,48.04,56.67
2018-01-01 10:00:00,45.04,46.85,53.54
2018-01-01 11:00:00,46.67,47.95,52.6
2018-01-01 12:00:00,46.99,46.6,50.77
2018-01-01 13:00:00,44.16,43.02,50.27
2018-01-01 14:00:00,45.26,44.2,50.64
2018-01-01 15:00:00,47.84,47.1,54.79
2018-01-01 16:00:00,50.1,50.83,60.17
2018-01-01 17:00:00,54.3,58.31,59.47
2018-01-01 18:00:00,51.91,63.5,60.16
2018-01-01 19:00:00,51.38,61.9,70.81
2018-01-01 20:00:00,49.2,59.62,62.65
2018-01-01 21:00:00,45.73,52.84,59.71
2018-01-01 22:00:00,44.84,51.43,50.96
2018-01-01 23:00:00,38.11,45.35,46.52
2018-01-02 00:00:00,19.19,41.61,49.62
2018-01-02 01:00:00,14.99,40.78,45.05
2018-01-02 02:00:00,11,39.59,45.18
2018-01-02 03:00:00,10,36.95,37.12
2018-01-02 04:00:00,11.83,31.38,38.03
2018-01-02 05:00:00,14.99,34.02,46.17
2018-01-02 06:00:00,40.6,41.27,51.71
2018-01-02 07:00:00,46.99,48.25,54.37
2018-01-02 08:00:00,47.95,43.57,75.3
2018-01-02 09:00:00,49.9,48.34,68.48
2018-01-02 10:00:00,50,48.01,61.94
2018-01-02 11:00:00,49.7,52.22,63.26
2018-01-02 12:00:00,48.16,47.47,59.41
2018-01-02 13:00:00,47.24,47.61,60
2018-01-02 14:00:00,46.1,49.12,67.44
2018-01-02 15:00:00,47.6,52.38,66.82
2018-01-02 16:00:00,50.45,58.35,72.17
2018-01-02 17:00:00,54.9,61.4,70.28
2018-01-02 18:00:00,57.18,54.58,62.63
2018-01-02 19:00:00,54.9,53.66,63.78
2018-01-02 20:00:00,51.2,54.15,63.08
2018-01-02 21:00:00,48.82,48.67,56.42
2018-01-02 22:00:00,45.14,47.46,49.85
2018-01-02 23:00:00,40.09,42.46,43.87

I read the dataframe as:

df=pd.read_csv('./test.csv',header = 0, index_col=0, parse_dates=True,
               usecols=['dates','rr.price','ax.price','be.price'])

I would like to compute the RMSE with all hourly data belonging to the same day.

I mean, I would like for each day apply

err = 0
for i in range(0:24):
    err = err + (dfr['rr.price'].values-dfr['be.price'].values).pow(2)

err = (err/24).pow(0.5)

As a result I should have a columns with 365 err value to which apply monthly mean with

resample('MS').mean()

With months, I use to do:

 dfr = dfr.assign(month=lambda x: x.index.month).groupby('month')
 rmse = dfr.apply(rmse,   s1='rr.price',s2='ax.price')

def rmse(group,s1,s2):
    if len(group) == 0:
        return np.nan
    s = (group[s1] - group[s2]).pow(2).sum()
    rmseO = np.sqrt(s / len(group)) 
    return rmseO

but it seems to not be the right thing with days.

Here I expected two values, one for each day

5.04
6.30

between 'rr.price' and 'be.price'.

Upvotes: 0

Views: 65

Answers (2)

diedro
diedro

Reputation: 613

This is my solution (thanks @pygirl):

  def rmse(group,s1,s2):
        if len(group) == 0:
            return np.nan
        s = (group[s1] - group[s2]).pow(2).sum()
        rmseO = np.sqrt(s / len(group)) 
        return rmseO 
    
    
dfr=pd.read_csv('./test.csv',header = 0, index_col=0, parse_dates=True,
                   usecols=['dates','rr.price','ax.price','be.price'])

dfr = dfr.assign(date=lambda x: x.index.date).groupby('date')
 
dfrM = pd.DataFrame()
dfrM['ax.rmse'] = dfr.apply(rmse,   s1='rr.price',s2='ax.price')
dfrM['be.rmse'] = dfr.apply(rmse,   s1='rr.price',s2='be.price')

dfrM.index = pd.to_datetime(dfrM.index,format='%Y-%m-%d')

dfrM= dfrM.resample('MS').mean()

Upvotes: 0

Pygirl
Pygirl

Reputation: 13349

You can try something like this:

This group by date and hour(giving you a hint for your previous question):

df.dates = pd.to_datetime(df.dates)
df.groupby([df.dates.dt.date, df.dates.dt.hour]).apply(lambda x: x['AA'].mean())

Coming to your question:

df.dates = pd.to_datetime(df.dates)
df.groupby([df.dates.dt.date]).apply(lambda x: ((x['AA']-x['BB'])**2).cumsum().div(24))

If you want rmse for each date:

df.groupby([df.dates.dt.date]).apply(lambda x: ((x['AA']-x['BB'])**2).cumsum()).reset_index(drop=True)[::23].pow(0.5).div(24)

Edit:

df.dates = pd.to_datetime(df.dates)
x = df.groupby([df.dates.dt.date]).apply(lambda x: ((x['rr.price']-x['ax.price'])**2).cumsum())[23::24].pow(0.5).div(24)

dates         
2018-01-01  23    1.027839
2018-01-02  47    2.519040
dtype: float64

Upvotes: 1

Related Questions