Skip to content

CLN: deprivatize names in pd.core.common #27741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,51 +165,39 @@ def cast_scalar_indexer(val):
return val


def _not_none(*args):
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):
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
return any(arg is None for arg in args)


def _all_none(*args):
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
return all(arg is None for arg in args)


def _any_not_none(*args):
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
return any(arg is not None for arg in args)


def _all_not_none(*args):
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
return all(arg is not None for arg in args)


def count_not_none(*args):
Expand Down Expand Up @@ -447,7 +435,7 @@ def random_state(state=None):
)


def _pipe(obj, func, *args, **kwargs):
def pipe(obj, func, *args, **kwargs):
"""
Apply a function ``func`` to object ``obj`` either by passing obj as the
first argument to the function or, in the case that the func is a tuple,
Expand Down Expand Up @@ -482,7 +470,7 @@ def _pipe(obj, func, *args, **kwargs):
return func(obj, *args, **kwargs)


def _get_rename_function(mapper):
def get_rename_function(mapper):
"""
Returns a function that will map names/labels, dependent if mapper
is a dict, Series or just a function.
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ def rename(self, *args, **kwargs):
v = axes.get(self._AXIS_NAMES[axis])
if v is None:
continue
f = com._get_rename_function(v)
f = com.get_rename_function(v)
baxis = self._get_block_manager_axis(axis)
if level is not None:
level = self.axes[axis]._get_level_number(level)
Expand Down Expand Up @@ -1312,7 +1312,7 @@ class name
if non_mapper:
newnames = v
else:
f = com._get_rename_function(v)
f = com.get_rename_function(v)
curnames = self._get_axis(axis).names
newnames = [f(name) for name in curnames]
result._set_axis_name(newnames, axis=axis, inplace=True)
Expand Down Expand Up @@ -4993,7 +4993,7 @@ def sample(

@Appender(_shared_docs["pipe"] % _shared_doc_kwargs)
def pipe(self, func, *args, **kwargs):
return com._pipe(self, func, *args, **kwargs)
return com.pipe(self, func, *args, **kwargs)

_shared_docs["aggregate"] = dedent(
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
# GH12824.
def first_not_none(values):
try:
return next(com._not_none(*values))
return next(com.not_none(*values))
except StopIteration:
return None

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def __getattr__(self, attr):
)
@Appender(_pipe_template)
def pipe(self, func, *args, **kwargs):
return com._pipe(self, func, *args, **kwargs)
return com.pipe(self, func, *args, **kwargs)

plot = property(GroupByPlot)

Expand Down Expand Up @@ -928,7 +928,7 @@ 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 com._not_none(*values):
for v in com.not_none(*values):
ax = v._get_axis(self.axis)
ax._reset_identity()
return values
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 @@ -283,7 +283,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 = {tuple(i.names) for i in indexes if com._any_not_none(*i.names)}
consensus_names = {tuple(i.names) for i in indexes 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
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3588,8 +3588,8 @@ def _join_multi(self, other, how, return_indexers=True):
from pandas.core.reshape.merge import _restore_dropped_levels_multijoin

# figure out join names
self_names = set(com._not_none(*self.names))
other_names = set(com._not_none(*other.names))
self_names = set(com.not_none(*self.names))
other_names = set(com.not_none(*other.names))
overlap = self_names & other_names

# need at least 1 in common
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,7 @@ def date_range(
dtype='datetime64[ns]', freq='D')
"""

if freq is None and com._any_none(periods, start, end):
if freq is None and com.any_none(periods, start, end):
freq = "D"

dtarr = DatetimeArray._generate_range(
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def _is_type_compatible(a, b):
(is_number(a) and is_number(b))
or (is_ts_compat(a) and is_ts_compat(b))
or (is_td_compat(a) and is_td_compat(b))
or com._any_none(a, b)
or com.any_none(a, b)
)


Expand Down Expand Up @@ -1416,7 +1416,7 @@ def interval_range(
end = com.maybe_box_datetimelike(end)
endpoint = start if start is not None else end

if freq is None and com._any_none(periods, start, end):
if freq is None and com.any_none(periods, start, end):
freq = 1 if is_number(endpoint) else "D"

if com.count_not_none(start, end, periods, freq) != 3:
Expand Down Expand Up @@ -1463,7 +1463,7 @@ def interval_range(

if is_number(endpoint):
# force consistency between start/end/freq (lower end if freq skips it)
if com._all_not_none(start, end, freq):
if com.all_not_none(start, end, freq):
end -= (end - start) % freq

# compute the period/start/end if unspecified (at most one)
Expand All @@ -1475,7 +1475,7 @@ def interval_range(
end = start + (periods - 1) * freq

breaks = np.linspace(start, end, periods)
if all(is_integer(x) for x in com._not_none(start, end, freq)):
if all(is_integer(x) for x in com.not_none(start, end, freq)):
# np.linspace always produces float output
breaks = maybe_downcast_to_dtype(breaks, "int64")
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __new__(
return cls._simple_new(start, dtype=dtype, name=name)

# validate the arguments
if com._all_none(start, stop, step):
if com.all_none(start, stop, step):
raise TypeError("RangeIndex(...) must be called with integers")

start = ensure_python_int(start) if start is not None else 0
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def timedelta_range(
'5 days 00:00:00'],
dtype='timedelta64[ns]', freq=None)
"""
if freq is None and com._any_none(periods, start, end):
if freq is None and com.any_none(periods, start, end):
freq = "D"

freq, freq_infer = dtl.maybe_infer_freq(freq)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs):

categories = kwargs.get("categories", None)
ordered = kwargs.get("ordered", None)
if com._any_not_none(categories, ordered):
if com.any_not_none(categories, ordered):
dtype = CategoricalDtype(categories, ordered)

if is_categorical_dtype(self.values):
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 @@ -304,7 +304,7 @@ def __init__(
raise ValueError("No objects to concatenate")

if keys is None:
objs = list(com._not_none(*objs))
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 @@ -1958,7 +1958,7 @@ def _should_fill(lname, rname):


def _any(x):
return x is not None and com._any_not_none(*x)
return x is not None and com.any_not_none(*x)


def validate_operand(obj):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def _get_with(self, key):

def _get_values_tuple(self, key):
# mpl hackaround
if com._any_none(*key):
if com.any_none(*key):
return self._get_values(key)

if not isinstance(self.index, MultiIndex):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def _format_hierarchical_rows(self):
self.rowcounter += 1

# if index labels are not empty go ahead and dump
if com._any_not_none(*index_labels) and self.header is not False:
if com.any_not_none(*index_labels) and self.header is not False:

for cidx, name in enumerate(index_labels):
yield ExcelCell(self.rowcounter - 1, cidx, name, self.header_style)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ def _cond(values):

def _has_names(index: Index) -> bool:
if isinstance(index, ABCMultiIndex):
return com._any_not_none(*index.names)
return com.any_not_none(*index.names)
else:
return index.name is not None

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def format_attr(pair):

if (
self.data.index.names
and com._any_not_none(*self.data.index.names)
and com.any_not_none(*self.data.index.names)
and not hidden_index
):
index_header_row = []
Expand Down Expand Up @@ -1405,7 +1405,7 @@ def pipe(self, func, *args, **kwargs):
... .pipe(format_conversion)
... .set_caption("Results with minimum conversion highlighted."))
"""
return com._pipe(self, func, *args, **kwargs)
return com.pipe(self, func, *args, **kwargs)


def _is_visible(idx_row, idx_col, lengths):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def as_json_table_type(x):

def set_default_names(data):
"""Sets index names to 'index' for regular, or 'level_x' for Multi"""
if com._all_not_none(*data.index.names):
if com.all_not_none(*data.index.names):
nms = data.index.names
if len(nms) == 1 and data.index.name == "index":
warnings.warn("Index name of 'index' is not round-trippable")
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ def remove(self, key, where=None, start=None, stop=None):
return None

# remove the node
if com._all_none(where, start, stop):
if com.all_none(where, start, stop):
s.group._f_remove(recursive=True)

# delete from the table
Expand Down Expand Up @@ -2634,7 +2634,7 @@ def delete(self, where=None, start=None, stop=None, **kwargs):
support fully deleting the node in its entirety (only) - where
specification must be None
"""
if com._all_none(where, start, stop):
if com.all_none(where, start, stop):
self._handle.remove_node(self.group, recursive=True)
return None

Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def _plot(cls, ax, x, y, style=None, is_errorbar=False, **kwds):
def _get_index_name(self):
if isinstance(self.data.index, ABCMultiIndex):
name = self.data.index.names
if com._any_not_none(*name):
if com.any_not_none(*name):
name = ",".join(pprint_thing(x) for x in name)
else:
name = None
Expand Down Expand Up @@ -1054,7 +1054,7 @@ def _make_plot(self):
it = self._iter_data()

stacking_id = self._get_stacking_id()
is_errorbar = com._any_not_none(*self.errors.values())
is_errorbar = com.any_not_none(*self.errors.values())

colors = self._get_colors()
for i, (label, y) in enumerate(it):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def _make_frame(names=None):
df = _make_frame(True)
df.to_csv(path, index=False)
result = read_csv(path, header=[0, 1])
assert com._all_none(*result.columns.names)
assert com.all_none(*result.columns.names)
result.columns.names = df.columns.names
assert_frame_equal(df, result)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
# GH 18538
left = Timestamp("2017-01-01", tz=tz_left)
right = Timestamp("2017-01-02", tz=tz_right)
error = TypeError if com._any_none(tz_left, tz_right) else ValueError
error = TypeError if com.any_none(tz_left, tz_right) else ValueError
with pytest.raises(error):
Interval(left, right)
10 changes: 5 additions & 5 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def __call__(self):


def test_any_none():
assert com._any_none(1, 2, 3, None)
assert not com._any_none(1, 2, 3, 4)
assert com.any_none(1, 2, 3, None)
assert not com.any_none(1, 2, 3, 4)


def test_all_not_none():
assert com._all_not_none(1, 2, 3, 4)
assert not com._all_not_none(1, 2, 3, None)
assert not com._all_not_none(None, None, None, None)
assert com.all_not_none(1, 2, 3, 4)
assert not com.all_not_none(1, 2, 3, None)
assert not com.all_not_none(None, None, None, None)


def test_random_state():
Expand Down