Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/core/window/expanding.py: 57%

96 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import annotations 

2 

3from textwrap import dedent 

4from typing import ( 

5 TYPE_CHECKING, 

6 Any, 

7 Callable, 

8) 

9 

10from pandas._typing import ( 

11 Axis, 

12 QuantileInterpolation, 

13 WindowingRankType, 

14) 

15 

16if TYPE_CHECKING: 16 ↛ 17line 16 didn't jump to line 17, because the condition on line 16 was never true

17 from pandas import DataFrame, Series 

18 from pandas.core.generic import NDFrame 

19 

20from pandas.compat.numpy import function as nv 

21from pandas.util._decorators import doc 

22 

23from pandas.core.indexers.objects import ( 

24 BaseIndexer, 

25 ExpandingIndexer, 

26 GroupbyIndexer, 

27) 

28from pandas.core.window.common import maybe_warn_args_and_kwargs 

29from pandas.core.window.doc import ( 

30 _shared_docs, 

31 args_compat, 

32 create_section_header, 

33 kwargs_compat, 

34 kwargs_numeric_only, 

35 numba_notes, 

36 template_header, 

37 template_returns, 

38 template_see_also, 

39 window_agg_numba_parameters, 

40 window_apply_parameters, 

41) 

42from pandas.core.window.rolling import ( 

43 BaseWindowGroupby, 

44 RollingAndExpandingMixin, 

45) 

46 

47 

48class Expanding(RollingAndExpandingMixin): 

49 """ 

50 Provide expanding window calculations. 

51 

52 Parameters 

53 ---------- 

54 min_periods : int, default 1 

55 Minimum number of observations in window required to have a value; 

56 otherwise, result is ``np.nan``. 

57 

58 center : bool, default False 

59 If False, set the window labels as the right edge of the window index. 

60 

61 If True, set the window labels as the center of the window index. 

62 

63 .. deprecated:: 1.1.0 

64 

65 axis : int or str, default 0 

66 If ``0`` or ``'index'``, roll across the rows. 

67 

68 If ``1`` or ``'columns'``, roll across the columns. 

69 

70 For `Series` this parameter is unused and defaults to 0. 

71 

72 method : str {'single', 'table'}, default 'single' 

73 Execute the rolling operation per single column or row (``'single'``) 

74 or over the entire object (``'table'``). 

75 

76 This argument is only implemented when specifying ``engine='numba'`` 

77 in the method call. 

78 

79 .. versionadded:: 1.3.0 

80 

81 Returns 

82 ------- 

83 ``Expanding`` subclass 

84 

85 See Also 

86 -------- 

87 rolling : Provides rolling window calculations. 

88 ewm : Provides exponential weighted functions. 

89 

90 Notes 

91 ----- 

92 See :ref:`Windowing Operations <window.expanding>` for further usage details 

93 and examples. 

94 

95 Examples 

96 -------- 

97 >>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) 

98 >>> df 

99 B 

100 0 0.0 

101 1 1.0 

102 2 2.0 

103 3 NaN 

104 4 4.0 

105 

106 **min_periods** 

107 

108 Expanding sum with 1 vs 3 observations needed to calculate a value. 

109 

110 >>> df.expanding(1).sum() 

111 B 

112 0 0.0 

113 1 1.0 

114 2 3.0 

115 3 3.0 

116 4 7.0 

117 >>> df.expanding(3).sum() 

118 B 

119 0 NaN 

120 1 NaN 

121 2 3.0 

122 3 3.0 

123 4 7.0 

124 """ 

125 

126 _attributes: list[str] = ["min_periods", "center", "axis", "method"] 

127 

128 def __init__( 

129 self, 

130 obj: NDFrame, 

131 min_periods: int = 1, 

132 center: bool | None = None, 

133 axis: Axis = 0, 

134 method: str = "single", 

135 selection=None, 

136 ) -> None: 

137 super().__init__( 

138 obj=obj, 

139 min_periods=min_periods, 

140 center=center, 

141 axis=axis, 

142 method=method, 

143 selection=selection, 

144 ) 

145 

146 def _get_window_indexer(self) -> BaseIndexer: 

