Skip to content

Commit

Permalink
CLN/INT: Rename _possibly to _maybe (GH15764)
Browse files Browse the repository at this point in the history
Also rename "private" functions in pandas.type.cast
closes pandas-dev#15764

Author: Dominik Stanczak <stanczakdominik@gmail.com>

Closes pandas-dev#15771 from StanczakDominik/rename-possibly and squashes the following commits:

486b932 [Dominik Stanczak] Cleanup missed linting errors
188c48b [Dominik Stanczak] CLN/INT: Rename _possibly to _maybe
  • Loading branch information
StanczakDominik authored and jreback committed Mar 22, 2017
1 parent bc1235e commit 2a3b05a
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 231 deletions.
34 changes: 17 additions & 17 deletions pandas/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _rewrite_membership_op(self, node, left, right):
op = self.visit(op_instance)
return op, op_instance, left, right

def _possibly_transform_eq_ne(self, node, left=None, right=None):
def _maybe_transform_eq_ne(self, node, left=None, right=None):
if left is None:
left = self.visit(node.left, side='left')
if right is None:
Expand All @@ -357,7 +357,7 @@ def _possibly_transform_eq_ne(self, node, left=None, right=None):
right)
return op, op_class, left, right

def _possibly_downcast_constants(self, left, right):
def _maybe_downcast_constants(self, left, right):
f32 = np.dtype(np.float32)
if left.isscalar and not right.isscalar and right.return_type == f32:
# right is a float32 array, left is a scalar
Expand All @@ -370,7 +370,7 @@ def _possibly_downcast_constants(self, left, right):

return left, right

def _possibly_eval(self, binop, eval_in_python):
def _maybe_eval(self, binop, eval_in_python):
# eval `in` and `not in` (for now) in "partial" python space
# things that can be evaluated in "eval" space will be turned into
# temporary variables. for example,
Expand All @@ -380,10 +380,10 @@ def _possibly_eval(self, binop, eval_in_python):
return binop.evaluate(self.env, self.engine, self.parser,
self.term_type, eval_in_python)

def _possibly_evaluate_binop(self, op, op_class, lhs, rhs,
eval_in_python=('in', 'not in'),
maybe_eval_in_python=('==', '!=', '<', '>',
'<=', '>=')):
def _maybe_evaluate_binop(self, op, op_class, lhs, rhs,
eval_in_python=('in', 'not in'),
maybe_eval_in_python=('==', '!=', '<', '>',
'<=', '>=')):
res = op(lhs, rhs)

if res.has_invalid_return_type:
Expand All @@ -397,24 +397,24 @@ def _possibly_evaluate_binop(self, op, op_class, lhs, rhs,
getattr(rhs, 'is_datetime', False)):
# all date ops must be done in python bc numexpr doesn't work
# well with NaT
return self._possibly_eval(res, self.binary_ops)
return self._maybe_eval(res, self.binary_ops)

if res.op in eval_in_python:
# "in"/"not in" ops are always evaluated in python
return self._possibly_eval(res, eval_in_python)
return self._maybe_eval(res, eval_in_python)
elif self.engine != 'pytables':
if (getattr(lhs, 'return_type', None) == object or
getattr(rhs, 'return_type', None) == object):
# evaluate "==" and "!=" in python if either of our operands
# has an object return type
return self._possibly_eval(res, eval_in_python +
maybe_eval_in_python)
return self._maybe_eval(res, eval_in_python +
maybe_eval_in_python)
return res

def visit_BinOp(self, node, **kwargs):
op, op_class, left, right = self._possibly_transform_eq_ne(node)
left, right = self._possibly_downcast_constants(left, right)
return self._possibly_evaluate_binop(op, op_class, left, right)
op, op_class, left, right = self._maybe_transform_eq_ne(node)
left, right = self._maybe_downcast_constants(left, right)
return self._maybe_evaluate_binop(op, op_class, left, right)

def visit_Div(self, node, **kwargs):
truediv = self.env.scope['truediv']
Expand Down Expand Up @@ -662,9 +662,9 @@ def visitor(x, y):
lhs = self._try_visit_binop(x)
rhs = self._try_visit_binop(y)

op, op_class, lhs, rhs = self._possibly_transform_eq_ne(node, lhs,
rhs)
return self._possibly_evaluate_binop(op, node.op, lhs, rhs)
op, op_class, lhs, rhs = self._maybe_transform_eq_ne(
node, lhs, rhs)
return self._maybe_evaluate_binop(op, node.op, lhs, rhs)

