Skip to content

Commit

Permalink
CLN: Use pandas.core.common for None checks (pandas-dev#17816)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschendel authored and Brian Tu committed Oct 14, 2017
1 parent bbe248c commit 44b08f2
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 75 deletions.
27 changes: 20 additions & 7 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,44 @@ def _mut_exclusive(**kwargs):


def _not_none(*args):
"""Returns a generator consisting of the arguments that are not None"""
return (arg for arg in args if arg is not None)


def _any_none(*args):
"""Returns a boolean indicating if any argument is None"""
for arg in args:
if arg is None:
return True
return False


def _all_none(*args):
"""Returns a boolean indicating if all arguments are None"""
for arg in args:
if arg is not None:
return False
return True


def _any_not_none(*args):
"""Returns a boolean indicating if any argument is not None"""
for arg in args:
if arg is not None:
return True
return False


def _all_not_none(*args):
"""Returns a boolean indicating if all arguments are not None"""
for arg in args:
if arg is None:
return False
return True


def _count_not_none(*args):
"""Returns the count of arguments that are not None"""
return sum(x is not None for x in args)


Expand Down Expand Up @@ -459,13 +479,6 @@ def _apply_if_callable(maybe_callable, obj, **kwargs):
return maybe_callable


def _all_none(*args):
for arg in args:
if arg is not None:
return False
return True


def _where_compat(mask, arr1, arr2):
if arr1.dtype == _NS_DTYPE and arr2.dtype == _NS_DTYPE:
new_vals = np.where(mask, arr1.view('i8'), arr2.view('i8'))
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
from pandas.core.dtypes.cast import maybe_promote, maybe_upcast_putmask
from pandas.core.dtypes.missing import isna, notna
from pandas.core.dtypes.generic import ABCSeries, ABCPanel, ABCDataFrame

from pandas.core.common import (_all_not_none,
_values_from_object,
_maybe_box_datetimelike,
SettingWithCopyError, SettingWithCopyWarning,
AbstractMethodError)
from pandas.core.common import (_all_not_none, _count_not_none,
_maybe_box_datetimelike, _values_from_object,
AbstractMethodError, SettingWithCopyError,
SettingWithCopyWarning)

from pandas.core.base import PandasObject, SelectionMixin
from pandas.core.index import (Index, MultiIndex, _ensure_index,
Expand Down Expand Up @@ -3252,7 +3250,7 @@ def filter(self, items=None, like=None, regex=None, axis=None):
"""
import re

nkw = sum([x is not None for x in [items, like, regex]])
nkw = _count_not_none(items, like, regex)
if nkw > 1:
raise TypeError('Keyword arguments `items`, `like`, or `regex` '
'are mutually exclusive')
Expand Down
28 changes: 13 additions & 15 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
from pandas.core.dtypes.missing import isna, notna, _maybe_fill

from pandas.core.common import (_values_from_object, AbstractMethodError,
_default_index)
_default_index, _not_none, _get_callable_name,
_asarray_tuplesafe)

from pandas.core.base import (PandasObject, SelectionMixin, GroupByError,
DataError, SpecificationError)
Expand All @@ -60,7 +61,6 @@
from pandas.util._validators import validate_kwargs

import pandas.core.algorithms as algorithms
import pandas.core.common as com
from pandas.core.config import option_context

from pandas.plotting._core import boxplot_frame_groupby
Expand Down Expand Up @@ -877,10 +877,9 @@ def _concat_objects(self, keys, values, not_indexed_same=False):
def reset_identity(values):
# reset the identities of the components
# of the values to prevent aliasing
for v in values:
if v is not None:
ax = v._get_axis(self.axis)
ax._reset_identity()
for v in _not_none(*values):
ax = v._get_axis(self.axis)
ax._reset_identity()
return values

if not not_indexed_same:
Expand Down Expand Up @@ -1806,7 +1805,7 @@ def apply(self, f, data, axis=0):
group_keys = self._get_group_keys()

# oh boy
f_name = com._get_callable_name(f)
f_name = _get_callable_name(f)
if (f_name not in _plotting_methods and
hasattr(splitter, 'fast_apply') and axis == 0):
try:
Expand Down Expand Up @@ -2533,7 +2532,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
self.grouper = self.obj[self.name]

elif isinstance(self.grouper, (list, tuple)):
self.grouper = com._asarray_tuplesafe(self.grouper)
self.grouper = _asarray_tuplesafe(self.grouper)

# a passed Categorical
elif is_categorical_dtype(self.grouper):
Expand Down Expand Up @@ -2739,7 +2738,7 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
if not any_callable and not all_in_columns_index and \
not any_arraylike and not any_groupers and \
match_axis_length and level is None:
keys = [com._asarray_tuplesafe(keys)]
keys = [_asarray_tuplesafe(keys)]

if isinstance(level, (tuple, list)):
if key is None:
Expand Down Expand Up @@ -3028,7 +3027,7 @@ def _aggregate_multiple_funcs(self, arg, _level):
columns.append(f)
else:
# protect against callables without names
columns.append(com._get_callable_name(f))
columns.append(_get_callable_name(f))
arg = lzip(columns, arg)

results = {}
Expand Down Expand Up @@ -3686,14 +3685,13 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
key_names = self.grouper.names

# GH12824.
def first_non_None_value(values):
def first_not_none(values):
try:
v = next(v for v in values if v is not None)
return next(_not_none(*values))
except StopIteration:
return None
return v

v = first_non_None_value(values)
v = first_not_none(values)

if v is None:
# GH9684. If all values are None, then this will throw an error.
Expand Down Expand Up @@ -3726,7 +3724,7 @@ def first_non_None_value(values):
key_index = None

# make Nones an empty object
v = first_non_None_value(values)
v = first_not_none(values)
if v is None:
return DataFrame()
elif isinstance(v, NDFrame):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _get_consensus_names(indexes):
# find the non-none names, need to tupleify to make
# the set hashable, then reverse on return
consensus_names = set([tuple(i.names) for i in indexes
if any(n is not None for n in i.names)])
if com._any_not_none(*i.names)])
if len(consensus_names) == 1:
return list(list(consensus_names)[0])
return [None] * indexes[0].nlevels
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@
needs_i8_conversion,
is_iterator, is_list_like,
is_scalar)
from pandas.core.common import (is_bool_indexer,
_values_from_object,
_asarray_tuplesafe)
from pandas.core.common import (is_bool_indexer, _values_from_object,
_asarray_tuplesafe, _not_none,
_index_labels_to_array)

from pandas.core.base import PandasObject, IndexOpsMixin
import pandas.core.base as base
from pandas.util._decorators import (
Appender, Substitution, cache_readonly, deprecate_kwarg)
from pandas.core.indexes.frozen import FrozenList
import pandas.core.common as com
import pandas.core.dtypes.concat as _concat
import pandas.core.missing as missing
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -3168,8 +3167,8 @@ def _join_multi(self, other, how, return_indexers=True):
other_is_mi = isinstance(other, MultiIndex)

# figure out join names
self_names = [n for n in self.names if n is not None]
other_names = [n for n in other.names if n is not None]
self_names = _not_none(*self.names)
other_names = _not_none(*other.names)
overlap = list(set(self_names) & set(other_names))

# need at least 1 in common, but not more than 1
Expand Down Expand Up @@ -3714,7 +3713,7 @@ def drop(self, labels, errors='raise'):
-------
dropped : Index
"""
labels = com._index_labels_to_array(labels)
labels = _index_labels_to_array(labels)
indexer = self.get_indexer(labels)
mask = indexer == -1
if mask.any():
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
is_scalar)
from pandas.core.dtypes.missing import isna, array_equivalent
from pandas.errors import PerformanceWarning, UnsortedIndexError
from pandas.core.common import (_values_from_object,
from pandas.core.common import (_any_not_none,
_values_from_object,
is_bool_indexer,
is_null_slice,
is_true_slices)
Expand Down Expand Up @@ -509,7 +510,7 @@ def _format_attrs(self):
max_seq_items=False)),
('labels', ibase.default_pprint(self._labels,
max_seq_items=False))]
if not all(name is None for name in self.names):
if _any_not_none(*self.names):
attrs.append(('names', ibase.default_pprint(self.names)))
if self.sortorder is not None:
attrs.append(('sortorder', ibase.default_pprint(self.sortorder)))
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas import compat
from pandas.compat import lrange, range
from pandas.compat.numpy import function as nv
from pandas.core.common import _all_none
from pandas.core.indexes.base import Index, _index_shared_docs
from pandas.util._decorators import Appender, cache_readonly
import pandas.core.dtypes.concat as _concat
Expand Down Expand Up @@ -83,7 +84,7 @@ def _ensure_int(value, field):

