1
1
from __future__ import annotations
2
2
3
3
import copy
4
- import warnings
5
4
from textwrap import dedent
6
5
from typing import (
7
6
TYPE_CHECKING ,
11
10
no_type_check ,
12
11
overload ,
13
12
)
13
+ import warnings
14
14
15
15
import numpy as np
16
16
17
- import pandas .core .algorithms as algos
18
17
from pandas ._libs import lib
19
18
from pandas ._libs .tslibs import (
20
19
BaseOffset ,
26
25
to_offset ,
27
26
)
28
27
from pandas ._typing import NDFrameT
29
- from pandas .core . apply import ResamplerWindowApply
30
- from pandas .core . arrays import ArrowExtensionArray
31
- from pandas . core . base import (
32
- PandasObject ,
33
- SelectionMixin ,
28
+ from pandas .errors import AbstractMethodError
29
+ from pandas .util . _decorators import (
30
+ Appender ,
31
+ Substitution ,
32
+ doc ,
34
33
)
34
+ from pandas .util ._exceptions import (
35
+ find_stack_level ,
36
+ rewrite_warning ,
37
+ )
38
+
35
39
from pandas .core .dtypes .dtypes import (
36
40
ArrowDtype ,
37
41
PeriodDtype ,
40
44
ABCDataFrame ,
41
45
ABCSeries ,
42
46
)
47
+
48
+ import pandas .core .algorithms as algos
49
+ from pandas .core .apply import ResamplerWindowApply
50
+ from pandas .core .arrays import ArrowExtensionArray
51
+ from pandas .core .base import (
52
+ PandasObject ,
53
+ SelectionMixin ,
54
+ )
43
55
from pandas .core .generic import (
44
56
NDFrame ,
45
57
_shared_docs ,
68
80
timedelta_range ,
69
81
)
70
82
from pandas .core .reshape .concat import concat
71
- from pandas . errors import AbstractMethodError
83
+
72
84
from pandas .tseries .frequencies import (
73
85
is_subperiod ,
74
86
is_superperiod ,
77
89
Day ,
78
90
Tick ,
79
91
)
80
- from pandas .util ._decorators import (
81
- Appender ,
82
- Substitution ,
83
- doc ,
84
- )
85
- from pandas .util ._exceptions import (
86
- find_stack_level ,
87
- rewrite_warning ,
88
- )
89
92
90
93
if TYPE_CHECKING :
91
94
from collections .abc import (
92
95
Callable ,
93
96
Hashable ,
94
97
)
95
98
96
- from pandas import (
97
- DataFrame ,
98
- Series ,
99
- )
100
99
from pandas ._typing import (
101
100
Any ,
102
101
AnyArrayLike ,
115
114
npt ,
116
115
)
117
116
117
+ from pandas import (
118
+ DataFrame ,
119
+ Series ,
120
+ )
121
+
118
122
_shared_docs_kwargs : dict [str , str ] = {}
119
123
120
124
@@ -356,8 +360,7 @@ def pipe(
356
360
axis = "" ,
357
361
)
358
362
def aggregate (self , func = None , * args , ** kwargs ):
359
- result = ResamplerWindowApply (
360
- self , func , args = args , kwargs = kwargs ).agg ()
363
+ result = ResamplerWindowApply (self , func , args = args , kwargs = kwargs ).agg ()
361
364
if result is None :
362
365
how = func
363
366
result = self ._groupby_and_aggregate (how , * args , ** kwargs )
@@ -444,14 +447,13 @@ def _groupby_and_aggregate(self, how, *args, **kwargs):
444
447
# Excludes `on` column when provided
445
448
obj = self ._obj_with_exclusions
446
449
447
- grouped = get_groupby (obj , by = None , grouper = grouper ,
448
- group_keys = self .group_keys )
450
+ grouped = get_groupby (obj , by = None , grouper = grouper , group_keys = self .group_keys )
449
451
450
452
try :
451
453
if callable (how ):
452
454
# TODO: test_resample_apply_with_additional_args fails if we go
453
455
# through the non-lambda path, not clear that it should.
454
- def func ( x ): return how (x , * args , ** kwargs )
456
+ func = lambda x : how (x , * args , ** kwargs )
455
457
result = grouped .aggregate (func )
456
458
else :
457
459
result = grouped .aggregate (how , * args , ** kwargs )
@@ -1647,16 +1649,14 @@ def _apply(self, f, *args, **kwargs):
1647
1649
"""
1648
1650
1649
1651
def func (x ):
1650
- x = self ._resampler_cls (
1651
- x , timegrouper = self ._timegrouper , gpr_index = self .ax )
1652
+ x = self ._resampler_cls (x , timegrouper = self ._timegrouper , gpr_index = self .ax )
1652
1653
1653
1654
if isinstance (f , str ):
1654
1655
return getattr (x , f )(** kwargs )
1655
1656
1656
1657
return x .apply (f , * args , ** kwargs )
1657
1658
1658
- result = _apply (self ._groupby , func ,
1659
- include_groups = self .include_groups )
1659
+ result = _apply (self ._groupby , func , include_groups = self .include_groups )
1660
1660
return self ._wrap_result (result )
1661
1661
1662
1662
_upsample = _apply
@@ -2074,17 +2074,14 @@ def __init__(
2074
2074
if closed not in {None , "left" , "right" }:
2075
2075
raise ValueError (f"Unsupported value { closed } for `closed`" )
2076
2076
if convention not in {None , "start" , "end" , "e" , "s" }:
2077
- raise ValueError (
2078
- f"Unsupported value { convention } for `convention`" )
2077
+ raise ValueError (f"Unsupported value { convention } for `convention`" )
2079
2078
2080
2079
if (
2081
- (key is None and obj is not None and isinstance (
2082
- obj .index , PeriodIndex )) # type: ignore[attr-defined]
2080
+ (key is None and obj is not None and isinstance (obj .index , PeriodIndex )) # type: ignore[attr-defined]
2083
2081
or (
2084
2082
key is not None
2085
2083
and obj is not None
2086
- # type: ignore[index]
2087
- and getattr (obj [key ], "dtype" , None ) == "period"
2084
+ and getattr (obj [key ], "dtype" , None ) == "period" # type: ignore[index]
2088
2085
)
2089
2086
):
2090
2087
freq = to_offset (freq , is_period = True )
@@ -2304,8 +2301,7 @@ def _adjust_bin_edges(
2304
2301
edges_dti = binner .tz_localize (None )
2305
2302
edges_dti = (
2306
2303
edges_dti
2307
- + Timedelta (days = 1 ,
2308
- unit = edges_dti .unit ).as_unit (edges_dti .unit )
2304
+ + Timedelta (days = 1 , unit = edges_dti .unit ).as_unit (edges_dti .unit )
2309
2305
- Timedelta (1 , unit = edges_dti .unit ).as_unit (edges_dti .unit )
2310
2306
)
2311
2307
bin_edges = edges_dti .tz_localize (binner .tz ).asi8
@@ -2335,8 +2331,7 @@ def _get_time_delta_bins(self, ax: TimedeltaIndex):
2335
2331
)
2336
2332
2337
2333
if not len (ax ):
2338
- binner = labels = TimedeltaIndex (
2339
- data = [], freq = self .freq , name = ax .name )
2334
+ binner = labels = TimedeltaIndex (data = [], freq = self .freq , name = ax .name )
2340
2335
return binner , [], labels
2341
2336
2342
2337
start , end = ax .min (), ax .max ()
@@ -2375,8 +2370,7 @@ def _get_time_period_bins(self, ax: DatetimeIndex):
2375
2370
)
2376
2371
return binner , [], labels
2377
2372
2378
- labels = binner = period_range (
2379
- start = ax [0 ], end = ax [- 1 ], freq = freq , name = ax .name )
2373
+ labels = binner = period_range (start = ax [0 ], end = ax [- 1 ], freq = freq , name = ax .name )
2380
2374
2381
2375
end_stamps = (labels + freq ).asfreq (freq , "s" ).to_timestamp ()
2382
2376
if ax .tz :
@@ -2405,12 +2399,10 @@ def _get_period_bins(self, ax: PeriodIndex):
2405
2399
if not len (memb ):
2406
2400
# index contains no valid (non-NaT) values
2407
2401
bins = np .array ([], dtype = np .int64 )
2408
- binner = labels = PeriodIndex (
2409
- data = [], freq = self .freq , name = ax .name )
2402
+ binner = labels = PeriodIndex (data = [], freq = self .freq , name = ax .name )
2410
2403
if len (ax ) > 0 :
2411
2404
# index is all NaT
2412
- binner , bins , labels = _insert_nat_bin (
2413
- binner , bins , labels , len (ax ))
2405
+ binner , bins , labels = _insert_nat_bin (binner , bins , labels , len (ax ))
2414
2406
return binner , bins , labels
2415
2407
2416
2408
freq_mult = self .freq .n
@@ -2434,8 +2426,7 @@ def _get_period_bins(self, ax: PeriodIndex):
2434
2426
)
2435
2427
2436
2428
# Get offset for bin edge (not label edge) adjustment
2437
- start_offset = Period (start , self .freq ) - \
2438
- Period (p_start , self .freq )
2429
+ start_offset = Period (start , self .freq ) - Period (p_start , self .freq )
2439
2430
# error: Item "Period" of "Union[Period, Any]" has no attribute "n"
2440
2431
bin_shift = start_offset .n % freq_mult # type: ignore[union-attr]
2441
2432
start = p_start
@@ -2459,8 +2450,7 @@ def _get_period_bins(self, ax: PeriodIndex):
2459
2450
bins = memb .searchsorted (prng , side = "left" )
2460
2451
2461
2452
if nat_count > 0 :
2462
- binner , bins , labels = _insert_nat_bin (
2463
- binner , bins , labels , nat_count )
2453
+ binner , bins , labels = _insert_nat_bin (binner , bins , labels , nat_count )
2464
2454
2465
2455
return binner , bins , labels
2466
2456
@@ -2497,8 +2487,7 @@ def _take_new_index(
2497
2487
new_values = algos .take_nd (obj ._values , indexer )
2498
2488
return obj ._constructor (new_values , index = new_index , name = obj .name )
2499
2489
elif isinstance (obj , ABCDataFrame ):
2500
- new_mgr = obj ._mgr .reindex_indexer (
2501
- new_axis = new_index , indexer = indexer , axis = 1 )
2490
+ new_mgr = obj ._mgr .reindex_indexer (new_axis = new_index , indexer = indexer , axis = 1 )
2502
2491
return obj ._constructor_from_mgr (new_mgr , axes = new_mgr .axes )
2503
2492
else :
2504
2493
raise ValueError ("'obj' should be either a Series or a DataFrame" )
@@ -2548,8 +2537,7 @@ def _get_timestamp_range_edges(
2548
2537
if isinstance (freq , Tick ):
2549
2538
index_tz = first .tz
2550
2539
if isinstance (origin , Timestamp ) and (origin .tz is None ) != (index_tz is None ):
2551
- raise ValueError (
2552
- "The origin must have the same timezone as the index." )
2540
+ raise ValueError ("The origin must have the same timezone as the index." )
2553
2541
if origin == "epoch" :
2554
2542
# set the epoch based on the timezone to have similar bins results when
2555
2543
# resampling on the same kind of indexes on different timezones
@@ -2778,8 +2766,7 @@ def asfreq(
2778
2766
if isinstance (obj .index , DatetimeIndex ):
2779
2767
# TODO: should we disallow non-DatetimeIndex?
2780
2768
unit = obj .index .unit
2781
- dti = date_range (obj .index .min (), obj .index .max (),
2782
- freq = freq , unit = unit )
2769
+ dti = date_range (obj .index .min (), obj .index .max (), freq = freq , unit = unit )
2783
2770
dti .name = obj .index .name
2784
2771
new_obj = obj .reindex (dti , method = method , fill_value = fill_value )
2785
2772
if normalize :
@@ -2809,11 +2796,9 @@ def _asfreq_compat(index: FreqIndexT, freq) -> FreqIndexT:
2809
2796
if isinstance (index , PeriodIndex ):
2810
2797
new_index = index .asfreq (freq = freq )
2811
2798
elif isinstance (index , DatetimeIndex ):
2812
- new_index = DatetimeIndex (
2813
- [], dtype = index .dtype , freq = freq , name = index .name )
2799
+ new_index = DatetimeIndex ([], dtype = index .dtype , freq = freq , name = index .name )
2814
2800
elif isinstance (index , TimedeltaIndex ):
2815
- new_index = TimedeltaIndex (
2816
- [], dtype = index .dtype , freq = freq , name = index .name )
2801
+ new_index = TimedeltaIndex ([], dtype = index .dtype , freq = freq , name = index .name )
2817
2802
else : # pragma: no cover
2818
2803
raise TypeError (type (index ))
2819
2804
return new_index
@@ -2830,6 +2815,5 @@ def _apply(
2830
2815
target_category = DeprecationWarning ,
2831
2816
new_message = new_message ,
2832
2817
):
2833
- result = grouped .apply (
2834
- how , * args , include_groups = include_groups , ** kwargs )
2835
- return result
2818
+ result = grouped .apply (how , * args , include_groups = include_groups , ** kwargs )
2819
+ return result
0 commit comments