operands = node.values
return reduce(visitor, operands)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from pandas import compat, _np_version_under1p8
from pandas.types.cast import _maybe_promote
from pandas.types.cast import maybe_promote
from pandas.types.generic import ABCSeries, ABCIndex
from pandas.types.common import (is_unsigned_integer_dtype,
is_signed_integer_dtype,
Expand Down Expand Up @@ -1297,7 +1297,7 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
else:
# check for promotion based on types only (do this first because
# it's faster than computing a mask)
dtype, fill_value = _maybe_promote(arr.dtype, fill_value)
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
if dtype != arr.dtype and (out is None or out.dtype != dtype):
# check if promotion is actually required based on indexer
if mask_info is not None:
Expand Down Expand Up @@ -1380,7 +1380,7 @@ def take_2d_multi(arr, indexer, out=None, fill_value=np.nan, mask_info=None,
else:
# check for promotion based on types only (do this first because
# it's faster than computing a mask)
dtype, fill_value = _maybe_promote(arr.dtype, fill_value)
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
if dtype != arr.dtype and (out is None or out.dtype != dtype):
# check if promotion is actually required based on indexer
if mask_info is not None:
Expand Down
21 changes: 10 additions & 11 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from pandas.types.generic import ABCSeries, ABCIndexClass, ABCCategoricalIndex
from pandas.types.missing import isnull, notnull
from pandas.types.cast import (_possibly_infer_to_datetimelike,
_coerce_indexer_dtype)
from pandas.types.cast import (maybe_infer_to_datetimelike,
coerce_indexer_dtype)
from pandas.types.dtypes import CategoricalDtype
from pandas.types.common import (_ensure_int64,
_ensure_object,
Expand Down Expand Up @@ -237,7 +237,7 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):

if fastpath:
# fast path
self._codes = _coerce_indexer_dtype(values, categories)
self._codes = coerce_indexer_dtype(values, categories)
self._categories = self._validate_categories(
categories, fastpath=isinstance(categories, ABCIndexClass))
self._ordered = ordered
Expand Down Expand Up @@ -266,8 +266,7 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):
# correctly no need here this is an issue because _sanitize_array
# also coerces np.nan to a string under certain versions of numpy
# as well
values = _possibly_infer_to_datetimelike(values,
convert_dates=True)
values = maybe_infer_to_datetimelike(values, convert_dates=True)
if not isinstance(values, np.ndarray):
values = _convert_to_list_like(values)
from pandas.core.series import _sanitize_array
Expand Down Expand Up @@ -324,7 +323,7 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):

self.set_ordered(ordered or False, inplace=True)
self._categories = categories
self._codes = _coerce_indexer_dtype(codes, categories)
self._codes = coerce_indexer_dtype(codes, categories)

@property
def _constructor(self):
Expand Down Expand Up @@ -877,7 +876,7 @@ def add_categories(self, new_categories, inplace=False):
new_categories = list(self._categories) + list(new_categories)
cat = self if inplace else self.copy()
cat._categories = self._validate_categories(new_categories)
cat._codes = _coerce_indexer_dtype(cat._codes, new_categories)
cat._codes = coerce_indexer_dtype(cat._codes, new_categories)
if not inplace:
return cat

Expand Down Expand Up @@ -961,7 +960,7 @@ def remove_unused_categories(self, inplace=False):
idx, inv = idx[1:], inv - 1

cat._categories = cat.categories.take(idx)
cat._codes = _coerce_indexer_dtype(inv, self._categories)
cat._codes = coerce_indexer_dtype(inv, self._categories)

if not inplace:
return cat
Expand Down Expand Up @@ -1065,8 +1064,8 @@ def __setstate__(self, state):
state['_categories'] = self._validate_categories(state.pop(
'_levels'))
if '_codes' not in state and 'labels' in state:
state['_codes'] = _coerce_indexer_dtype(state.pop('labels'),
state['_categories'])
state['_codes'] = coerce_indexer_dtype(
state.pop('labels'), state['_categories'])

# 0.16.0 ordered change
if '_ordered' not in state:
Expand Down Expand Up @@ -2062,7 +2061,7 @@ def _get_codes_for_values(values, categories):
(_, _), cats = _get_data_algo(categories, _hashtables)
t = hash_klass(len(cats))
t.map_locations(cats)
return _coerce_indexer_dtype(t.lookup(vals), cats)
return coerce_indexer_dtype(t.lookup(vals), cats)