return new_value

if start is None and stop is None and step is None:
if _all_none(start, stop, step):
msg = "RangeIndex(...) must be called with integers"
raise TypeError(msg)
elif start is None:
Expand Down
16 changes: 9 additions & 7 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
is_string_like, is_scalar)
from pandas.core.dtypes.missing import notna

import pandas.core.common as com
import pandas.core.ops as ops
import pandas.core.missing as missing
from pandas import compat
from pandas.compat import (map, zip, range, u, OrderedDict)
from pandas.compat.numpy import function as nv
from pandas.core.common import _try_sort, _default_index
from pandas.core.common import (_try_sort, _default_index, _all_not_none,
_any_not_none, _apply_if_callable)
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame, _shared_docs
from pandas.core.index import (Index, MultiIndex, _ensure_index,
Expand Down Expand Up @@ -166,7 +166,7 @@ def _init_data(self, data, copy, dtype, **kwargs):

axes = None
if isinstance(data, BlockManager):
if any(x is not None for x in passed_axes):
if _any_not_none(*passed_axes):
axes = [x if x is not None else y
for x, y in zip(passed_axes, data.axes)]
mgr = data
Expand All @@ -178,7 +178,7 @@ def _init_data(self, data, copy, dtype, **kwargs):
mgr = self._init_matrix(data, passed_axes, dtype=dtype, copy=copy)
copy = False
dtype = None
elif is_scalar(data) and all(x is not None for x in passed_axes):
elif is_scalar(data) and _all_not_none(*passed_axes):
values = cast_scalar_to_array([len(x) for x in passed_axes],
data, dtype=dtype)
mgr = self._init_matrix(values, passed_axes, dtype=values.dtype,
Expand Down Expand Up @@ -279,7 +279,7 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None):
return cls(**d)

def __getitem__(self, key):
key = com._apply_if_callable(key, self)
key = _apply_if_callable(key, self)

if isinstance(self._info_axis, MultiIndex):
return self._getitem_multilevel(key)
Expand Down Expand Up @@ -594,7 +594,7 @@ def _box_item_values(self, key, values):
return self._constructor_sliced(values, **d)

def __setitem__(self, key, value):
key = com._apply_if_callable(key, self)
key = _apply_if_callable(key, self)
shape = tuple(self.shape)
if isinstance(value, self._constructor_sliced):
value = value.reindex(
Expand All @@ -616,7 +616,9 @@ def __setitem__(self, key, value):

def _unpickle_panel_compat(self, state): # pragma: no cover
"Unpickle the panel"
_unpickle = com._unpickle_array
from pandas.io.pickle import _unpickle_array

_unpickle = _unpickle_array
vals, items, major, minor = state

items = _unpickle(items)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
raise ValueError('No objects to concatenate')

if keys is None:
objs = [obj for obj in objs if obj is not None]
objs = list(com._not_none(*objs))
else:
# #1649
clean_keys = []
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,4 +1550,4 @@ def _should_fill(lname, rname):


def _any(x):
return x is not None and len(x) > 0 and any([y is not None for y in x])
return x is not None and com._any_not_none(*x)
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
SettingWithCopyError,
_maybe_box_datetimelike,
_dict_compat,
standardize_mapping)
standardize_mapping,
_any_none)
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
Float64Index, _ensure_index)
from pandas.core.indexing import check_bool_indexer, maybe_convert_indices
Expand Down Expand Up @@ -713,7 +714,7 @@ def _get_with(self, key):

