Flab98
Flab98

Reputation: 31

Pandas df.mean() throws TypeError: 'NoneType' object is not callable or returns empty Series

The title says it all.

df = pd.DataFrame({"A":np.array([1,2,3,4]),"B":np.array([1,2,3,4])})
df_mean = df.mean(axis=0)
print(df_mean)

The code above outputs an empty series object:

Series([], dtype: float64)

Using df.mean() on a dataframe filled with MNIST data throws the following stacktrace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-bab38039484e> in <module>
      2 
      3 X_train_class, y_train_class, X_valid_class, \
----> 4         y_valid_class, X_test_class, y_test_class = prepare_load_classification_data()
      5 X_train_class.mean()
      6 # ebm = ExplainableBoostingClassifier()

<ipython-input-37-b1dcfdd01adc> in prepare_load_classification_data()
     45     train_features, train_labels, dev_features, \
     46         dev_labels, test_features, test_labels = load_data()
---> 47     feature_mean, label_mean = train_features.mean(axis=0), train_labels.mean(axis=0)
     48 
     49     train_features = pd.DataFrame(data=np.where(train_features > feature_mean, 1, 0), columns=FEATURE_NAMES)

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\generic.py in mean(self, axis, skipna, level, numeric_only, **kwargs)
  11107         )
  11108         def mean(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs):
> 11109             return NDFrame.mean(self, axis, skipna, level, numeric_only, **kwargs)
  11110 
  11111         # pandas\core\generic.py:10924: error: Cannot assign to a method

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\generic.py in mean(self, axis, skipna, level, numeric_only, **kwargs)
  10718     def mean(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs):
  10719         return self._stat_function(
> 10720             "mean", nanops.nanmean, axis, skipna, level, numeric_only, **kwargs
  10721         )
  10722 

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\generic.py in _stat_function(self, name, func, axis, skipna, level, numeric_only, **kwargs)
  10703             return self._agg_by_level(name, axis=axis, level=level, skipna=skipna)
  10704         return self._reduce(
> 10705             func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  10706         )
  10707 

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\series.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   4150                 )
   4151             with np.errstate(all="ignore"):
-> 4152                 return op(delegate, skipna=skipna, **kwds)
   4153 
   4154     def _reindex_indexer(self, new_index, indexer, copy):

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\nanops.py in _f(*args, **kwargs)
     69             try:
     70                 with np.errstate(invalid="ignore"):
---> 71                     return f(*args, **kwargs)
     72             except ValueError as e:
     73                 # we want to transform an object array

c:\users\fmijs\anaconda3\lib\site-packages\pandas\core\nanops.py in f(values, axis, skipna, **kwds)
    122                     #  TypeError if called
    123                     kwds.pop("mask", None)
--> 124                     result = bn_func(values, axis=axis, **kwds)
    125 
    126                     # prefer to treat inf/-inf as NA, but must compute the func

TypeError: 'NoneType' object is not callable

It appears to be related to a somehow corrupted instalation of pandas or numpy but after reinstalling both downgrading or starting a new Conda environment the issues still remain. Any help would be greatly apreciated!

Upvotes: 1

Views: 2034

Answers (2)

Shahaf Cohen-Yashar
Shahaf Cohen-Yashar

Reputation: 11

for me, the issue was caused by importing pandas before importing numpy

so instead of:

import pandas as pd
import numpy as np

i changed it to

import numpy as np
import pandas as pd

and it fixed the issue

Upvotes: 1

andrewalenta
andrewalenta

Reputation: 81

I ran it with pandas 1.1.3 and numpy 1.19.2 and worked. I ran it also with pandas 1.2.3 and numpy 1.19.5 in a Jupyter and worked.

I updated all and ran it with pandas 1.2.4 and numpy 1.20.2 and it worked.

So either it is because of numpy or the reason is something different.

Do you have really just this code? Or is there other code that might interfere with your snippet?

Upvotes: 2

Related Questions