def _convert_to_list_like(list_like):
Expand Down
54 changes: 27 additions & 27 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import numpy as np
import numpy.ma as ma

from pandas.types.cast import (_maybe_upcast, _infer_dtype_from_scalar,
_possibly_cast_to_datetime,
_possibly_infer_to_datetimelike,
_possibly_convert_platform,
_possibly_downcast_to_dtype,
_invalidate_string_dtypes,
_coerce_to_dtypes,
_maybe_upcast_putmask,
_find_common_type)
from pandas.types.cast import (maybe_upcast, infer_dtype_from_scalar,
maybe_cast_to_datetime,
maybe_infer_to_datetimelike,
maybe_convert_platform,
maybe_downcast_to_dtype,
invalidate_string_dtypes,
coerce_to_dtypes,
maybe_upcast_putmask,
find_common_type)
from pandas.types.common import (is_categorical_dtype,
is_object_dtype,
is_extension_type,
Expand Down Expand Up @@ -275,7 +275,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
else:
mask = ma.getmaskarray(data)
if mask.any():
data, fill_value = _maybe_upcast(data, copy=True)
data, fill_value = maybe_upcast(data, copy=True)
data[mask] = fill_value
else:
data = data.copy()
Expand Down Expand Up @@ -335,7 +335,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
if isinstance(data, compat.string_types) and dtype is None:
dtype = np.object_
if dtype is None:
dtype, data = _infer_dtype_from_scalar(data)
dtype, data = infer_dtype_from_scalar(data)

values = np.empty((len(index), len(columns)), dtype=dtype)
values.fill(data)
Expand Down Expand Up @@ -469,7 +469,7 @@ def _get_axes(N, K, index=index, columns=columns):
# on the entire block; this is to convert if we have datetimelike's
# embedded in an object type
if dtype is None and is_object_dtype(values):
values = _possibly_infer_to_datetimelike(values)
values = maybe_infer_to_datetimelike(values)

return create_block_manager_from_blocks([values], [columns, index])

Expand Down Expand Up @@ -2359,7 +2359,7 @@ def select_dtypes(self, include=None, exclude=None):
include, exclude = map(
lambda x: frozenset(map(_get_dtype_from_object, x)), selection)
for dtypes in (include, exclude):
_invalidate_string_dtypes(dtypes)
invalidate_string_dtypes(dtypes)

# can't both include AND exclude!
if not include.isdisjoint(exclude):
Expand Down Expand Up @@ -2659,7 +2659,7 @@ def reindexer(value):
value = _sanitize_index(value, self.index, copy=False)
if not isinstance(value, (np.ndarray, Index)):
if isinstance(value, list) and len(value) > 0:
value = _possibly_convert_platform(value)
value = maybe_convert_platform(value)
else:
value = com._asarray_tuplesafe(value)
elif value.ndim == 2:
Expand All @@ -2671,13 +2671,13 @@ def reindexer(value):

# possibly infer to datetimelike
if is_object_dtype(value.dtype):
value = _possibly_infer_to_datetimelike(value)
value = maybe_infer_to_datetimelike(value)

else:
# upcast the scalar
dtype, value = _infer_dtype_from_scalar(value)
dtype, value = infer_dtype_from_scalar(value)
value = np.repeat(value, len(self.index)).astype(dtype)
value = _possibly_cast_to_datetime(value, dtype)
value = maybe_cast_to_datetime(value, dtype)

# return internal types directly
if is_extension_type(value):
Expand Down Expand Up @@ -3000,8 +3000,8 @@ def _maybe_casted_values(index, labels=None):
else:
values = values.take(labels)
if mask.any():
values, changed = _maybe_upcast_putmask(values, mask,
np.nan)
values, changed = maybe_upcast_putmask(
values, mask, np.nan)
return values

new_index = _default_index(len(new_obj))
Expand Down Expand Up @@ -3722,7 +3722,7 @@ def combine(self, other, func, fill_value=None, overwrite=True):
# if we have different dtypes, possibily promote
new_dtype = this_dtype
if not is_dtype_equal(this_dtype, other_dtype):
new_dtype = _find_common_type([this_dtype, other_dtype])
new_dtype = find_common_type([this_dtype, other_dtype])
if not is_dtype_equal(this_dtype, new_dtype):
series = series.astype(new_dtype)
if not is_dtype_equal(other_dtype, new_dtype):
Expand All @@ -3743,13 +3743,13 @@ def combine(self, other, func, fill_value=None, overwrite=True):
# try to downcast back to the original dtype
if needs_i8_conversion_i:
# ToDo: This conversion should be handled in
# _possibly_cast_to_datetime but the change affects lot...
# _maybe_cast_to_datetime but the change affects lot...
if is_datetime64tz_dtype(new_dtype):
arr = DatetimeIndex._simple_new(arr, tz=new_dtype.tz)
else:
arr = _possibly_cast_to_datetime(arr, new_dtype)
arr = maybe_cast_to_datetime(arr, new_dtype)
else:
arr = _possibly_downcast_to_dtype(arr, this_dtype)
arr = maybe_downcast_to_dtype(arr, this_dtype)

result[col] = arr

Expand Down Expand Up @@ -5003,7 +5003,7 @@ def f(x):

# try to coerce to the original dtypes item by item if we can
if axis == 0:
result = _coerce_to_dtypes(result, self.dtypes)
result = coerce_to_dtypes(result, self.dtypes)

return Series(result, index=labels)

Expand Down Expand Up @@ -5505,7 +5505,7 @@ def _prep_ndarray(values, copy=True):
return np.empty((0, 0), dtype=object)

def convert(v):
return _possibly_convert_platform(v)
return maybe_convert_platform(v)

# we could have a 1-dim or 2-dim list here
# this is equiv of np.asarray, but does object conversion
Expand Down Expand Up @@ -5601,7 +5601,7 @@ def _masked_rec_array_to_mgr(data, index, columns, dtype, copy):
for fv, arr, col in zip(fill_value, arrays, arr_columns):
mask = ma.getmaskarray(data[col])
if mask.any():
arr, fv = _maybe_upcast(arr, fill_value=fv, copy=True)
arr, fv = maybe_upcast(arr, fill_value=fv, copy=True)
arr[mask] = fv
new_arrays.append(arr)

Expand Down Expand Up @@ -5699,7 +5699,7 @@ def _convert_object_array(content, columns, coerce_float=False, dtype=None):
def convert(arr):
if dtype != object and dtype != np.object:
arr = lib.maybe_convert_objects(arr, try_float=coerce_float)
arr = _possibly_cast_to_datetime(arr, dtype)
arr = maybe_cast_to_datetime(arr, dtype)
return arr

arrays = [convert(arr) for arr in content]
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
is_list_like,
is_dict_like,
is_re_compilable)
from pandas.types.cast import _maybe_promote, _maybe_upcast_putmask
from pandas.types.cast import maybe_promote, maybe_upcast_putmask
from pandas.types.missing import isnull, notnull
from pandas.types.generic import ABCSeries, ABCPanel