def _get_values_tuple(self, key):
# mpl hackaround
if any(k is None for k in key):
if _any_none(*key):
return self._get_values(key)

if not isinstance(self.index, MultiIndex):
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from pandas.core.base import (PandasObject, SelectionMixin,
GroupByMixin)
import pandas.core.common as com
from pandas.core.common import _asarray_tuplesafe, _count_not_none
import pandas._libs.window as _window

from pandas import compat
Expand Down Expand Up @@ -535,7 +535,7 @@ def _prep_window(self, **kwargs):

window = self._get_window()
if isinstance(window, (list, tuple, np.ndarray)):
return com._asarray_tuplesafe(window).astype(float)
return _asarray_tuplesafe(window).astype(float)
elif is_integer(window):
import scipy.signal as sig

Expand Down Expand Up @@ -1972,8 +1972,7 @@ def dataframe_from_int_dict(data, frame_template):


def _get_center_of_mass(com, span, halflife, alpha):
valid_count = len([x for x in [com, span, halflife, alpha]
if x is not None])
valid_count = _count_not_none(com, span, halflife, alpha)
if valid_count > 1:
raise ValueError("com, span, halflife, and alpha "
"are mutually exclusive")
Expand Down
Loading

0 comments on commit 44b08f2

Please sign in to comment.