Skip to content

Commit 5b5b2fe

Browse files
rockgwesm
authored andcommitted
PEP: pandas/core round 5 (nanops, ops, panel*)
Author: rockg <[email protected]> Closes #12079 from rockg/pep8-round5 and squashes the following commits: ab9f1e1 [rockg] PEP: pandas/core round 5 (nanops, ops, panel*)
1 parent 118fd01 commit 5b5b2fe

File tree

5 files changed

+334
-289
lines changed

5 files changed

+334
-289
lines changed

pandas/core/nanops.py

+39-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import functools
33
import numpy as np
4+
import operator
45

56
try:
67
import bottleneck as bn
@@ -10,13 +11,10 @@
1011

1112
import pandas.hashtable as _hash
1213
from pandas import compat, lib, algos, tslib
13-
from pandas.compat import builtins
1414
from pandas.core.common import (isnull, notnull, _values_from_object,
15-
_maybe_upcast_putmask,
16-
ensure_float, _ensure_float64,
17-
_ensure_int64, _ensure_object,
18-
is_float, is_integer, is_complex,
19-
is_float_dtype,
15+
_maybe_upcast_putmask, _ensure_float64,
16+
_ensure_int64, _ensure_object, is_float,
17+
is_integer, is_complex, is_float_dtype,
2018
is_complex_dtype, is_integer_dtype,
2119
is_bool_dtype, is_object_dtype,
2220
is_datetime64_dtype, is_timedelta64_dtype,
@@ -26,7 +24,6 @@
2624

2725

2826
class disallow(object):
29-
3027
def __init__(self, *dtypes):
3128
super(disallow, self).__init__()
3229
self.dtypes = tuple(np.dtype(dtype).type for dtype in dtypes)
@@ -41,8 +38,8 @@ def _f(*args, **kwargs):
4138
obj_iter = itertools.chain(args, compat.itervalues(kwargs))
4239
if any(self.check(obj) for obj in obj_iter):
4340
raise TypeError('reduction operation {0!r} not allowed for '
44-
'this dtype'.format(f.__name__.replace('nan',
45-
'')))
41+
'this dtype'.format(
42+
f.__name__.replace('nan', '')))
4643
try:
4744
return f(*args, **kwargs)
4845
except ValueError as e:
@@ -53,11 +50,11 @@ def _f(*args, **kwargs):
5350
if is_object_dtype(args[0]):
5451
raise TypeError(e)
5552
raise
53+
5654
return _f
5755

5856

5957
class bottleneck_switch(object):
60-
6158
def __init__(self, zero_value=None, **kwargs):
6259
self.zero_value = zero_value
6360
self.kwargs = kwargs
@@ -91,8 +88,8 @@ def f(values, axis=None, skipna=True, **kwds):
9188
result.fill(0)
9289
return result
9390

94-
if _USE_BOTTLENECK and skipna and _bn_ok_dtype(values.dtype,
95-
bn_name):
91+
if (_USE_BOTTLENECK and skipna and
92+
_bn_ok_dtype(values.dtype, bn_name)):
9693
result = bn_func(values, axis=axis, **kwds)
9794

9895
# prefer to treat inf/-inf as NA, but must compute the func
@@ -121,8 +118,7 @@ def f(values, axis=None, skipna=True, **kwds):
121118

122119
def _bn_ok_dtype(dt, name):
123120
# Bottleneck chokes on datetime64
124-
if (not is_object_dtype(dt) and
125-
not is_datetime_or_timedelta_dtype(dt)):
121+
if (not is_object_dtype(dt) and not is_datetime_or_timedelta_dtype(dt)):
126122

127123
# bottleneck does not properly upcast during the sum
128124
# so can overflow
@@ -142,7 +138,7 @@ def _has_infs(result):
142138
return lib.has_infs_f4(result.ravel())
143139
try:
144140
return np.isinf(result).any()
145-
except (TypeError, NotImplementedError) as e:
141+
except (TypeError, NotImplementedError):
146142
# if it doesn't support infs, then it can't have infs
147143
return False
148144