Expand Down Expand Up @@ -4956,10 +4956,10 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
# or not try_quick
if not try_quick:

dtype, fill_value = _maybe_promote(other.dtype)
dtype, fill_value = maybe_promote(other.dtype)
new_other = np.empty(len(icond), dtype=dtype)
new_other.fill(fill_value)
_maybe_upcast_putmask(new_other, icond, other)
maybe_upcast_putmask(new_other, icond, other)
other = new_other

else:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
_ensure_object,
_ensure_categorical,
_ensure_float)
from pandas.types.cast import _possibly_downcast_to_dtype
from pandas.types.cast import maybe_downcast_to_dtype
from pandas.types.missing import isnull, notnull, _maybe_fill

from pandas.core.common import (_values_from_object, AbstractMethodError,
Expand Down Expand Up @@ -783,7 +783,7 @@ def _try_cast(self, result, obj, numeric_only=False):

if not is_scalar(result):
if numeric_only and is_numeric_dtype(dtype) or not numeric_only:
result = _possibly_downcast_to_dtype(result, dtype)
result = maybe_downcast_to_dtype(result, dtype)

return result

Expand Down Expand Up @@ -2914,7 +2914,7 @@ def transform(self, func, *args, **kwargs):
# the cython take a different path (and casting)
dtype = self._selected_obj.dtype
if is_numeric_dtype(dtype):
result = _possibly_downcast_to_dtype(result, dtype)
result = maybe_downcast_to_dtype(result, dtype)

result.name = self._selected_obj.name
result.index = self._selected_obj.index
Expand Down
Loading

0 comments on commit 2a3b05a

Please sign in to comment.