question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keepdims fails when taking mean

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

print(np.mean(np.zeros((30, 30)), axis=0, keepdims=True).shape,
pd.DataFrame(np.zeros((30, 30))).mean( axis=0, keepdims=True).shape,
np.mean(pd.DataFrame(np.zeros((30, 30))), axis=0, keepdims=True).shape)

Problem description

The keepdims argument is ignored when taking the mean of pandas dataframes, resulting in an array of lower dimensionality. Oddly, there’s no documentation for pd.DataFrame.mean suggesting keepdims should even be allowed as an argument, but it accepts it with no effect. This also modifies the behavior of np.mean.

Expected Output

(1, 30) (1,30,) (1,30,)

Output of pd.show_versions()

(1, 30) (30,) (30,)

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-75-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8

pandas: 0.17.1 nose: 1.3.7 pip: None setuptools: None Cython: None numpy: 1.11.0 scipy: 0.17.0 statsmodels: None IPython: 2.4.1 sphinx: None patsy: None dateutil: 2.4.2 pytz: 2014.10 blosc: None bottleneck: None tables: 3.2.2 numexpr: 2.4.3 matplotlib: 1.5.1 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: 4.4.1 html5lib: 0.999 httplib2: None apiclient: None sqlalchemy: None pymysql: None psycopg2: None Jinja2: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TomAugspurgercommented, May 3, 2017

Ok, let’s close for now. We can close for now. We can revisit a keepdims keyword in the future if there’s interest.

0reactions
seth-acommented, May 3, 2017

Ah, that makes sense. Nope, looking at the code it only checks that there’s a mean property, not even if it’s a method:

    if type(a) is not mu.ndarray:
        try:
            mean = a.mean
        except AttributeError:
            pass
        else:
            return mean(axis=axis, dtype=dtype, out=out, **kwargs)

Well, definitely not a pandas bug. Thanks for looking at that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy sum() got an 'keepdims' error - python - Stack Overflow
So it states here that if you're using a sub-class of numpy.ndarray , then you'll get this error if the corresponding sum function...
Read more >
How to use the NumPy mean function - Sharp Sight
The keepdims parameter enables you keep the dimensions of the output the same as the dimensions of the input.
Read more >
tf.math.reduce_mean | TensorFlow v2.11.0
Computes the mean of elements across dimensions of a tensor. ... input_tensor, axis=None, keepdims=False, name=None
Read more >
[Solved] NumPy RuntimeWarning: Mean of empty slice - Finxter
But when using it, NumPy raises a RuntimeWarning: Mean of empty slice ... The function doesn't cause an error if the array has...
Read more >
numpy.nanquantile — NumPy v1.24 Manual
If this is anything but the default value it will be passed through (in the special case of an empty array) to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found