@@ -173,8 +169,9 @@ def _get_fill_value(dtype, fill_value=None, fill_value_typ=None):
173169
def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
174170
isfinite=False, copy=True):
175171
""" utility to get the values view, mask, dtype
176-
if necessary copy and mask using the specified fill_value
177-
copy = True will force the copy """
172+
if necessary copy and mask using the specified fill_value
173+
copy = True will force the copy
174+
"""
178175
values = _values_from_object(values)
179176
if isfinite:
180177
mask = _isfinite(values)
@@ -331,7 +328,8 @@ def get_median(x):
331328
if values.ndim > 1:
332329
# there's a non-empty array to apply over otherwise numpy raises
333330
if notempty:
334-
return _wrap_results(np.apply_along_axis(get_median, axis, values), dtype)
331+
return _wrap_results(
332+
np.apply_along_axis(get_median, axis, values), dtype)
335333

336334
# must return the correct shape, but median is not defined for the
337335
# empty set so return nans of shape "everything but the passed axis"
@@ -400,7 +398,7 @@ def nanvar(values, axis=None, skipna=True, ddof=1):
400398
avg = _ensure_numeric(values.sum(axis=axis, dtype=np.float64)) / count
401399
if axis is not None:
402400
avg = np.expand_dims(avg, axis)
403-
sqr = _ensure_numeric((avg - values) ** 2)
401+
sqr = _ensure_numeric((avg - values)**2)
404402
np.putmask(sqr, mask, 0)
405403
result = sqr.sum(axis=axis, dtype=np.float64) / d
406404

@@ -429,13 +427,10 @@ def _nanminmax(meth, fill_value_typ):
429427
@bottleneck_switch()
430428
def reduction(values, axis=None, skipna=True):
431429
values, mask, dtype, dtype_max = _get_values(
432-
values,
433-
skipna,
434-
fill_value_typ=fill_value_typ,
435-
)
430+
values, skipna, fill_value_typ=fill_value_typ, )
436431

437-
if ((axis is not None and values.shape[axis] == 0)
438-
or values.size == 0):
432+
if ((axis is not None and values.shape[axis] == 0) or
433+
values.size == 0):
439434
try:
440435
result = getattr(values, meth)(axis, dtype=dtype_max)
441436
result.fill(np.nan)
@@ -477,7 +472,7 @@ def nanargmin(values, axis=None, skipna=True):
477472
return result
478473

479474

480-
@disallow('M8','m8')
475+
@disallow('M8', 'm8')
481476
def nanskew(values, axis=None, skipna=True):
482477

483478
mask = isnull(values)
@@ -493,15 +488,15 @@ def nanskew(values, axis=None, skipna=True):
493488

494489
typ = values.dtype.type
495490
A = values.sum(axis) / count
496-
B = (values ** 2).sum(axis) / count - A ** typ(2)
497-
C = (values ** 3).sum(axis) / count - A ** typ(3) - typ(3) * A * B
491+
B = (values**2).sum(axis) / count - A**typ(2)
492+
C = (values**3).sum(axis) / count - A**typ(3) - typ(3) * A * B
498493

499494
# floating point error
500495
B = _zero_out_fperr(B)
501496
C = _zero_out_fperr(C)
502497

503498
result = ((np.sqrt(count * count - count) * C) /
504-
((count - typ(2)) * np.sqrt(B) ** typ(3)))
499+
((count - typ(2)) * np.sqrt(B)**typ(3)))
505500

506501
if isinstance(result, np.ndarray):
507502
result = np.where(B == 0, 0, result)
@@ -514,7 +509,7 @@ def nanskew(values, axis=None, skipna=True):
514509
return result
515510

516511

517-
@disallow('M8','m8')
512+
@disallow('M8', 'm8')
518513
def nankurt(values, axis=None, skipna=True):
519514

520515
mask = isnull(values)
@@ -530,22 +525,25 @@ def nankurt(values, axis=None, skipna=True):
530525

531526
typ = values.dtype.type
532527
A = values.sum(axis) / count
533-
B = (values ** 2).sum(axis) / count - A ** typ(2)
534-
C = (values ** 3).sum(axis) / count - A ** typ(3) - typ(3) * A * B
535-
D = (values ** 4).sum(axis) / count - A ** typ(4) - typ(6) * B * A * A - typ(4) * C * A
528+
B = (values**2).sum(axis) / count - A**typ(2)
529+
C = (values**3).sum(axis) / count - A**typ(3) - typ(3) * A * B
530+
D = ((values**4).sum(axis) / count - A**typ(4) -
531+
typ(6) * B * A * A - typ(4) * C * A)
536532

537533
B = _zero_out_fperr(B)
538534
D = _zero_out_fperr(D)
539535

540536
if not isinstance(B, np.ndarray):
541-
# if B is a scalar, check these corner cases first before doing division
537+
# if B is a scalar, check these corner cases first before doing
538+
# division
542539
if count < 4:
543540
return np.nan
544541
if B == 0:
545542
return 0
546543

547-
result = (((count * count - typ(1)) * D / (B * B) - typ(3) * ((count - typ(1)) ** typ(2))) /
548-
((count - typ(2)) * (count - typ(3))))
544+
result = (((count * count - typ(1)) * D / (B * B) - typ(3) *
545+
((count - typ(1))**typ(2))) / ((count - typ(2)) *
546+
(count - typ(3))))
549547

550548
if isinstance(result, np.ndarray):
551549
result = np.where(B == 0, 0, result)
@@ -554,7 +552,7 @@ def nankurt(values, axis=None, skipna=True):
554552
return result
555553

556554

557-
@disallow('M8','m8')
555+
@disallow('M8', 'm8')
558556
def nanprod(values, axis=None, skipna=True):
559557
mask = isnull(values)
560558
if skipna and not is_any_int_dtype(values):
@@ -621,7 +619,7 @@ def _zero_out_fperr(arg):
621619
return arg.dtype.type(0) if np.abs(arg) < 1e-14 else arg
622620

623621

624-
@disallow('M8','m8')
622+
@disallow('M8', 'm8')
625623
def nancorr(a, b, method='pearson', min_periods=None):
626624
"""
627625
a, b: ndarrays
@@ -668,7 +666,7 @@ def _spearman(a, b):
668666
return _cor_methods[method]
669667

670668

671-
@disallow('M8','m8')
669+
@disallow('M8', 'm8')
672670
def nancov(a, b, min_periods=None):
673671
if len(a) != len(b):
674672
raise AssertionError('Operands to nancov must have same size')
@@ -711,8 +709,6 @@ def _ensure_numeric(x):
711709

712710
# NA-friendly array comparisons
713711

714-
import operator
715-
716712

717713
def make_nancomp(op):
718714
def f(x, y):
@@ -728,8 +724,10 @@ def f(x, y):
728724
np.putmask(result, mask, np.nan)
729725

730726
return result
727+
731728
return f
732729

730+
733731
nangt = make_nancomp(operator.gt)
734732
nange = make_nancomp(operator.ge)
735733
nanlt = make_nancomp(operator.lt)

0 commit comments

Comments
 (0)