Skip to content

Commit 22cf22f

Browse files
Merge branch '1.3.x' into auto-backport-of-pr-43150-on-1.3.x
2 parents 17b373d + 6d8b38b commit 22cf22f

File tree

8 files changed

+46
-14
lines changed

8 files changed

+46
-14
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed regressions
2626
- Fixed regression in :func:`concat` where ``copy=False`` was not honored in ``axis=1`` Series concatenation (:issue:`42501`)
2727
- Regression in :meth:`Series.nlargest` and :meth:`Series.nsmallest` with nullable integer or float dtype (:issue:`42816`)
2828
- Fixed regression in :meth:`Series.quantile` with :class:`Int64Dtype` (:issue:`42626`)
29+
- Fixed regression in :meth:`Series.groupby` and :meth:`DataFrame.groupby` where supplying the ``by`` argument with a Series named with a tuple would incorrectly raise (:issue:`42731`)
2930

3031
.. ---------------------------------------------------------------------------
3132

doc/source/whatsnew/v1.3.3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ Fixed regressions
1818
- Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`)
1919
- Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`)
2020
- Fixed regression in :meth:`.GroupBy.quantile` which was failing with ``pandas.NA`` (:issue:`42849`)
21+
- Fixed regression in :meth:`.GroupBy.apply` where ``nan`` values were dropped even with ``dropna=False`` (:issue:`43205`)
2122
- Fixed regression in :meth:`merge` where ``on`` columns with ``ExtensionDtype`` or ``bool`` data types were cast to ``object`` in ``right`` and ``outer`` merge (:issue:`40073`)
2223
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2324
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
25+
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
2426

2527
.. ---------------------------------------------------------------------------
2628

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ def is_list_like(obj: object, allow_sets: bool = True) -> bool:
11051105
cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
11061106
return (
11071107
# equiv: `isinstance(obj, abc.Iterable)`
1108-
hasattr(obj, "__iter__") and not isinstance(obj, type)
1108+
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
11091109
# we do not count strings/unicode/bytes as list-like
11101110
and not isinstance(obj, (str, bytes))
11111111
# exclude zero-dimensional numpy arrays, effectively scalars

pandas/core/groupby/groupby.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,11 @@ def reset_identity(values):
10131013

10141014
if not not_indexed_same:
10151015
result = concat(values, axis=self.axis)
1016-
ax = self.filter(lambda x: True).axes[self.axis]
1016+
ax = (
1017+
self.filter(lambda x: True).axes[self.axis]
1018+
if self.dropna
1019+
else self._selected_obj._get_axis(self.axis)
1020+
)
10171021

10181022
# this is a very unfortunate situation
10191023
# we can't use reindex to restore the original order

pandas/core/groupby/grouper.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,11 @@ def is_in_obj(gpr) -> bool:
833833
return False
834834
try:
835835
return gpr is obj[gpr.name]
836-
except (KeyError, IndexError):
836+
except (KeyError, IndexError, InvalidIndexError):
837837
# IndexError reached in e.g. test_skip_group_keys when we pass
838838
# lambda here
839+
# InvalidIndexError raised on key-types inappropriate for index,
840+
# e.g. DatetimeIndex.get_loc(tuple())
839841
return False
840842

841843
for gpr, level in zip(keys, levels):

pandas/tests/dtypes/test_inference.py

+12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ def foo():
150150
foo()
151151

152152

153+
def test_is_list_like_iter_is_none():
154+
# GH 43373
155+
# is_list_like was yielding false positives with __iter__ == None
156+
class NotListLike:
157+
def __getitem__(self, item):
158+
return self
159+
160+
__iter__ = None
161+
162+
assert not inference.is_list_like(NotListLike())
163+
164+
153165
def test_is_sequence():
154166
is_seq = inference.is_sequence
155167
assert is_seq((1, 2))

pandas/tests/groupby/test_apply.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1102,25 +1102,19 @@ def test_apply_by_cols_equals_apply_by_rows_transposed():
11021102
tm.assert_frame_equal(by_cols, df)
11031103

11041104

1105-
def test_apply_dropna_with_indexed_same():
1105+
@pytest.mark.parametrize("dropna", [True, False])
1106+
def test_apply_dropna_with_indexed_same(dropna):
11061107
# GH 38227
1107-
1108+
# GH#43205
11081109
df = DataFrame(
11091110
{
11101111
"col": [1, 2, 3, 4, 5],
11111112
"group": ["a", np.nan, np.nan, "b", "b"],
11121113
},
11131114
index=list("xxyxz"),
11141115
)
1115-
result = df.groupby("group").apply(lambda x: x)
1116-
expected = DataFrame(
1117-
{
1118-
"col": [1, 4, 5],
1119-
"group": ["a", "b", "b"],
1120-
},
1121-
index=list("xxz"),
1122-
)
1123-
1116+
result = df.groupby("group", dropna=dropna).apply(lambda x: x)
1117+
expected = df.dropna() if dropna else df.iloc[[0, 3, 1, 2, 4]]
11241118
tm.assert_frame_equal(result, expected)
11251119

11261120

pandas/tests/groupby/test_grouping.py

+17
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ def test_groupby_dict_mapping(self):
396396
tm.assert_series_equal(result, result2)
397397
tm.assert_series_equal(result, expected2)
398398

399+
@pytest.mark.parametrize(
400+
"index",
401+
[
402+
[0, 1, 2, 3],
403+
["a", "b", "c", "d"],
404+
[Timestamp(2021, 7, 28 + i) for i in range(4)],
405+
],
406+
)
407+
def test_groupby_series_named_with_tuple(self, frame_or_series, index):
408+
# GH 42731
409+
obj = frame_or_series([1, 2, 3, 4], index=index)
410+
groups = Series([1, 0, 1, 0], index=index, name=("a", "a"))
411+
result = obj.groupby(groups).last()
412+
expected = frame_or_series([4, 3])
413+
expected.index.name = ("a", "a")
414+
tm.assert_equal(result, expected)
415+
399416
def test_groupby_grouper_f_sanity_checked(self):
400417
dates = date_range("01-Jan-2013", periods=12, freq="MS")
401418
ts = Series(np.random.randn(12), index=dates)

0 commit comments

Comments
 (0)