Skip to content

Commit 893ffa9

Browse files
committed
Merge branch 'main' into 37715-remove-mypy-ignore-V
2 parents c409d62 + 9b20759 commit 893ffa9

38 files changed

+837
-276
lines changed

doc/source/user_guide/cookbook.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
459459
df
460460
461461
# List the size of the animals with the highest weight.
462-
df.groupby("animal").apply(lambda subf: subf["size"][subf["weight"].idxmax()])
462+
df.groupby("animal")[["size", "weight"]].apply(lambda subf: subf["size"][subf["weight"].idxmax()])
463463
464464
`Using get_group
465465
<https://stackoverflow.com/questions/14734533/how-to-access-pandas-groupby-dataframe-by-key>`__
@@ -482,7 +482,7 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
482482
return pd.Series(["L", avg_weight, True], index=["size", "weight", "adult"])
483483
484484
485-
expected_df = gb.apply(GrowUp)
485+
expected_df = gb[["size", "weight"]].apply(GrowUp)
486486
expected_df
487487
488488
`Expanding apply

doc/source/user_guide/groupby.rst

+10-4
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ This is mainly syntactic sugar for the alternative, which is much more verbose:
430430
Additionally, this method avoids recomputing the internal grouping information
431431
derived from the passed key.
432432

433+
You can also include the grouping columns if you want to operate on them.
434+
435+
.. ipython:: python
436+
437+
grouped[["A", "B"]].sum()
438+
433439
.. _groupby.iterating-label:
434440

435441
Iterating through groups
@@ -1067,7 +1073,7 @@ missing values with the ``ffill()`` method.
10671073
).set_index("date")
10681074
df_re
10691075
1070-
df_re.groupby("group").resample("1D").ffill()
1076+
df_re.groupby("group")[["val"]].resample("1D").ffill()
10711077
10721078
.. _groupby.filter:
10731079

@@ -1233,13 +1239,13 @@ the argument ``group_keys`` which defaults to ``True``. Compare
12331239

12341240
.. ipython:: python
12351241
1236-
df.groupby("A", group_keys=True).apply(lambda x: x)
1242+
df.groupby("A", group_keys=True)[["B", "C", "D"]].apply(lambda x: x)
12371243
12381244
with
12391245

12401246
.. ipython:: python
12411247
1242-
df.groupby("A", group_keys=False).apply(lambda x: x)
1248+
df.groupby("A", group_keys=False)[["B", "C", "D"]].apply(lambda x: x)
12431249
12441250
12451251
Numba Accelerated Routines
@@ -1722,7 +1728,7 @@ column index name will be used as the name of the inserted column:
17221728
result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()}
17231729
return pd.Series(result, name="metrics")
17241730
1725-
result = df.groupby("a").apply(compute_metrics)
1731+
result = df.groupby("a")[["b", "c"]].apply(compute_metrics)
17261732
17271733
result
17281734

doc/source/user_guide/io.rst

+12
Original file line numberDiff line numberDiff line change
@@ -3449,6 +3449,18 @@ Reading Excel files
34493449
In the most basic use-case, ``read_excel`` takes a path to an Excel
34503450
file, and the ``sheet_name`` indicating which sheet to parse.
34513451

3452+
When using the ``engine_kwargs`` parameter, pandas will pass these arguments to the
3453+
engine. For this, it is important to know which function pandas is
3454+
using internally.
3455+
3456+
* For the engine openpyxl, pandas is using :func:`openpyxl.load_workbook` to read in (``.xlsx``) and (``.xlsm``) files.
3457+
3458+
* For the engine xlrd, pandas is using :func:`xlrd.open_workbook` to read in (``.xls``) files.
3459+
3460+
* For the engine pyxlsb, pandas is using :func:`pyxlsb.open_workbook` to read in (``.xlsb``) files.
3461+
3462+
* For the engine odf, pandas is using :func:`odf.opendocument.load` to read in (``.ods``) files.
3463+
34523464
.. code-block:: python
34533465
34543466
# Returns a DataFrame

doc/source/whatsnew/v0.14.0.rst

+17-5
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,25 @@ More consistent behavior for some groupby methods:
328328

329329
- groupby ``head`` and ``tail`` now act more like ``filter`` rather than an aggregation:
330330

331-
.. ipython:: python
331+
.. code-block:: ipython
332332
333-
df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=['A', 'B'])
334-
g = df.groupby('A')
335-
g.head(1) # filters DataFrame
333+
In [1]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=['A', 'B'])
334+
335+
In [2]: g = df.groupby('A')
336+
337+
In [3]: g.head(1) # filters DataFrame
338+
Out[3]:
339+
A B
340+
0 1 2
341+
2 5 6
342+
343+
In [4]: g.apply(lambda x: x.head(1)) # used to simply fall-through
344+
Out[4]:
345+
A B
346+
A
347+
1 0 1 2
348+
5 2 5 6
336349
337-
g.apply(lambda x: x.head(1)) # used to simply fall-through
338350
339351
- groupby head and tail respect column selection:
340352

doc/source/whatsnew/v0.18.1.rst

+87-6
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,52 @@ Previously you would have to do this to get a rolling window mean per-group:
7777
df = pd.DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})
7878
df
7979
80-
.. ipython:: python
80+
.. code-block:: ipython
8181
82-
df.groupby("A").apply(lambda x: x.rolling(4).B.mean())
82+
In [1]: df.groupby("A").apply(lambda x: x.rolling(4).B.mean())
83+
Out[1]:
84+
A
85+
1 0 NaN
86+
1 NaN
87+
2 NaN
88+
3 1.5
89+
4 2.5
90+
5 3.5
91+
6 4.5
92+
7 5.5
93+
8 6.5
94+
9 7.5
95+
10 8.5
96+
11 9.5
97+
12 10.5
98+
13 11.5
99+
14 12.5
100+
15 13.5
101+
16 14.5
102+
17 15.5
103+
18 16.5
104+
19 17.5
105+
2 20 NaN
106+
21 NaN
107+
22 NaN
108+
23 21.5
109+
24 22.5
110+
25 23.5
111+
26 24.5
112+
27 25.5
113+
28 26.5
114+
29 27.5
115+
30 28.5
116+
31 29.5
117+
3 32 NaN
118+
33 NaN
119+
34 NaN
120+
35 33.5
121+
36 34.5
122+
37 35.5
123+
38 36.5
124+
39 37.5
125+
Name: B, dtype: float64
83126
84127
Now you can do:
85128

@@ -101,15 +144,53 @@ For ``.resample(..)`` type of operations, previously you would have to:
101144
102145
df
103146
104-
.. ipython:: python
147+
.. code-block:: ipython
105148
106-
df.groupby("group").apply(lambda x: x.resample("1D").ffill())
149+
In[1]: df.groupby("group").apply(lambda x: x.resample("1D").ffill())
150+
Out[1]:
151+
group val
152+
group date
153+
1 2016-01-03 1 5
154+
2016-01-04 1 5
155+
2016-01-05 1 5
156+
2016-01-06 1 5
157+
2016-01-07 1 5
158+
2016-01-08 1 5
159+
2016-01-09 1 5
160+
2016-01-10 1 6
161+
2 2016-01-17 2 7
162+
2016-01-18 2 7
163+
2016-01-19 2 7
164+
2016-01-20 2 7
165+
2016-01-21 2 7
166+
2016-01-22 2 7
167+
2016-01-23 2 7
168+
2016-01-24 2 8
107169
108170
Now you can do:
109171

110-
.. ipython:: python
172+
.. code-block:: ipython
111173
112-
df.groupby("group").resample("1D").ffill()
174+
In[1]: df.groupby("group").resample("1D").ffill()
175+
Out[1]:
176+
group val
177+
group date
178+
1 2016-01-03 1 5
179+
2016-01-04 1 5
180+
2016-01-05 1 5
181+
2016-01-06 1 5
182+
2016-01-07 1 5
183+
2016-01-08 1 5
184+
2016-01-09 1 5
185+
2016-01-10 1 6
186+
2 2016-01-17 2 7
187+
2016-01-18 2 7
188+
2016-01-19 2 7
189+
2016-01-20 2 7
190+
2016-01-21 2 7
191+
2016-01-22 2 7
192+
2016-01-23 2 7
193+
2016-01-24 2 8
113194
114195
.. _whatsnew_0181.enhancements.method_chain:
115196

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Other enhancements
8787
- :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`)
8888
- :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`).
8989
- Add dtype of categories to ``repr`` information of :class:`CategoricalDtype` (:issue:`52179`)
90+
- Adding ``engine_kwargs`` parameter to :meth:`DataFrame.read_excel` (:issue:`52214`)
9091
-
9192

9293
.. ---------------------------------------------------------------------------
@@ -199,6 +200,7 @@ Other API changes
199200

200201
Deprecations
201202
~~~~~~~~~~~~
203+
- Deprecated :meth:`.DataFrameGroupBy.apply` and methods on the objects returned by :meth:`.DataFrameGroupBy.resample` operating on the grouping column(s); select the columns to operate on after groupby to either explicitly include or exclude the groupings and avoid the ``FutureWarning`` (:issue:`7155`)
202204
- Deprecated silently dropping unrecognized timezones when parsing strings to datetimes (:issue:`18702`)
203205
- Deprecated :meth:`DataFrame._data` and :meth:`Series._data`, use public APIs instead (:issue:`33333`)
204206
- Deprecated :meth:`.Groupby.all` and :meth:`.GroupBy.any` with datetime64 or :class:`PeriodDtype` values, matching the :class:`Series` and :class:`DataFrame` deprecations (:issue:`34479`)

pandas/core/frame.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -8595,20 +8595,20 @@ def update(
85958595
>>> df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
85968596
... 'Parrot', 'Parrot'],
85978597
... 'Max Speed': [380., 370., 24., 26.]})
8598-
>>> df.groupby("Animal", group_keys=True).apply(lambda x: x)
8599-
Animal Max Speed
8598+
>>> df.groupby("Animal", group_keys=True)[['Max Speed']].apply(lambda x: x)
8599+
Max Speed
86008600
Animal
8601-
Falcon 0 Falcon 380.0
8602-
1 Falcon 370.0
8603-
Parrot 2 Parrot 24.0
8604-
3 Parrot 26.0
8605-
8606-
>>> df.groupby("Animal", group_keys=False).apply(lambda x: x)
8607-
Animal Max Speed
8608-
0 Falcon 380.0
8609-
1 Falcon 370.0
8610-
2 Parrot 24.0
8611-
3 Parrot 26.0
8601+
Falcon 0 380.0
8602+
1 370.0
8603+
Parrot 2 24.0
8604+
3 26.0
8605+
8606+
>>> df.groupby("Animal", group_keys=False)[['Max Speed']].apply(lambda x: x)
8607+
Max Speed
8608+
0 380.0
8609+
1 370.0
8610+
2 24.0
8611+
3 26.0
86128612
"""
86138613
)
86148614
)

0 commit comments

Comments
 (0)