Skip to content

Commit cd25470

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#48159: TST: Fix interchange/plotting/groupby test warnings
1 parent 7313da5 commit cd25470

File tree

10 files changed

+45
-32
lines changed

10 files changed

+45
-32
lines changed

pandas/core/groupby/generic.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,9 @@ def func(df):
16341634
return df._constructor_sliced(result, index=res.index)
16351635

16361636
func.__name__ = "idxmax"
1637-
result = self._python_apply_general(func, self._obj_with_exclusions)
1637+
result = self._python_apply_general(
1638+
func, self._obj_with_exclusions, not_indexed_same=True
1639+
)
16381640
self._maybe_warn_numeric_only_depr("idxmax", result, numeric_only)
16391641
return result
16401642

@@ -1673,7 +1675,9 @@ def func(df):
16731675
return df._constructor_sliced(result, index=res.index)
16741676

16751677
func.__name__ = "idxmin"
1676-
result = self._python_apply_general(func, self._obj_with_exclusions)
1678+
result = self._python_apply_general(
1679+
func, self._obj_with_exclusions, not_indexed_same=True
1680+
)
16771681
self._maybe_warn_numeric_only_depr("idxmin", result, numeric_only)
16781682
return result
16791683

pandas/core/groupby/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,10 @@ def curried(x):
10401040
return self._obj_with_exclusions
10411041

10421042
result = self._python_apply_general(
1043-
curried, self._obj_with_exclusions, is_transform=is_transform
1043+
curried,
1044+
self._obj_with_exclusions,
1045+
is_transform=is_transform,
1046+
not_indexed_same=not is_transform,
10441047
)
10451048

10461049
if self._selected_obj.ndim != 1 and self.axis != 1 and result.ndim != 1:

