BUG:fix rolling skew kurt floating issue
State: closed

- [x] closes #18044
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Numeric Operations
description: Arithmetic, Comparison, and Logical operations

Comments:
Thanks, the failure on Travis is unrelated.

Could you add a note to whatsnew/v0.21.1.txt and a unit test (maybe using the example from #18044)?
Hello @ogotaiking! Thanks for updating the PR.





Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on November 07, 2017 at 13:51 Hours UTC
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=h1) Report
> Merging [#18065](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/15fa4bd62c5c7ac7ac618a7e68742b7eb1f90d21?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/18065/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #18065      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       50120    50120              
==========================================
- Hits        45737    45728       -9     
- Misses       4383     4392       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.04% <ø> (ø)` | :arrow_up: |
| #single | `40.32% <ø> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.75% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=footer). Last update [15fa4bd...58299cb](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=h1) Report
> Merging [#18065](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/bc69dc69b168bae59d0ed6a461f18222b8137fa1?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/18065/graphs/tree.svg?token=eZ4WkYLtcO&src=pr&height=150&width=650)](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #18065      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       50124    50124              
==========================================
- Hits        45742    45733       -9     
- Misses       4382     4391       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.05% <ø> (ø)` | :arrow_up: |
| #single | `40.32% <ø> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.75% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=footer). Last update [bc69dc6...ee5a33d](https://codecov.io/gh/pandas-dev/pandas/pull/18065?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

pls rebase on master as well.
@jreback for the comments for return Nan for numerically unstable issue[ seems lost the comments during rebase]

consider the uniform distribution {Xi}, X1=X2=....Xn-1=Xn, the variance sum(Xi-Xm)^2/n should be Zero, but some floating issue cause the variance is in range(0,1e-14), and the variance is in the denominator, that will cause skew/kurt result with an extremely large number. so we can treat if the variance is less than 1e-14, it's  uniform distribution. 

The original code only checks if the variance == Zero,  and return Nan for uniform distribution, so I don't want to break this behavior and leave the result as Nan.

some reference : 
http://www.wolframalpha.com/input/?source=nav&i=skewness%7B1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1%7D

http://www.wolframalpha.com/input/?source=nav&i=kurtosis%7B1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1%7D

result is Nan(undefined)

@ogotaiking not sure of your last, is this a different case?
@jreback  same case. and also a reference from original skewness calculation code 

pandas/core/nanops.py
```python
def nanskew(values, axis=None, skipna=True):
    ...
    # floating point error
    m2 = _zero_out_fperr(m2) <- here m2 is eq B in rolling_skew
    m3 = _zero_out_fperr(m3)

    with np.errstate(invalid='ignore', divide='ignore'):
        result = (count * (count - 1) ** 0.5 / (count - 2)) * (m3 / m2 ** 1.5)
```

and the function _zero_out_fperr
```python
_zero_out_fperr(arg):
    if isinstance(arg, np.ndarray):
        with np.errstate(invalid='ignore'):
            return np.where(np.abs(arg) < 1e-14, 0, arg)
    else:
        return arg.dtype.type(0) if np.abs(arg) < 1e-14 else arg
```

so here we treat the B <1e-14 is eq 0 in calc_skew and calc_kurt is following the original skew/kurt behavior.


@ogotaiking ok I c. can you add update the comment in the code above to include a refernce to _zero_fperr (and maybe a reference in zero_fperr to calc_kurt/calc_skew), just so that future readers will know that these are connected. otherwise lgtm. ping when pushed / green.
lgtm merge on green 
thanks @ogotaiking