Pandas series operator as function parameter

Michael

Background

I want to perform a calculation on a Pandas Series. This calculation involves min and max. The calculation is used twice. In both cases it is the same calculation except for the min or max functions, which should be different.

I've created a function to perform this calculation:

def my_calc(my_series):
   return my_series.rolling(...).max()

The problem

I don't know how to pass max as a parameter of my_calc.

Attempts

  • This solution works only for basic operators.
  • Currently I use my_calc(my_series).max() and my_calc(my_series).min()
Ignacio Vazquez-Abrams

There's really no pretty way to do it.

def my_calc(my_series, func=max):
   if not func in {'min', 'max'}:
      raise ValueError('{} is not a valid method name!'.format(func))
   return getattr(my_series.rolling(...), func)()

foo = my_calc(some_series, 'min')
bar = my_calc(some_series, 'max')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Operator in Function parameter?

From Dev

What is the name parameter in Pandas Series?

From Dev

overloaded operator with std::function parameter

From Dev

Finding a string in pandas.Series using the IN operator

From Dev

Incorrect results when using "in" operator with Pandas series?

From Dev

Function with conditional statement to pandas series

From Dev

Applying strptime function to pandas series

From Dev

How to pass text parameter to stored function for `IN` operator

From Dev

Assign function as default parameter using & operator

From Dev

How to use php ternary operator in the parameter of a function?

From Dev

Iterating through rows of a Pandas series within a function

From Dev

what's the inverse of the quantile function on a pandas Series?

From Dev

Apply function on cumulative values of pandas series

From Dev

Pandas cumulative function of series with dates and NaT

From Dev

Pandas function for generating series from dataframe

From Dev

Faster alternative to Series.add function in pandas

From Dev

How to speed up rank function in pandas series?

From Dev

Pandas: Timing difference between Function and Apply to Series

From Dev

Use Map function with subsets of a pandas series or dataframe

From Dev

Pandas Series.ne operator returning unexpected result against two slices of same Series

From Dev

How to pass a rvalue reference parameter to a template operator() function in C++?

From Dev

How to overload operator<< with a function as parameter (like endl for cout)?

From Dev

How to pass a rvalue reference parameter to a template operator() function in C++?

From Dev

Return output of function that takes pandas dataframe as a parameter

From Dev

Integer parameter in pandas DataFrame.drop function

From Dev

python pandas: apply a function with arguments to a series. Update

From Dev

Pandas Filter function returned a Series, but expected a scalar bool

From Dev

how do I apply normalize function to pandas string series?

From Dev

Pandas function that iterates over values in a series with case statements

Related Related

  1. 1

    Operator in Function parameter?

  2. 2

    What is the name parameter in Pandas Series?

  3. 3

    overloaded operator with std::function parameter

  4. 4

    Finding a string in pandas.Series using the IN operator

  5. 5

    Incorrect results when using "in" operator with Pandas series?

  6. 6

    Function with conditional statement to pandas series

  7. 7

    Applying strptime function to pandas series

  8. 8

    How to pass text parameter to stored function for `IN` operator

  9. 9

    Assign function as default parameter using & operator

  10. 10

    How to use php ternary operator in the parameter of a function?

  11. 11

    Iterating through rows of a Pandas series within a function

  12. 12

    what's the inverse of the quantile function on a pandas Series?

  13. 13

    Apply function on cumulative values of pandas series

  14. 14

    Pandas cumulative function of series with dates and NaT

  15. 15

    Pandas function for generating series from dataframe

  16. 16

    Faster alternative to Series.add function in pandas

  17. 17

    How to speed up rank function in pandas series?

  18. 18

    Pandas: Timing difference between Function and Apply to Series

  19. 19

    Use Map function with subsets of a pandas series or dataframe

  20. 20

    Pandas Series.ne operator returning unexpected result against two slices of same Series

  21. 21

    How to pass a rvalue reference parameter to a template operator() function in C++?

  22. 22

    How to overload operator<< with a function as parameter (like endl for cout)?

  23. 23

    How to pass a rvalue reference parameter to a template operator() function in C++?

  24. 24

    Return output of function that takes pandas dataframe as a parameter

  25. 25

    Integer parameter in pandas DataFrame.drop function

  26. 26

    python pandas: apply a function with arguments to a series. Update

  27. 27

    Pandas Filter function returned a Series, but expected a scalar bool

  28. 28

    how do I apply normalize function to pandas string series?

  29. 29

    Pandas function that iterates over values in a series with case statements

HotTag

Archive