22
33import pickle
44from copy import deepcopy
5+ from distutils .version import LooseVersion
56from textwrap import dedent
67import warnings
78
1415 DataArray , Dataset , IndexVariable , Variable , align , broadcast , set_options )
1516from xarray .convert import from_cdms2
1617from xarray .coding .times import CFDatetimeCoder , _import_cftime
17- from xarray .core .common import full_like
18+ from xarray .core .common import full_like , ALL_DIMS
1819from xarray .core .pycompat import OrderedDict , iteritems
1920from xarray .tests import (
2021 ReturnItem , TestCase , assert_allclose , assert_array_equal , assert_equal ,
@@ -2000,15 +2001,15 @@ def test_groupby_sum(self):
20002001 self .x [:, 10 :].sum (),
20012002 self .x [:, 9 :10 ].sum ()]).T ),
20022003 'abc' : Variable (['abc' ], np .array (['a' , 'b' , 'c' ]))})['foo' ]
2003- assert_allclose (expected_sum_all , grouped .reduce (np .sum ))
2004- assert_allclose (expected_sum_all , grouped .sum ())
2004+ assert_allclose (expected_sum_all , grouped .reduce (np .sum , dim = ALL_DIMS ))
2005+ assert_allclose (expected_sum_all , grouped .sum (ALL_DIMS ))
20052006
20062007 expected = DataArray ([array ['y' ].values [idx ].sum () for idx
20072008 in [slice (9 ), slice (10 , None ), slice (9 , 10 )]],
20082009 [['a' , 'b' , 'c' ]], ['abc' ])
20092010 actual = array ['y' ].groupby ('abc' ).apply (np .sum )
20102011 assert_allclose (expected , actual )
2011- actual = array ['y' ].groupby ('abc' ).sum ()
2012+ actual = array ['y' ].groupby ('abc' ).sum (ALL_DIMS )
20122013 assert_allclose (expected , actual )
20132014
20142015 expected_sum_axis1 = Dataset (
@@ -2019,6 +2020,27 @@ def test_groupby_sum(self):
20192020 assert_allclose (expected_sum_axis1 , grouped .reduce (np .sum , 'y' ))
20202021 assert_allclose (expected_sum_axis1 , grouped .sum ('y' ))
20212022
2023+ def test_groupby_warning (self ):
2024+ array = self .make_groupby_example_array ()
2025+ grouped = array .groupby ('y' )
2026+ with pytest .warns (FutureWarning ):
2027+ grouped .sum ()
2028+
2029+ @pytest .mark .skipif (LooseVersion (xr .__version__ ) < LooseVersion ('0.12' ),
2030+ reason = "not to forget the behavior change" )
2031+ def test_groupby_sum_default (self ):
2032+ array = self .make_groupby_example_array ()
2033+ grouped = array .groupby ('abc' )
2034+
2035+ expected_sum_all = Dataset (
2036+ {'foo' : Variable (['x' , 'abc' ],
2037+ np .array ([self .x [:, :9 ].sum (axis = - 1 ),
2038+ self .x [:, 10 :].sum (axis = - 1 ),
2039+ self .x [:, 9 :10 ].sum (axis = - 1 )]).T ),
2040+ 'abc' : Variable (['abc' ], np .array (['a' , 'b' , 'c' ]))})['foo' ]
2041+
2042+ assert_allclose (expected_sum_all , grouped .sum ())
2043+
20222044 def test_groupby_count (self ):
20232045 array = DataArray (
20242046 [0 , 0 , np .nan , np .nan , 0 , 0 ],
@@ -2099,9 +2121,9 @@ def test_groupby_math(self):
20992121 assert_identical (expected , actual )
21002122
21012123 grouped = array .groupby ('abc' )
2102- expected_agg = (grouped .mean () - np .arange (3 )).rename (None )
2124+ expected_agg = (grouped .mean (ALL_DIMS ) - np .arange (3 )).rename (None )
21032125 actual = grouped - DataArray (range (3 ), [('abc' , ['a' , 'b' , 'c' ])])
2104- actual_agg = actual .groupby ('abc' ).mean ()
2126+ actual_agg = actual .groupby ('abc' ).mean (ALL_DIMS )
21052127 assert_allclose (expected_agg , actual_agg )
21062128
21072129 with raises_regex (TypeError , 'only support binary ops' ):
@@ -2175,7 +2197,7 @@ def test_groupby_multidim(self):
21752197 ('lon' , DataArray ([5 , 28 , 23 ],
21762198 coords = [('lon' , [30. , 40. , 50. ])])),
21772199 ('lat' , DataArray ([16 , 40 ], coords = [('lat' , [10. , 20. ])]))]:
2178- actual_sum = array .groupby (dim ).sum ()
2200+ actual_sum = array .groupby (dim ).sum (ALL_DIMS )
21792201 assert_identical (expected_sum , actual_sum )
21802202
21812203 def test_groupby_multidim_apply (self ):
0 commit comments