Home > Net >  Pandas groupby and get yesterday min value
Pandas groupby and get yesterday min value

Time:04-17

How do I modify my code to have groupby return the previous days min instead of current days min Please see desired output below as this shows exactly what I am trying to achieve.

Data

np.random.seed(5)
series = pd.Series(np.random.choice([1,3,5], 10), index = pd.date_range('2014-01-01', '2014-01-04', freq = '8h'))
series


2014-01-01 00:00:00    5
2014-01-01 08:00:00    3
2014-01-01 16:00:00    5
2014-01-02 00:00:00    5
2014-01-02 08:00:00    1
2014-01-02 16:00:00    3
2014-01-03 00:00:00    1
2014-01-03 08:00:00    1
2014-01-03 16:00:00    5
2014-01-04 00:00:00    1

Output after groupby

series.groupby(series.index.date).transform(min)

2014-01-01 00:00:00    3
2014-01-01 08:00:00    3
2014-01-01 16:00:00    3
2014-01-02 00:00:00    1
2014-01-02 08:00:00    1
2014-01-02 16:00:00    1
2014-01-03 00:00:00    1
2014-01-03 08:00:00    1
2014-01-03 16:00:00    1
2014-01-04 00:00:00    1

Desired output (yesterday min)

2014-01-01 00:00:00    Nan
2014-01-01 08:00:00    Nan
2014-01-01 16:00:00    Nan
2014-01-02 00:00:00    3
2014-01-02 08:00:00    3
2014-01-02 16:00:00    3
2014-01-03 00:00:00    1
2014-01-03 08:00:00    1
2014-01-03 16:00:00    1
2014-01-04 00:00:00    1

CodePudding user response:

You can swap the index to just the date, calculate min per day, shift it and swap the original index back:

# Swap the index to just the date component
s = series.set_axis(series.index.date)

# Calculate the min per day, and shift it
t = s.groupby(level=0).min().shift()

# Final assembly
s[t.index] = t
s.index = series.index

CodePudding user response:

Let us do reindex

series[:] = series.groupby(series.index.date).min().shift().reindex(series.index.date)
series
Out[370]: 
2014-01-01 00:00:00    NaN
2014-01-01 08:00:00    NaN
2014-01-01 16:00:00    NaN
2014-01-02 00:00:00    1.0
2014-01-02 08:00:00    1.0
2014-01-02 16:00:00    1.0
2014-01-03 00:00:00    3.0
2014-01-03 08:00:00    3.0
2014-01-03 16:00:00    3.0
2014-01-04 00:00:00    1.0
Freq: 8H, dtype: float64
  • Related