test_skew.py 841 B

123456789101112131415161718192021222324252627
  1. import numpy as np
  2. import pandas as pd
  3. import pandas._testing as tm
  4. def test_groupby_skew_equivalence():
  5. # Test that that groupby skew method (which uses libgroupby.group_skew)
  6. # matches the results of operating group-by-group (which uses nanops.nanskew)
  7. nrows = 1000
  8. ngroups = 3
  9. ncols = 2
  10. nan_frac = 0.05
  11. arr = np.random.default_rng(2).standard_normal((nrows, ncols))
  12. arr[np.random.default_rng(2).random(nrows) < nan_frac] = np.nan
  13. df = pd.DataFrame(arr)
  14. grps = np.random.default_rng(2).integers(0, ngroups, size=nrows)
  15. gb = df.groupby(grps)
  16. result = gb.skew()
  17. grpwise = [grp.skew().to_frame(i).T for i, grp in gb]
  18. expected = pd.concat(grpwise, axis=0)
  19. expected.index = expected.index.astype(result.index.dtype) # 32bit builds
  20. tm.assert_frame_equal(result, expected)