But lets try to achieve this in two steps:
In[41]: df = df.set_index('date')
In[42]: pd.rolling_apply(df, window = 5, func = lambda x: x.argmin(), min_periods = 1)
Out[42]:
value
date
2014 - 01 - 20 0
2014 - 01 - 21 0
2014 - 01 - 22 0
2014 - 01 - 23 3
2014 - 01 - 24 4
2014 - 01 - 25 3
2014 - 01 - 26 2
This gives you the index in the window where the minimum is found. But, this index is for that particular window and not for the entire dataframe. So let's add the start of the window, and then use this integer location to retrieve the correct index locations index:
In[45]: ilocs_window = pd.rolling_apply(df, window = 5, func = lambda x: x.argmin(), min_periods = 1)
In[46]: ilocs = ilocs_window['value'] + ([0, 0, 0, 0] + range(len(ilocs_window) - 4))
In[47]: ilocs
Out[47]:
date
2014 - 01 - 20 0
2014 - 01 - 21 0
2014 - 01 - 22 0
2014 - 01 - 23 3
2014 - 01 - 24 4
2014 - 01 - 25 4
2014 - 01 - 26 4
Name: value, dtype: float64
In[48]: df.index.take(ilocs)
Out[48]:
Index([u '2014-01-20', u '2014-01-20', u '2014-01-20', u '2014-01-23',
u '2014-01-24', u '2014-01-24', u '2014-01-24'
],
dtype = 'object', name = u 'date')
In[49]: df['rolling_min_date'] = df.index.take(ilocs)
In[50]: df
Out[50]:
value rolling_min_date
date
2014 - 01 - 20 10 2014 - 01 - 20
2014 - 01 - 21 12 2014 - 01 - 20
2014 - 01 - 22 13 2014 - 01 - 20
2014 - 01 - 23 9 2014 - 01 - 23
2014 - 01 - 24 7 2014 - 01 - 24
2014 - 01 - 25 12 2014 - 01 - 24
2014 - 01 - 26 11 2014 - 01 - 24
The same can be done for the maximum:
ilocs_window = pd.rolling_apply(df, window = 5, func = lambda x: x.argmax(), min_periods = 1)
ilocs = ilocs_window['value'] + ([0, 0, 0, 0] + range(len(ilocs_window) - 4))
df['rolling_max_date'] = df.index.take(ilocs)
%MAX returns the maximum value of its operands and %MIN returns the minimum value of its operands. Otherwise, the rules and behavior of these built-in functions are identical.,If all the operands have data type timestamp, the result has data type timestamp and the number of fractional digits of the returned value is the maximum number of fractional digits of any of the operands. ,If the built-in function is used in a Declaration statement, the result is the operand with the higher (%MAX) or lower (%MIN) value.,If all operands are hexadecimal literals, the return value is alphanumeric with CCSID(*HEX). Otherwise, hexadecimal literals are treated as numeric or character depending on the other operands of the built-in function.
% MAX(item1: item2 { : item3 { item4... } })
%MAX(item1 : item2 {: item3 { item4 ... } })
% MIN(item1: item2 { : item3 { item4... } })
DCL - S arr1 CHAR(10) DIM(3);
DCL - S arr2 CHAR(10) DIM(5);
DCL - S arr3 CHAR(10) DIM( % MAX( % ELEM(arr1): % ELEM(arr2)));
DCL - S arr1 CHAR(10) DIM(3);
DCL - S arr2 CHAR(10) DIM(5);
DCL - S arr3 CHAR(10) DIM( % MAX( % ELEM(arr1): % ELEM(arr2)));
DCL - S triangleArea PACKED(7: 2); DCL - S squareArea PACKED(7: 2); DCL - S circleArea PACKED(5: 2); DCL - S size PACKED(7: 2); size = % MIN(triangleArea: squareArea: circleArea);
This function is used to compute the minimum of the values passed in its argument and lexicographically smallest value if strings are passed as arguments. ,This function is used to compute the maximum of the values passed in its argument and lexicographically largest value if strings are passed as arguments. ,One of the practical application among many are finding lexicographically largest and smallest of Strings i.e String appearing first in dictionary or last. ,This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many elements as arguments and also support strings in their arguments, hence allowing to display lexicographically smallest or largest string as well. Detailed functionality are explained below.
This function is used to compute the maximum of the values passed in its argument and lexicographically largest value if strings are passed as arguments.
Syntax:
max(a, b, c, .., key,
default)
Parameters:
a, b, c, ..: similar type of data.
key: key
function where the iterables are passed and comparison is performed
default: default value is passed
if the given iterable is empty
Return Value:
Returns the maximum of all the arguments.
Exceptions:
Returns TypeError when conflicting types are compared.
Output :
Maximum of 4, 12, 43.3, 19 and 100 is: 100
This function is used to compute the minimum of the values passed in its argument and lexicographically smallest value if strings are passed as arguments.
Syntax:
min(a, b, c, .., key,
default)
Parameters:
a, b, c, ..: similar type of data.
key: key
function where the iterables are passed and comparison is performed
default: default value is passed
if the given iterable is empty
Return Value:
Returns the minimum of all the arguments.
Exceptions:
Returns TypeError when conflicting types are compared.