pandas/core/interchange/from_dataframe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def set_nulls(
497497
null_pos = None
498498

499499
if null_kind == ColumnNullType.USE_SENTINEL:
500-
null_pos = data == sentinel_val
500+
null_pos = pd.Series(data) == sentinel_val
501501
elif null_kind in (ColumnNullType.USE_BITMASK, ColumnNullType.USE_BYTEMASK):
502502
assert validity, "Expected to have a validity buffer for the mask"
503503
valid_buff, valid_dtype = validity

pandas/plotting/_matplotlib/core.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from pandas.io.formats.printing import pprint_thing
5757
from pandas.plotting._matplotlib.converter import register_pandas_matplotlib_converters
5858
from pandas.plotting._matplotlib.groupby import reconstruct_data_with_by
59+
from pandas.plotting._matplotlib.misc import unpack_single_str_list
5960
from pandas.plotting._matplotlib.style import get_standard_colors
6061
from pandas.plotting._matplotlib.timeseries import (
6162
decorate_axes,
@@ -177,7 +178,7 @@ def __init__(
177178
# For `hist` plot, need to get grouped original data before `self.data` is
178179
# updated later
179180
if self.by is not None and self._kind == "hist":
180-
self._grouped = data.groupby(self.by)
181+
self._grouped = data.groupby(unpack_single_str_list(self.by))
181182

182183
self.kind = kind
183184

pandas/plotting/_matplotlib/hist.py

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def __init__(
6363
MPLPlot.__init__(self, data, **kwargs)
6464

6565
def _args_adjust(self):
66-
6766
# calculate bin number separately in different subplots
6867
# where subplots are created based on by argument
6968
if is_integer(self.bins):

pandas/plotting/_matplotlib/misc.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ def r(h):
479479

480480
def unpack_single_str_list(keys):
481481
# GH 42795
482-
if isinstance(keys, list):
483-
if len(keys) == 1 and isinstance(keys[0], str):
484-
keys = keys[0]
482+
if isinstance(keys, list) and len(keys) == 1:
483+
keys = keys[0]
485484
return keys

pandas/tests/groupby/test_counting.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,20 @@ def test_ngroup_cumcount_pair(self):
188188
tm.assert_series_equal(g.ngroup(), Series(ngroupd))
189189
tm.assert_series_equal(g.cumcount(), Series(cumcounted))
190190

191-
def test_ngroup_respects_groupby_order(self):
191+
def test_ngroup_respects_groupby_order(self, sort):
192192
np.random.seed(0)
193193
df = DataFrame({"a": np.random.choice(list("abcdef"), 100)})
194-
for sort_flag in (False, True):
195-
g = df.groupby(["a"], sort=sort_flag)
196-
df["group_id"] = -1
197-
df["group_index"] = -1
198-
199-
for i, (_, group) in enumerate(g):
200-
df.loc[group.index, "group_id"] = i
201-
for j, ind in enumerate(group.index):
202-
df.loc[ind, "group_index"] = j
203-
204-
tm.assert_series_equal(Series(df["group_id"].values), g.ngroup())
205-
tm.assert_series_equal(Series(df["group_index"].values), g.cumcount())
194+
g = df.groupby("a", sort=sort)
195+
df["group_id"] = -1
196+
df["group_index"] = -1
197+
198+
for i, (_, group) in enumerate(g):
199+
df.loc[group.index, "group_id"] = i
200+
for j, ind in enumerate(group.index):
201+
df.loc[ind, "group_index"] = j
202+
203+
tm.assert_series_equal(Series(df["group_id"].values), g.ngroup())
204+
tm.assert_series_equal(Series(df["group_index"].values), g.cumcount())
206205

207206
@pytest.mark.parametrize(
208207
"datetimelike",

pandas/tests/groupby/test_function.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1590,11 +1590,11 @@ def test_corrwith_with_1_axis():
15901590
tm.assert_series_equal(result, expected)
15911591

15921592

1593-
@pytest.mark.filterwarnings("ignore:The 'mad' method.*:FutureWarning")
1593+
@pytest.mark.filterwarnings("ignore:.* is deprecated:FutureWarning")
15941594
def test_multiindex_group_all_columns_when_empty(groupby_func):
15951595
# GH 32464
15961596
df = DataFrame({"a": [], "b": [], "c": []}).set_index(["a", "b", "c"])
1597-
gb = df.groupby(["a", "b", "c"])
1597+
gb = df.groupby(["a", "b", "c"], group_keys=False)
15981598
method = getattr(gb, groupby_func)
15991599
args = get_groupby_method_args(groupby_func, df)
16001600

pandas/tests/io/sas/test_sas7bdat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def data_test_ix(request, dirpath):
3333
for k in range(df.shape[1]):
3434
col = df.iloc[:, k]
3535
if col.dtype == np.int64:
36-
df.iloc[:, k] = df.iloc[:, k].astype(np.float64)
36+
df.isetitem(k, df.iloc[:, k].astype(np.float64))
3737
return df, test_ix
3838

3939

pandas/tests/plotting/frame/test_hist_box_by.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class TestHistWithBy(TestPlotBase):
8383
)
8484
def test_hist_plot_by_argument(self, by, column, titles, legends, hist_df):
8585
# GH 15079
86-
axes = _check_plot_works(hist_df.plot.hist, column=column, by=by)
86+
axes = _check_plot_works(
87+
hist_df.plot.hist, column=column, by=by, default_axes=True
88+
)
8789
result_titles = [ax.get_title() for ax in axes]
8890
result_legends = [
8991
[legend.get_text() for legend in ax.get_legend().texts] for ax in axes
@@ -120,7 +122,7 @@ def test_hist_plot_by_0(self, by, column, titles, legends, hist_df):
120122
df = hist_df.copy()
121123
df = df.rename(columns={"C": 0})
122124

123-
axes = _check_plot_works(df.plot.hist, column=column, by=by)
125+
axes = _check_plot_works(df.plot.hist, default_axes=True, column=column, by=by)
124126
result_titles = [ax.get_title() for ax in axes]
125127
result_legends = [
126128
[legend.get_text() for legend in ax.get_legend().texts] for ax in axes
@@ -142,7 +144,9 @@ def test_hist_plot_empty_list_string_tuple_by(self, by, column, hist_df):
142144
# GH 15079
143145
msg = "No group keys passed"
144146
with pytest.raises(ValueError, match=msg):
145-
_check_plot_works(hist_df.plot.hist, column=column, by=by)
147+
_check_plot_works(
148+
hist_df.plot.hist, default_axes=True, column=column, by=by
149+
)
146150

147151
@pytest.mark.slow
148152
@pytest.mark.parametrize(
@@ -274,7 +278,9 @@ class TestBoxWithBy(TestPlotBase):
274278
)
275279
def test_box_plot_by_argument(self, by, column, titles, xticklabels, hist_df):
276280
# GH 15079
277-
axes = _check_plot_works(hist_df.plot.box, column=column, by=by)
281+
axes = _check_plot_works(
282+
hist_df.plot.box, default_axes=True, column=column, by=by
283+
)
278284
result_titles = [ax.get_title() for ax in axes]
279285
result_xticklabels = [
280286
[label.get_text() for label in ax.get_xticklabels()] for ax in axes
@@ -313,7 +319,7 @@ def test_box_plot_by_0(self, by, column, titles, xticklabels, hist_df):
313319
df = hist_df.copy()
314320
df = df.rename(columns={"C": 0})
315321

316-
axes = _check_plot_works(df.plot.box, column=column, by=by)
322+
axes = _check_plot_works(df.plot.box, default_axes=True, column=column, by=by)
317323
result_titles = [ax.get_title() for ax in axes]
318324
result_xticklabels = [
319325
[label.get_text() for label in ax.get_xticklabels()] for ax in axes
@@ -335,7 +341,7 @@ def test_box_plot_with_none_empty_list_by(self, by, column, hist_df):
335341
# GH 15079
336342
msg = "No group keys passed"
337343
with pytest.raises(ValueError, match=msg):
338-
_check_plot_works(hist_df.plot.box, column=column, by=by)
344+
_check_plot_works(hist_df.plot.box, default_axes=True, column=column, by=by)
339345

340346
@pytest.mark.slow
341347
@pytest.mark.parametrize(
@@ -351,7 +357,9 @@ def test_box_plot_with_none_empty_list_by(self, by, column, hist_df):
351357
)
352358
def test_box_plot_layout_with_by(self, by, column, layout, axes_num, hist_df):
353359
# GH 15079
354-
axes = _check_plot_works(hist_df.plot.box, column=column, by=by, layout=layout)
360+
axes = _check_plot_works(
361+
hist_df.plot.box, default_axes=True, column=column, by=by, layout=layout
362+
)
355363
self._check_axes_shape(axes, axes_num=axes_num, layout=layout)
356364

357365
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)