147 """ 

148 Return an indexer class that will compute the window start and end bounds 

149 """ 

150 return ExpandingIndexer() 

151 

152 @doc( 

153 _shared_docs["aggregate"], 

154 see_also=dedent( 

155 """ 

156 See Also 

157 -------- 

158 pandas.DataFrame.aggregate : Similar DataFrame method. 

159 pandas.Series.aggregate : Similar Series method. 

160 """ 

161 ), 

162 examples=dedent( 

163 """ 

164 Examples 

165 -------- 

166 >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) 

167 >>> df 

168 A B C 

169 0 1 4 7 

170 1 2 5 8 

171 2 3 6 9 

172 

173 >>> df.ewm(alpha=0.5).mean() 

174 A B C 

175 0 1.000000 4.000000 7.000000 

176 1 1.666667 4.666667 7.666667 

177 2 2.428571 5.428571 8.428571 

178 """ 

179 ), 

180 klass="Series/Dataframe", 

181 axis="", 

182 ) 

183 def aggregate(self, func, *args, **kwargs): 

184 return super().aggregate(func, *args, **kwargs) 

185 

186 agg = aggregate 

187 

188 @doc( 

189 template_header, 

190 create_section_header("Returns"), 

191 template_returns, 

192 create_section_header("See Also"), 

193 template_see_also[:-1], 

194 window_method="expanding", 

195 aggregation_description="count of non NaN observations", 

196 agg_method="count", 

197 ) 

198 def count(self, numeric_only: bool = False): 

199 return super().count(numeric_only=numeric_only) 

200 

201 @doc( 

202 template_header, 

203 create_section_header("Parameters"), 

204 window_apply_parameters, 

205 create_section_header("Returns"), 

206 template_returns, 

207 create_section_header("See Also"), 

208 template_see_also[:-1], 

209 window_method="expanding", 

210 aggregation_description="custom aggregation function", 

211 agg_method="apply", 

212 ) 

213 def apply( 

214 self, 

215 func: Callable[..., Any], 

216 raw: bool = False, 

217 engine: str | None = None, 

218 engine_kwargs: dict[str, bool] | None = None, 

219 args: tuple[Any, ...] | None = None, 

220 kwargs: dict[str, Any] | None = None, 

221 ): 

222 return super().apply( 

223 func, 

224 raw=raw, 

225 engine=engine, 

226 engine_kwargs=engine_kwargs, 

227 args=args, 

228 kwargs=kwargs, 

229 ) 

230 

231 @doc( 

232 template_header, 

233 create_section_header("Parameters"), 

234 kwargs_numeric_only, 

235 args_compat, 

236 window_agg_numba_parameters(), 

237 kwargs_compat, 

238 create_section_header("Returns"), 

239 template_returns, 

240 create_section_header("See Also"), 

241 template_see_also, 

242 create_section_header("Notes"), 

243 numba_notes[:-1], 

244 window_method="expanding", 

245 aggregation_description="sum", 

246 agg_method="sum", 

247 ) 

248 def sum( 

249 self, 

250 numeric_only: bool = False, 

251 *args, 

252 engine: str | None = None, 

253 engine_kwargs: dict[str, bool] | None = None, 

254 **kwargs, 

255 ): 

256 maybe_warn_args_and_kwargs(type(self), "sum", args, kwargs) 

257 nv.validate_expanding_func("sum", args, kwargs) 

258 return super().sum( 

259 numeric_only=numeric_only, 

260 engine=engine, 

261 engine_kwargs=engine_kwargs, 

262 **kwargs, 

263 ) 

264 

265 @doc( 

266 template_header, 

267 create_section_header("Parameters"), 

268 kwargs_numeric_only, 

269 args_compat, 

270 window_agg_numba_parameters(), 

271 kwargs_compat, 

272 create_section_header("Returns"), 

273 template_returns, 

274 create_section_header("See Also"), 

275 template_see_also, 

276 create_section_header("Notes"), 

277 numba_notes[:-1], 

278 window_method="expanding", 

279 aggregation_description="maximum", 

280 agg_method="max", 

281 ) 

