BUG: the sample skewness is wrong computed
State: closed

- [ ] closes #xxxx
- [ ] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

Right formula can be found here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skew.html

Labels:

Comments:
needs a test
Thanks @anmyachev , good catch! I believe you're right, there is a missing bracket: the code is currently computing

![current](https://latex.codecogs.com/gif.latex?n%20%5Csqrt%7Bn-1%7D)

rather than

![correct](https://latex.codecogs.com/gif.latex?%5Csqrt%7Bn%28n-1%29%7D)

, where _n_ is the sample size.

Before this PR can be accepted, you need to write tests / make sure you don't break existing ones - see [contributing to the code base](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#contributing-to-the-code-base):

> pandas is serious about testing and strongly encourages contributors to embrace test-driven development (TDD). This development process “relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test.” So, before actually writing any code, you should write your tests. Often the test can be taken from the original GitHub issue. However, it is always worth considering additional use cases and writing corresponding tests.
> 
> Adding tests is one of the most common requests after code is pushed to pandas. Therefore, it is worth getting in the habit of writing tests ahead of time so this is never an issue.

So I think this is actually correct as is (although it's a little confusing the way it's written, the output seems to agree with `scipy`).

If we look starting at line https://github.com/pandas-dev/pandas/blob/master/pandas/core/nanops.py#L1004 `m2` and `m3` are actually the _sums_ of squares and cubes, and not the central moments as you'd expect. In order to normalize those we have to multiply by `(1 / n) / (1 / n) ** 1.5` (see formula from Wikipedia) which is equal to `n ** 0.5`, so we get the factor of `n` instead of `n ** 0.5`.

https://en.wikipedia.org/wiki/Skewness#Sample_skewness
> So I think this is actually correct as is (although it's a little confusing the way it's written, the output seems to agree with `scipy`).
> 
> If we look starting at line https://github.com/pandas-dev/pandas/blob/master/pandas/core/nanops.py#L1004 `m2` and `m3` are actually the _sums_ of squares and cubes, and not the central moments as you'd expect. In order to normalize those we have to multiply by `(1 / n) / (1 / n) ** 1.5` (see formula from Wikipedia) which is equal to `n ** 0.5`, so we get the factor of `n` instead of `n ** 0.5`.
> 
> https://en.wikipedia.org/wiki/Skewness#Sample_skewness

@dsaxton you're right.

I think that this should be written more obviously, even though in this way we save on two division operations. What do you think about it?
> I think that this should be written more obviously, even though in this way we save on two division operations. What do you think about it?

I think taking a mean instead of a sum and keeping your original change could make this formula less confusing (and shouldn't affect performance). Thoughts @MarcoGorelli @jbrockmendel ?
@dsaxton - you're right, thanks - I wish I'd checked this carefully before commenting now :)

> I think that this should be written more obviously, even though in this way we save on two division operations. What do you think about it?

If it's correct and matches the Wiki formula (with a bit of simplification)

![image](https://user-images.githubusercontent.com/33491632/75289365-100c7c80-5816-11ea-94e7-5c6d00a3243e.png)


then IMO it's not worth touching it
1) Do we have sufficient testing for this?
2) Performance impact is an empirical question
3) My intuition is that in choosing between centered vs non-centered moments the things to worry about are floating point approximation and overflow errors.  cc @josef-pkt @ChadFulton know more about this than me. 
@anmyachev is this still active?
Closing.  @anmyachev feel free to re-open if you want to address the comments above