30
30
31
31
from pandas ._config import config
32
32
33
- from pandas ._libs import Timestamp , iNaT , lib
33
+ from pandas ._libs import Timestamp , lib
34
34
from pandas ._typing import (
35
35
Axis ,
36
36
FilePathOrBuffer ,
@@ -4558,6 +4558,10 @@ def filter(
4558
4558
>>> df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6])),
4559
4559
... index=['mouse', 'rabbit'],
4560
4560
... columns=['one', 'two', 'three'])
4561
+ >>> df
4562
+ one two three
4563
+ mouse 1 2 3
4564
+ rabbit 4 5 6
4561
4565
4562
4566
>>> # select columns by name
4563
4567
>>> df.filter(items=['one', 'three'])
@@ -10102,8 +10106,6 @@ def mad(self, axis=None, skipna=None, level=None):
10102
10106
desc = "minimum" ,
10103
10107
accum_func = np .minimum .accumulate ,
10104
10108
accum_func_name = "min" ,
10105
- mask_a = np .inf ,
10106
- mask_b = np .nan ,
10107
10109
examples = _cummin_examples ,
10108
10110
)
10109
10111
cls .cumsum = _make_cum_function (
@@ -10115,8 +10117,6 @@ def mad(self, axis=None, skipna=None, level=None):
10115
10117
desc = "sum" ,
10116
10118
accum_func = np .cumsum ,
10117
10119
accum_func_name = "sum" ,
10118
- mask_a = 0.0 ,
10119
- mask_b = np .nan ,
10120
10120
examples = _cumsum_examples ,
10121
10121
)
10122
10122
cls .cumprod = _make_cum_function (
@@ -10128,8 +10128,6 @@ def mad(self, axis=None, skipna=None, level=None):
10128
10128
desc = "product" ,
10129
10129
accum_func = np .cumprod ,
10130
10130
accum_func_name = "prod" ,
10131
- mask_a = 1.0 ,
10132
- mask_b = np .nan ,
10133
10131
examples = _cumprod_examples ,
10134
10132
)
10135
10133
cls .cummax = _make_cum_function (
@@ -10141,8 +10139,6 @@ def mad(self, axis=None, skipna=None, level=None):
10141
10139
desc = "maximum" ,
10142
10140
accum_func = np .maximum .accumulate ,
10143
10141
accum_func_name = "max" ,
10144
- mask_a = - np .inf ,
10145
- mask_b = np .nan ,
10146
10142
examples = _cummax_examples ,
10147
10143
)
10148
10144
@@ -11182,8 +11178,6 @@ def _make_cum_function(
11182
11178
desc : str ,
11183
11179
accum_func : Callable ,
11184
11180
accum_func_name : str ,
11185
- mask_a : float ,
11186
- mask_b : float ,
11187
11181
examples : str ,
11188
11182
) -> Callable :
11189
11183
@Substitution (
@@ -11205,61 +11199,15 @@ def cum_func(self, axis=None, skipna=True, *args, **kwargs):
11205
11199
if axis == 1 :
11206
11200
return cum_func (self .T , axis = 0 , skipna = skipna , * args , ** kwargs ).T
11207
11201
11208
- def na_accum_func (blk_values ):
11209
- # We will be applying this function to block values
11210
- if blk_values .dtype .kind in ["m" , "M" ]:
11211
- # GH#30460, GH#29058
11212
- # numpy 1.18 started sorting NaTs at the end instead of beginning,
11213
- # so we need to work around to maintain backwards-consistency.
11214
- orig_dtype = blk_values .dtype
11215
-
11216
- # We need to define mask before masking NaTs
11217
- mask = isna (blk_values )
11218
-
11219
- if accum_func == np .minimum .accumulate :
11220
- # Note: the accum_func comparison fails as an "is" comparison
11221
- y = blk_values .view ("i8" )
11222
- y [mask ] = np .iinfo (np .int64 ).max
11223
- changed = True
11224
- else :
11225
- y = blk_values
11226
- changed = False
11227
-
11228
- result = accum_func (y .view ("i8" ), axis )
11229
- if skipna :
11230
- np .putmask (result , mask , iNaT )
11231
- elif accum_func == np .minimum .accumulate :
11232
- # Restore NaTs that we masked previously
11233
- nz = (~ np .asarray (mask )).nonzero ()[0 ]
11234
- if len (nz ):
11235
- # everything up to the first non-na entry stays NaT
11236
- result [: nz [0 ]] = iNaT
11237
-
11238
- if changed :
11239
- # restore NaT elements
11240
- y [mask ] = iNaT # TODO: could try/finally for this?
11241
-
11242
- if isinstance (blk_values , np .ndarray ):
11243
- result = result .view (orig_dtype )
11244
- else :
11245
- # DatetimeArray
11246
- result = type (blk_values )._from_sequence (result , dtype = orig_dtype )
11247
-
11248
- elif skipna and not issubclass (
11249
- blk_values .dtype .type , (np .integer , np .bool_ )
11250
- ):
11251
- vals = blk_values .copy ().T
11252
- mask = isna (vals )
11253
- np .putmask (vals , mask , mask_a )
11254
- result = accum_func (vals , axis )
11255
- np .putmask (result , mask , mask_b )
11256
- else :
11257
- result = accum_func (blk_values .T , axis )
11202
+ def block_accum_func (blk_values ):
11203
+ values = blk_values .T if hasattr (blk_values , "T" ) else blk_values
11204
+
11205
+ result = nanops .na_accum_func (values , accum_func , skipna = skipna )
11258
11206
11259
- # transpose back for ndarray, not for EA
11260
- return result . T if hasattr ( result , "T" ) else result
11207
+ result = result . T if hasattr ( result , "T" ) else result
11208
+ return result
11261
11209
11262
- result = self ._data .apply (na_accum_func )
11210
+ result = self ._data .apply (block_accum_func )
11263
11211
11264
11212
d = self ._construct_axes_dict ()
11265
11213
d ["copy" ] = False
0 commit comments