282 def max( 

283 self, 

284 numeric_only: bool = False, 

285 *args, 

286 engine: str | None = None, 

287 engine_kwargs: dict[str, bool] | None = None, 

288 **kwargs, 

289 ): 

290 maybe_warn_args_and_kwargs(type(self), "max", args, kwargs) 

291 nv.validate_expanding_func("max", args, kwargs) 

292 return super().max( 

293 numeric_only=numeric_only, 

294 engine=engine, 

295 engine_kwargs=engine_kwargs, 

296 **kwargs, 

297 ) 

298 

299 @doc( 

300 template_header, 

301 create_section_header("Parameters"), 

302 kwargs_numeric_only, 

303 args_compat, 

304 window_agg_numba_parameters(), 

305 kwargs_compat, 

306 create_section_header("Returns"), 

307 template_returns, 

308 create_section_header("See Also"), 

309 template_see_also, 

310 create_section_header("Notes"), 

311 numba_notes[:-1], 

312 window_method="expanding", 

313 aggregation_description="minimum", 

314 agg_method="min", 

315 ) 

316 def min( 

317 self, 

318 numeric_only: bool = False, 

319 *args, 

320 engine: str | None = None, 

321 engine_kwargs: dict[str, bool] | None = None, 

322 **kwargs, 

323 ): 

324 maybe_warn_args_and_kwargs(type(self), "min", args, kwargs) 

325 nv.validate_expanding_func("min", args, kwargs) 

326 return super().min( 

327 numeric_only=numeric_only, 

328 engine=engine, 

329 engine_kwargs=engine_kwargs, 

330 **kwargs, 

331 ) 

332 

333 @doc( 

334 template_header, 

335 create_section_header("Parameters"), 

336 kwargs_numeric_only, 

337 args_compat, 

338 window_agg_numba_parameters(), 

339 kwargs_compat, 

340 create_section_header("Returns"), 

341 template_returns, 

342 create_section_header("See Also"), 

343 template_see_also, 

344 create_section_header("Notes"), 

345 numba_notes[:-1], 

346 window_method="expanding", 

347 aggregation_description="mean", 

348 agg_method="mean", 

349 ) 

350 def mean( 

351 self, 

352 numeric_only: bool = False, 

353 *args, 

354 engine: str | None = None, 

355 engine_kwargs: dict[str, bool] | None = None, 

356 **kwargs, 

357 ): 

358 maybe_warn_args_and_kwargs(type(self), "mean", args, kwargs) 

359 nv.validate_expanding_func("mean", args, kwargs) 

360 return super().mean( 

361 numeric_only=numeric_only, 

362 engine=engine, 

363 engine_kwargs=engine_kwargs, 

364 **kwargs, 

365 ) 

366 

367 @doc( 

368 template_header, 

369 create_section_header("Parameters"), 

370 kwargs_numeric_only, 

371 window_agg_numba_parameters(), 

372 kwargs_compat, 

373 create_section_header("Returns"), 

374 template_returns, 

375 create_section_header("See Also"), 

376 template_see_also, 

377 create_section_header("Notes"), 

378 numba_notes[:-1], 

379 window_method="expanding", 

380 aggregation_description="median", 

381 agg_method="median", 

382 ) 

383 def median( 

384 self, 

385 numeric_only: bool = False, 

386 engine: str | None = None, 

387 engine_kwargs: dict[str, bool] | None = None, 

388 **kwargs, 

389 ): 

390 maybe_warn_args_and_kwargs(type(self), "median", None, kwargs) 

391 return super().median( 

392 numeric_only=numeric_only, 

393 engine=engine, 

394 engine_kwargs=engine_kwargs, 

395 **kwargs, 

396 ) 

397 

