Search code examples
pythonpandasmodulepackagemonkeypatching

Monkey patch pandas: modify value_counts function at import


I want to change the value_counts function in pandas, and I try to figure out why it is not working.

If I do

>>> import pandas as pd
>>> pd.core.algorithms.value_counts = None

>>> pd.Series([1,2,3]).value_counts()

3    1
2    1
1    1
dtype: int64

I would expect to have a TypeError

However if I do

>>> import pandas as pd
>>> pd.core.base.value_counts = None #or a modified function

>>> pd.Series([1,2,3]).value_counts()

TypeError: 'NoneType' object is not callable

I get the TypeError .

What is the reason of this difference of behavior? How I can modify the pd.core.algorithms.value_counts function such that it changes the behavior of all classes that use it to construct value_counts methods?


Solution

  • Can you try using pd.core.base.IndexOpsMixin.value_counts = None ?

    This makes both pd.Series.value_counts and pd.Index.value_counts as None. Anything else you are looking at?

    Explanation:

    In pd.core.base there is an import from pandas.core.algorithms import duplicated, unique1d, value_counts

    So, pd.core.base.value_counts = pd.core.algorithms.value_counts

    Now, even if you make pd.core.algorithms.value_counts as None, the value_counts object in base still contains the old value_counts function. So, that doesn't work!

    Obviously, if you make pd.core.base.value_counts directly None - that works out.

    Which is the same as my solution really, as pd.core.base.IndexOpsMixin.value_counts just returns the pd.core.base.value_counts.