Taking first value in a rolling window that is not numeric

pierre_j :

This question follows one I previously asked here, and that was answered for numeric values.

I raise this 2nd one now relative to data of Period type.

While the example given below appears simple, I have actually windows that are of variable size. Interested in the 1st row of the windows, I am looking for a technic that makes use of this definition.

import pandas as pd
from random import seed, randint

# DataFrame
pi1h = pd.period_range(start='2020-01-01 00:00+00:00', end='2020-01-02 00:00+00:00', freq='1h')

seed(1)
values = [randint(0, 10) for ts in pi1h]

df = pd.DataFrame({'Values' : values, 'Period' : pi1h}, index=pi1h)

# This works (numeric type)
df['first'] = df['Values'].rolling(3).agg(lambda rows: rows[0])

# This doesn't (Period type)
df['OpeningPeriod'] = df['Period'].rolling(3).agg(lambda rows: rows[0])

Result of 2nd command

DataError: No numeric types to aggregate

Please, any idea? Thanks for any help! Bests,

Grzegorz Skibinski :

First row of rolling window of size 3 means row, which is 2 rows above the current - just use pd.Series.shift(2):

df['OpeningPeriod'] = df['Period'].shift(2)

For the variable size (for the sake of example- I took Values column as this variable size):

import numpy as np

x=(np.arange(len(df))-df['Values'])

df['OpeningPeriod'] = np.where(x.ge(0), df.loc[df.index[x.tolist()], 'Period'], np.nan)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=383184&siteId=1