398 @doc( 

399 template_header, 

400 create_section_header("Parameters"), 

401 dedent( 

402 """ 

403 ddof : int, default 1 

404 Delta Degrees of Freedom. The divisor used in calculations 

405 is ``N - ddof``, where ``N`` represents the number of elements.\n 

406 """ 

407 ).replace("\n", "", 1), 

408 kwargs_numeric_only, 

409 args_compat, 

410 window_agg_numba_parameters("1.4"), 

411 kwargs_compat, 

412 create_section_header("Returns"), 

413 template_returns, 

414 create_section_header("See Also"), 

415 "numpy.std : Equivalent method for NumPy array.\n", 

416 template_see_also, 

417 create_section_header("Notes"), 

418 dedent( 

419 """ 

420 The default ``ddof`` of 1 used in :meth:`Series.std` is different 

421 than the default ``ddof`` of 0 in :func:`numpy.std`. 

422 

423 A minimum of one period is required for the rolling calculation.\n 

424 """ 

425 ).replace("\n", "", 1), 

426 create_section_header("Examples"), 

427 dedent( 

428 """ 

429 >>> s = pd.Series([5, 5, 6, 7, 5, 5, 5]) 

430 

431 >>> s.expanding(3).std() 

432 0 NaN 

433 1 NaN 

434 2 0.577350 

435 3 0.957427 

436 4 0.894427 

437 5 0.836660 

438 6 0.786796 

439 dtype: float64 

440 """ 

441 ).replace("\n", "", 1), 

442 window_method="expanding", 

443 aggregation_description="standard deviation", 

444 agg_method="std", 

445 ) 

446 def std( 

447 self, 

448 ddof: int = 1, 

449 numeric_only: bool = False, 

450 *args, 

451 engine: str | None = None, 

452 engine_kwargs: dict[str, bool] | None = None, 

453 **kwargs, 

454 ): 

455 maybe_warn_args_and_kwargs(type(self), "std", args, kwargs) 

456 nv.validate_expanding_func("std", args, kwargs) 

457 return super().std( 

458 ddof=ddof, 

459 numeric_only=numeric_only, 

460 engine=engine, 

461 engine_kwargs=engine_kwargs, 

462 **kwargs, 

463 ) 

464 

465 @doc( 

466 template_header, 

467 create_section_header("Parameters"), 

468 dedent( 

469 """ 

470 ddof : int, default 1 

471 Delta Degrees of Freedom. The divisor used in calculations 

472 is ``N - ddof``, where ``N`` represents the number of elements.\n 

473 """ 

474 ).replace("\n", "", 1), 

475 kwargs_numeric_only, 

476 args_compat, 

477 window_agg_numba_parameters("1.4"), 

478 kwargs_compat, 

479 create_section_header("Returns"), 

480 template_returns, 

481 create_section_header("See Also"), 

482 "numpy.var : Equivalent method for NumPy array.\n", 

483 template_see_also, 

484 create_section_header("Notes"), 

485 dedent( 

486 """ 

487 The default ``ddof`` of 1 used in :meth:`Series.var` is different 

488 than the default ``ddof`` of 0 in :func:`numpy.var`. 

489 

490 A minimum of one period is required for the rolling calculation.\n 

491 """ 

492 ).replace("\n", "", 1), 

493 create_section_header("Examples"), 

494 dedent( 

495 """ 

496 >>> s = pd.Series([5, 5, 6, 7, 5, 5, 5]) 

497 

498 >>> s.expanding(3).var() 

499 0 NaN 

500 1 NaN 

501 2 0.333333 

502 3 0.916667 

503 4 0.800000 

504 5 0.700000 

505 6 0.619048 

506 dtype: float64 

507 """ 

508 ).replace("\n", "", 1), 

509 window_method="expanding", 

510 aggregation_description="variance", 

511 agg_method="var", 

512 ) 

513 def var( 

514 self, 

515 ddof: int = 1, 

516 numeric_only: bool = False, 

517 *args, 

518 engine: str | None = None, 

519 engine_kwargs: dict[str, bool] | None = None, 

520 **kwargs, 

521 ): 

522 maybe_warn_args_and_kwargs(type(self), "var", args, kwargs) 

523 nv.validate_expanding_func("var", args, kwargs) 

524 return super().var( 

525 ddof=ddof, 

526 numeric_only=numeric_only, 

527 engine=engine, 

528 engine_kwargs=engine_kwargs, 

529 **kwargs, 

530 ) 

531 

