Skip to content

Commit 80d362b

Browse files
authored
Merge branch 'pandas-dev:main' into main
2 parents 8a29b68 + cec873e commit 80d362b

File tree

4 files changed

+21
-73
lines changed

4 files changed

+21
-73
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Removal of prior version deprecations/changes
191191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192192
- :func:`read_excel`, :func:`read_json`, :func:`read_html`, and :func:`read_xml` no longer accept raw string or byte representation of the data. That type of data must be wrapped in a :py:class:`StringIO` or :py:class:`BytesIO` (:issue:`53767`)
193193
- :meth:`Series.dt.to_pydatetime` now returns a :class:`Series` of :py:class:`datetime.datetime` objects (:issue:`52459`)
194+
- :meth:`SeriesGroupBy.agg` no longer pins the name of the group to the input passed to the provided ``func`` (:issue:`51703`)
194195
- All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`)
195196
- All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`)
196197
- All arguments in :meth:`Index.sort_values` are now keyword only (:issue:`56493`)
@@ -238,7 +239,6 @@ Removal of prior version deprecations/changes
238239
- Removed unused arguments ``*args`` and ``**kwargs`` in :class:`Resampler` methods (:issue:`50977`)
239240
- Unrecognized timezones when parsing strings to datetimes now raises a ``ValueError`` (:issue:`51477`)
240241

241-
242242
.. ---------------------------------------------------------------------------
243243
.. _whatsnew_300.performance:
244244

Diff for: pandas/core/groupby/generic.py

+2-50
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@
6262
)
6363
import pandas.core.common as com
6464
from pandas.core.frame import DataFrame
65-
from pandas.core.groupby import (
66-
base,
67-
ops,
68-
)
65+
from pandas.core.groupby import base
6966
from pandas.core.groupby.groupby import (
7067
GroupBy,
7168
GroupByPlot,
@@ -373,32 +370,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
373370
index=self._grouper.result_index,
374371
dtype=obj.dtype,
375372
)
376-
377-
if self._grouper.nkeys > 1:
378-
return self._python_agg_general(func, *args, **kwargs)
379-
380-
try:
381-
return self._python_agg_general(func, *args, **kwargs)
382-
except KeyError:
383-
# KeyError raised in test_groupby.test_basic is bc the func does
384-
# a dictionary lookup on group.name, but group name is not
385-
# pinned in _python_agg_general, only in _aggregate_named
386-
result = self._aggregate_named(func, *args, **kwargs)
387-
388-
warnings.warn(
389-
"Pinning the groupby key to each group in "
390-
f"{type(self).__name__}.agg is deprecated, and cases that "
391-
"relied on it will raise in a future version. "
392-
"If your operation requires utilizing the groupby keys, "
393-
"iterate over the groupby object instead.",
394-
FutureWarning,
395-
stacklevel=find_stack_level(),
396-
)
397-
398-
# result is a dict whose keys are the elements of result_index
399-
result = Series(result, index=self._grouper.result_index)
400-
result = self._wrap_aggregated_output(result)
401-
return result
373+
return self._python_agg_general(func, *args, **kwargs)
402374

403375
agg = aggregate
404376

@@ -527,26 +499,6 @@ def _wrap_applied_output(
527499
result.index = default_index(len(result))
528500
return result
529501

530-
def _aggregate_named(self, func, *args, **kwargs):
531-
# Note: this is very similar to _aggregate_series_pure_python,
532-
# but that does not pin group.name
533-
result = {}
534-
initialized = False
535-
536-
for name, group in self._grouper.get_iterator(self._obj_with_exclusions):
537-
# needed for pandas/tests/groupby/test_groupby.py::test_basic_aggregations
538-
object.__setattr__(group, "name", name)
539-
540-
output = func(group, *args, **kwargs)
541-
output = ops.extract_result(output)
542-
if not initialized:
543-
# We only do this validation on the first iteration
544-
ops.check_result_array(output, group.dtype)
545-
initialized = True
546-
result[name] = output
547-
548-
return result
549-
550502
__examples_series_doc = dedent(
551503
"""
552504
>>> ser = pd.Series([390.0, 350.0, 30.0, 20.0],

Diff for: pandas/io/excel/_base.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from pandas.core.dtypes.common import (
4545
is_bool,
46+
is_file_like,
4647
is_float,
4748
is_integer,
4849
is_list_like,
@@ -1523,20 +1524,25 @@ def __init__(
15231524
# Always a string
15241525
self._io = stringify_path(path_or_buffer)
15251526

1526-
# Determine xlrd version if installed
1527-
if import_optional_dependency("xlrd", errors="ignore") is None:
1528-
xlrd_version = None
1529-
else:
1530-
import xlrd
1531-
1532-
xlrd_version = Version(get_version(xlrd))
1533-
15341527
if engine is None:
15351528
# Only determine ext if it is needed
1536-
ext: str | None
1537-
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
1538-
ext = "xls"
1539-
else:
1529+
ext: str | None = None
1530+
1531+
if not isinstance(
1532+
path_or_buffer, (str, os.PathLike, ExcelFile)
1533+
) and not is_file_like(path_or_buffer):
1534+
# GH#56692 - avoid importing xlrd if possible
1535+
if import_optional_dependency("xlrd", errors="ignore") is None:
1536+
xlrd_version = None
1537+
else:
1538+
import xlrd
1539+
1540+
xlrd_version = Version(get_version(xlrd))
1541+
1542+
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
1543+
ext = "xls"
1544+
1545+
if ext is None:
15401546
ext = inspect_excel_format(
15411547
content_or_path=path_or_buffer, storage_options=storage_options
15421548
)

Diff for: pandas/tests/groupby/test_reductions.py

-10
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ def test_basic_aggregations(dtype):
6565
with pytest.raises(pd.errors.SpecificationError, match=msg):
6666
grouped.aggregate({"one": np.mean, "two": np.std})
6767

68-
group_constants = {0: 10, 1: 20, 2: 30}
69-
msg = (
70-
"Pinning the groupby key to each group in SeriesGroupBy.agg is deprecated, "
71-
"and cases that relied on it will raise in a future version"
72-
)
73-
with tm.assert_produces_warning(FutureWarning, match=msg):
74-
# GH#41090
75-
agged = grouped.agg(lambda x: group_constants[x.name] + x.mean())
76-
assert agged[1] == 21
77-
7868
# corner cases
7969
msg = "Must produce aggregated value"
8070
# exception raised is type Exception

0 commit comments

Comments
 (0)