Skip to content

Commit 9a4ccc1

Browse files
committed
Merge branch 'master' into cln-getitem_block
2 parents e9d0a92 + 0a2a200 commit 9a4ccc1

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

pandas/core/frame.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6464,6 +6464,57 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
64646464
Returns
64656465
-------
64666466
DataFrame
6467+
6468+
Examples
6469+
--------
6470+
>>> df = pd.DataFrame(
6471+
... {"Grade": ["A", "B", "A", "C"]},
6472+
... index=[
6473+
... ["Final exam", "Final exam", "Coursework", "Coursework"],
6474+
... ["History", "Geography", "History", "Geography"],
6475+
... ["January", "February", "March", "April"],
6476+
... ],
6477+
... )
6478+
>>> df
6479+
Grade
6480+
Final exam History January A
6481+
Geography February B
6482+
Coursework History March A
6483+
Geography April C
6484+
6485+
In the following example, we will swap the levels of the indices.
6486+
Here, we will swap the levels column-wise, but levels can be swapped row-wise
6487+
in a similar manner. Note that column-wise is the default behaviour.
6488+
By not supplying any arguments for i and j, we swap the last and second to
6489+
last indices.
6490+
6491+
>>> df.swaplevel()
6492+
Grade
6493+
Final exam January History A
6494+
February Geography B
6495+
Coursework March History A
6496+
April Geography C
6497+
6498+
By supplying one argument, we can choose which index to swap the last
6499+
index with. We can for example swap the first index with the last one as
6500+
follows.
6501+
6502+
>>> df.swaplevel(0)
6503+
Grade
6504+
January History Final exam A
6505+
February Geography Final exam B
6506+
March History Coursework A
6507+
April Geography Coursework C
6508+
6509+
We can also define explicitly which indices we want to swap by supplying values
6510+
for both i and j. Here, we for example swap the first and second indices.
6511+
6512+
>>> df.swaplevel(0, 1)
6513+
Grade
6514+
History Final exam January A
6515+
Geography Final exam February B
6516+
History Coursework March A
6517+
Geography Coursework April C
64676518
"""
64686519
result = self.copy()
64696520

pandas/core/generic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11014,7 +11014,9 @@ def ewm(
1101411014
times: Optional[Union[str, np.ndarray, FrameOrSeries]] = None,
1101511015
) -> ExponentialMovingWindow:
1101611016
axis = self._get_axis_number(axis)
11017-
return ExponentialMovingWindow(
11017+
# error: Value of type variable "FrameOrSeries" of "ExponentialMovingWindow"
11018+
# cannot be "object"
11019+
return ExponentialMovingWindow( # type: ignore[type-var]
1101811020
self,
1101911021
com=com,
1102011022
span=span,

pandas/core/internals/array_manager.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,12 +786,10 @@ def get_slice(self, slobj: slice, axis: int = 0) -> ArrayManager:
786786
arrays = self.arrays[slobj]
787787

788788
new_axes = list(self._axes)
789-
new_axes[axis] = new_axes[axis][slobj]
789+
new_axes[axis] = new_axes[axis]._getitem_slice(slobj)
790790

791791
return type(self)(arrays, new_axes, verify_integrity=False)
792792

793-
getitem_mgr = get_slice
794-
795793
def fast_xs(self, loc: int) -> ArrayLike:
796794
"""
797795
Return the array corresponding to `frame.iloc[loc]`.
@@ -1218,7 +1216,12 @@ def get_slice(self, slobj: slice, axis: int = 0) -> SingleArrayManager:
12181216
raise IndexError("Requested axis not found in manager")
12191217

12201218
new_array = self.array[slobj]
1221-
new_index = self.index[slobj]
1219+
new_index = self.index._getitem_slice(slobj)
1220+
return type(self)([new_array], [new_index], verify_integrity=False)
1221+
1222+
def getitem_mgr(self, indexer) -> SingleArrayManager:
1223+
new_array = self.array[indexer]
1224+
new_index = self.index[indexer]
12221225
return type(self)([new_array], [new_index])
12231226

12241227
def apply(self, func, **kwargs):

pandas/core/window/expanding.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
Dict,
66
Optional,
77
Tuple,
8-
Union,
98
)
109

11-
import numpy as np
12-
1310
from pandas._typing import (
1411
Axis,
1512
FrameOrSeries,
@@ -589,7 +586,7 @@ def quantile(
589586
)
590587
def cov(
591588
self,
592-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
589+
other: Optional[FrameOrSeriesUnion] = None,
593590
pairwise: Optional[bool] = None,
594591
ddof: int = 1,
595592
**kwargs,
@@ -654,7 +651,7 @@ def cov(
654651
)
655652
def corr(
656653
self,
657-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
654+
other: Optional[FrameOrSeriesUnion] = None,
658655
pairwise: Optional[bool] = None,
659656
ddof: int = 1,
660657
**kwargs,

pandas/core/window/rolling.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ def quantile(self, quantile: float, interpolation: str = "linear", **kwargs):
13541354

13551355
def cov(
13561356
self,
1357-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
1357+
other: Optional[FrameOrSeriesUnion] = None,
13581358
pairwise: Optional[bool] = None,
13591359
ddof: int = 1,
13601360
**kwargs,
@@ -1392,7 +1392,7 @@ def cov_func(x, y):
13921392

13931393
def corr(
13941394
self,
1395-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
1395+
other: Optional[FrameOrSeriesUnion] = None,
13961396
pairwise: Optional[bool] = None,
13971397
ddof: int = 1,
13981398
**kwargs,
@@ -2137,7 +2137,7 @@ def quantile(self, quantile: float, interpolation: str = "linear", **kwargs):
21372137
)
21382138
def cov(
21392139
self,
2140-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
2140+
other: Optional[FrameOrSeriesUnion] = None,
21412141
pairwise: Optional[bool] = None,
21422142
ddof: int = 1,
21432143
**kwargs,
@@ -2262,7 +2262,7 @@ def cov(
22622262
)
22632263
def corr(
22642264
self,
2265-
other: Optional[Union[np.ndarray, FrameOrSeriesUnion]] = None,
2265+
other: Optional[FrameOrSeriesUnion] = None,
22662266
pairwise: Optional[bool] = None,
22672267
ddof: int = 1,
22682268
**kwargs,

0 commit comments

Comments
 (0)