532 @doc( 

533 template_header, 

534 create_section_header("Parameters"), 

535 dedent( 

536 """ 

537 ddof : int, default 1 

538 Delta Degrees of Freedom. The divisor used in calculations 

539 is ``N - ddof``, where ``N`` represents the number of elements.\n 

540 """ 

541 ).replace("\n", "", 1), 

542 kwargs_numeric_only, 

543 args_compat, 

544 kwargs_compat, 

545 create_section_header("Returns"), 

546 template_returns, 

547 create_section_header("See Also"), 

548 template_see_also, 

549 create_section_header("Notes"), 

550 "A minimum of one period is required for the calculation.\n\n", 

551 create_section_header("Examples"), 

552 dedent( 

553 """ 

554 >>> s = pd.Series([0, 1, 2, 3]) 

555 

556 >>> s.expanding().sem() 

557 0 NaN 

558 1 0.707107 

559 2 0.707107 

560 3 0.745356 

561 dtype: float64 

562 """ 

563 ).replace("\n", "", 1), 

564 window_method="expanding", 

565 aggregation_description="standard error of mean", 

566 agg_method="sem", 

567 ) 

568 def sem(self, ddof: int = 1, numeric_only: bool = False, *args, **kwargs): 

569 maybe_warn_args_and_kwargs(type(self), "sem", args, kwargs) 

570 return super().sem(ddof=ddof, numeric_only=numeric_only, **kwargs) 

571 

572 @doc( 

573 template_header, 

574 create_section_header("Parameters"), 

575 kwargs_numeric_only, 

576 kwargs_compat, 

577 create_section_header("Returns"), 

578 template_returns, 

579 create_section_header("See Also"), 

580 "scipy.stats.skew : Third moment of a probability density.\n", 

581 template_see_also, 

582 create_section_header("Notes"), 

583 "A minimum of three periods is required for the rolling calculation.\n", 

584 window_method="expanding", 

585 aggregation_description="unbiased skewness", 

586 agg_method="skew", 

587 ) 

588 def skew(self, numeric_only: bool = False, **kwargs): 

589 maybe_warn_args_and_kwargs(type(self), "skew", None, kwargs) 

590 return super().skew(numeric_only=numeric_only, **kwargs) 

591 

592 @doc( 

593 template_header, 

594 create_section_header("Parameters"), 

595 kwargs_numeric_only, 

596 kwargs_compat, 

597 create_section_header("Returns"), 

598 template_returns, 

599 create_section_header("See Also"), 

600 "scipy.stats.kurtosis : Reference SciPy method.\n", 

601 template_see_also, 

602 create_section_header("Notes"), 

603 "A minimum of four periods is required for the calculation.\n\n", 

604 create_section_header("Examples"), 

605 dedent( 

606 """ 

607 The example below will show a rolling calculation with a window size of 

608 four matching the equivalent function call using `scipy.stats`. 

609 

610 >>> arr = [1, 2, 3, 4, 999] 

611 >>> import scipy.stats 

612 >>> print(f"{{scipy.stats.kurtosis(arr[:-1], bias=False):.6f}}") 

613 -1.200000 

614 >>> print(f"{{scipy.stats.kurtosis(arr, bias=False):.6f}}") 

615 4.999874 

616 >>> s = pd.Series(arr) 

617 >>> s.expanding(4).kurt() 

618 0 NaN 

619 1 NaN 

620 2 NaN 

621 3 -1.200000 

622 4 4.999874 

623 dtype: float64 

624 """ 

625 ).replace("\n", "", 1), 

626 window_method="expanding", 

627 aggregation_description="Fisher's definition of kurtosis without bias", 

628 agg_method="kurt", 

629 ) 

630 def kurt(self, numeric_only: bool = False, **kwargs): 

631 maybe_warn_args_and_kwargs(type(self), "kurt", None, kwargs) 

632 return super().kurt(numeric_only=numeric_only, **kwargs) 

633 

634 @doc( 

635 template_header, 

636 create_section_header("Parameters"), 

637 dedent( 

638 """ 

639 quantile : float 

640 Quantile to compute. 0 <= quantile <= 1. 

641 interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}} 

642 This optional parameter specifies the interpolation method to use, 

643 when the desired quantile lies between two data points `i` and `j`: 

644 

645 * linear: `i + (j - i) * fraction`, where `fraction` is the 

646 fractional part of the index surrounded by `i` and `j`. 

647 * lower: `i`. 

648 * higher: `j`. 

649 * nearest: `i` or `j` whichever is nearest. 

650 * midpoint: (`i` + `j`) / 2. 

651 """ 

652 ).replace("\n", "", 1), 

653 kwargs_numeric_only, 

654 kwargs_compat, 

655 create_section_header("Returns"), 

656 template_returns, 

657 create_section_header("See Also"), 

658 template_see_also[:-1], 

659 window_method="expanding", 

660 aggregation_description="quantile", 

661 agg_method="quantile", 

662 ) 

663 def quantile( 

664 self, 

665 quantile: float, 

666 interpolation: QuantileInterpolation = "linear", 

667 numeric_only: bool = False, 

668 **kwargs, 

669 ): 

670 maybe_warn_args_and_kwargs(type(self), "quantile", None, kwargs) 

671 return super().quantile( 

672 quantile=quantile, 

673 interpolation=interpolation, 

674 numeric_only=numeric_only, 

675 **kwargs, 

676 ) 

677 

678 @doc( 

679 template_header, 

680 ".. versionadded:: 1.4.0 \n\n", 

681 create_section_header("Parameters"), 

682 dedent( 

683 """ 

684 method : {{'average', 'min', 'max'}}, default 'average' 

685 How to rank the group of records that have the same value (i.e. ties): 

686 

687 * average: average rank of the group 

688 * min: lowest rank in the group 

689 * max: highest rank in the group 

690 

691 ascending : bool, default True 

692 Whether or not the elements should be ranked in ascending order. 

693 pct : bool, default False 

694 Whether or not to display the returned rankings in percentile 

695 form. 

696 """ 

697 ).replace("\n", "", 1), 

698 kwargs_numeric_only, 

699 kwargs_compat, 

700 create_section_header("Returns"), 

701 template_returns, 

702 create_section_header("See Also"), 

703 template_see_also, 

704 create_section_header("Examples"), 

705 dedent( 

706 """ 

707 >>> s = pd.Series([1, 4, 2, 3, 5, 3]) 

708 >>> s.expanding().rank() 

709 0 1.0 

710 1 2.0 

711 2 2.0 

712 3 3.0 

713 4 5.0 

714 5 3.5 

715 dtype: float64 

716 

717 >>> s.expanding().rank(method="max") 

718 0 1.0 

719 1 2.0 

720 2 2.0 

721 3 3.0 

722 4 5.0 

723 5 4.0 

724 dtype: float64 

725 

726 >>> s.expanding().rank(method="min") 

727 0 1.0 

728 1 2.0 

729 2 2.0 

730 3 3.0 

731 4 5.0 

732 5 3.0 

733 dtype: float64 

734 """ 

735 ).replace("\n", "", 1), 

736 window_method="expanding", 

737 aggregation_description="rank", 

738 agg_method="rank", 

739 ) 

740 def rank( 

741 self, 

742 method: WindowingRankType = "average", 

743 ascending: bool = True, 

744 pct: bool = False, 

745 numeric_only: bool = False, 

746 **kwargs, 

747 ): 

748 maybe_warn_args_and_kwargs(type(self), "rank", None, kwargs) 

749 return super().rank( 

750 method=method, 

751 ascending=ascending, 

752 pct=pct, 

753 numeric_only=numeric_only, 

754 **kwargs, 

755 ) 

756 

757 @doc( 

758 template_header, 

759 create_section_header("Parameters"), 

760 dedent( 

761 """ 

762 other : Series or DataFrame, optional 

763 If not supplied then will default to self and produce pairwise 

764 output. 

765 pairwise : bool, default None 

766 If False then only matching columns between self and other will be 

767 used and the output will be a DataFrame. 

768 If True then all pairwise combinations will be calculated and the 

769 output will be a MultiIndexed DataFrame in the case of DataFrame 

770 inputs. In the case of missing elements, only complete pairwise 

771 observations will be used. 

772 ddof : int, default 1 

773 Delta Degrees of Freedom. The divisor used in calculations 

774 is ``N - ddof``, where ``N`` represents the number of elements. 

775 """ 

776 ).replace("\n", "", 1), 

777 kwargs_numeric_only, 

778 kwargs_compat, 

779 create_section_header("Returns"), 

780 template_returns, 

781 create_section_header("See Also"), 

782 template_see_also[:-1], 

783 window_method="expanding", 

784 aggregation_description="sample covariance", 

785 agg_method="cov", 

786 ) 

787 def cov( 

788 self, 

789 other: DataFrame | Series | None = None, 

790 pairwise: bool | None = None, 

791 ddof: int = 1, 

792 numeric_only: bool = False, 

793 **kwargs, 

794 ): 

795 maybe_warn_args_and_kwargs(type(self), "cov", None, kwargs) 

796 return super().cov( 

797 other=other, 

798 pairwise=pairwise, 

799 ddof=ddof, 

800 numeric_only=numeric_only, 

801 **kwargs, 

802 ) 

803 

804 @doc( 

805 template_header, 

806 create_section_header("Parameters"), 

807 dedent( 

808 """ 

809 other : Series or DataFrame, optional 

810 If not supplied then will default to self and produce pairwise 

811 output. 

812 pairwise : bool, default None 

813 If False then only matching columns between self and other will be 

814 used and the output will be a DataFrame. 

815 If True then all pairwise combinations will be calculated and the 

816 output will be a MultiIndexed DataFrame in the case of DataFrame 

817 inputs. In the case of missing elements, only complete pairwise 

818 observations will be used. 

819 """ 

820 ).replace("\n", "", 1), 

821 kwargs_numeric_only, 

822 kwargs_compat, 

823 create_section_header("Returns"), 

824 template_returns, 

825 create_section_header("See Also"), 

826 dedent( 

827 """ 

828 cov : Similar method to calculate covariance. 

829 numpy.corrcoef : NumPy Pearson's correlation calculation. 

830 """ 

831 ).replace("\n", "", 1), 

832 template_see_also, 

833 create_section_header("Notes"), 

834 dedent( 

835 """ 

836 This function uses Pearson's definition of correlation 

837 (https://en.wikipedia.org/wiki/Pearson_correlation_coefficient). 

838 

839 When `other` is not specified, the output will be self correlation (e.g. 

840 all 1's), except for :class:`~pandas.DataFrame` inputs with `pairwise` 

841 set to `True`. 

842 

843 Function will return ``NaN`` for correlations of equal valued sequences; 

844 this is the result of a 0/0 division error. 

845 

846 When `pairwise` is set to `False`, only matching columns between `self` and 

847 `other` will be used. 

848 

849 When `pairwise` is set to `True`, the output will be a MultiIndex DataFrame 

850 with the original index on the first level, and the `other` DataFrame 

851 columns on the second level. 

852 

853 In the case of missing elements, only complete pairwise observations 

854 will be used. 

855 """ 

856 ).replace("\n", "", 1), 

857 window_method="expanding", 

858 aggregation_description="correlation", 

859 agg_method="corr", 

860 ) 

861 def corr( 

862 self, 

863 other: DataFrame | Series | None = None, 

864 pairwise: bool | None = None, 

865 ddof: int = 1, 

866 numeric_only: bool = False, 

867 **kwargs, 

868 ): 

869 maybe_warn_args_and_kwargs(type(self), "corr", None, kwargs) 

870 return super().corr( 

871 other=other, 

872 pairwise=pairwise, 

873 ddof=ddof, 

874 numeric_only=numeric_only, 

875 **kwargs, 

876 ) 

877 

878 

879class ExpandingGroupby(BaseWindowGroupby, Expanding): 

880 """ 

881 Provide a expanding groupby implementation. 

882 """ 

883 

884 _attributes = Expanding._attributes + BaseWindowGroupby._attributes 

885 

886 def _get_window_indexer(self) -> GroupbyIndexer: 

887 """ 

888 Return an indexer class that will compute the window start and end bounds 

889 

890 Returns 

891 ------- 

892 GroupbyIndexer 

893 """ 

894 window_indexer = GroupbyIndexer( 

895 groupby_indices=self._grouper.indices, 

896 window_indexer=ExpandingIndexer, 

897 ) 

898 return window_indexer