Skip to content

Commit 1944ab8

Browse files
authored
CLN: deprivatize names in pd.core.common (#27741)
* deprivative funcs in pd.core.common * simplify all/any funcs
1 parent c23c130 commit 1944ab8

File tree

23 files changed

+47
-59
lines changed

23 files changed

+47
-59
lines changed

pandas/core/common.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -165,51 +165,39 @@ def cast_scalar_indexer(val):
165165
return val
166166

167167

168-
def _not_none(*args):
168+
def not_none(*args):
169169
"""
170170
Returns a generator consisting of the arguments that are not None.
171171
"""
172172
return (arg for arg in args if arg is not None)
173173

174174

175-
def _any_none(*args):
175+
def any_none(*args):
176176
"""
177177
Returns a boolean indicating if any argument is None.
178178
"""
179-
for arg in args:
180-
if arg is None:
181-
return True
182-
return False
179+
return any(arg is None for arg in args)
183180

184181

185-
def _all_none(*args):
182+
def all_none(*args):
186183
"""
187184
Returns a boolean indicating if all arguments are None.
188185
"""
189-
for arg in args:
190-
if arg is not None:
191-
return False
192-
return True
186+
return all(arg is None for arg in args)
193187

194188

195-
def _any_not_none(*args):
189+
def any_not_none(*args):
196190
"""
197191
Returns a boolean indicating if any argument is not None.
198192
"""
199-
for arg in args:
200-
if arg is not None:
201-
return True
202-
return False
193+
return any(arg is not None for arg in args)
203194

204195

205-
def _all_not_none(*args):
196+
def all_not_none(*args):
206197
"""
207198
Returns a boolean indicating if all arguments are not None.
208199
"""
209-
for arg in args:
210-
if arg is None:
211-
return False
212-
return True
200+
return all(arg is not None for arg in args)
213201

214202

215203
def count_not_none(*args):
@@ -447,7 +435,7 @@ def random_state(state=None):
447435
)
448436

449437

450-
def _pipe(obj, func, *args, **kwargs):
438+
def pipe(obj, func, *args, **kwargs):
451439
"""
452440
Apply a function ``func`` to object ``obj`` either by passing obj as the
453441
first argument to the function or, in the case that the func is a tuple,
@@ -482,7 +470,7 @@ def _pipe(obj, func, *args, **kwargs):
482470
return func(obj, *args, **kwargs)
483471

484472

485-
def _get_rename_function(mapper):
473+
def get_rename_function(mapper):
486474
"""
487475
Returns a function that will map names/labels, dependent if mapper
488476
is a dict, Series or just a function.

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ def rename(self, *args, **kwargs):
11241124
v = axes.get(self._AXIS_NAMES[axis])
11251125
if v is None:
11261126
continue
1127-
f = com._get_rename_function(v)
1127+
f = com.get_rename_function(v)
11281128
baxis = self._get_block_manager_axis(axis)
11291129
if level is not None:
11301130
level = self.axes[axis]._get_level_number(level)
@@ -1312,7 +1312,7 @@ class name
13121312
if non_mapper:
13131313
newnames = v
13141314
else:
1315-
f = com._get_rename_function(v)
1315+
f = com.get_rename_function(v)
13161316
curnames = self._get_axis(axis).names
13171317
newnames = [f(name) for name in curnames]
13181318
result._set_axis_name(newnames, axis=axis, inplace=True)
@@ -4993,7 +4993,7 @@ def sample(
49934993

49944994
@Appender(_shared_docs["pipe"] % _shared_doc_kwargs)
49954995
def pipe(self, func, *args, **kwargs):
4996-
return com._pipe(self, func, *args, **kwargs)
4996+
return com.pipe(self, func, *args, **kwargs)
49974997

49984998
_shared_docs["aggregate"] = dedent(
49994999
"""

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
361361
# GH12824.
362362
def first_not_none(values):
363363
try:
364-
return next(com._not_none(*values))
364+
return next(com.not_none(*values))
365365
except StopIteration:
366366
return None
367367

pandas/core/groupby/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def __getattr__(self, attr):
590590
)
591591
@Appender(_pipe_template)
592592
def pipe(self, func, *args, **kwargs):
593-
return com._pipe(self, func, *args, **kwargs)
593+
return com.pipe(self, func, *args, **kwargs)
594594

595595
plot = property(GroupByPlot)
596596

@@ -928,7 +928,7 @@ def _concat_objects(self, keys, values, not_indexed_same=False):
928928
def reset_identity(values):
929929
# reset the identities of the components
930930
# of the values to prevent aliasing
931-
for v in com._not_none(*values):
931+
for v in com.not_none(*values):
932932
ax = v._get_axis(self.axis)
933933
ax._reset_identity()
934934
return values

pandas/core/indexes/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _get_consensus_names(indexes):
283283

284284
# find the non-none names, need to tupleify to make
285285
# the set hashable, then reverse on return
286-
consensus_names = {tuple(i.names) for i in indexes if com._any_not_none(*i.names)}
286+
consensus_names = {tuple(i.names) for i in indexes if com.any_not_none(*i.names)}
287287
if len(consensus_names) == 1:
288288
return list(list(consensus_names)[0])
289289
return [None] * indexes[0].nlevels

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3588,8 +3588,8 @@ def _join_multi(self, other, how, return_indexers=True):
35883588
from pandas.core.reshape.merge import _restore_dropped_levels_multijoin
35893589

35903590
# figure out join names
3591-
self_names = set(com._not_none(*self.names))
3592-
other_names = set(com._not_none(*other.names))
3591+
self_names = set(com.not_none(*self.names))
3592+
other_names = set(com.not_none(*other.names))
35933593
overlap = self_names & other_names
35943594

35953595
# need at least 1 in common

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ def date_range(
15691569
dtype='datetime64[ns]', freq='D')
15701570
"""
15711571

1572-
if freq is None and com._any_none(periods, start, end):
1572+
if freq is None and com.any_none(periods, start, end):
15731573
freq = "D"
15741574

15751575
dtarr = DatetimeArray._generate_range(

pandas/core/indexes/interval.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ def _is_type_compatible(a, b):
13181318
(is_number(a) and is_number(b))
13191319
or (is_ts_compat(a) and is_ts_compat(b))
13201320
or (is_td_compat(a) and is_td_compat(b))
1321-
or com._any_none(a, b)
1321+
or com.any_none(a, b)
13221322
)
13231323

13241324

@@ -1416,7 +1416,7 @@ def interval_range(
14161416
end = com.maybe_box_datetimelike(end)
14171417
endpoint = start if start is not None else end
14181418

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

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

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

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

14771477
breaks = np.linspace(start, end, periods)
1478-
if all(is_integer(x) for x in com._not_none(start, end, freq)):
1478+
if all(is_integer(x) for x in com.not_none(start, end, freq)):
14791479
# np.linspace always produces float output
14801480
breaks = maybe_downcast_to_dtype(breaks, "int64")
14811481
else:

pandas/core/indexes/range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __new__(
110110
return cls._simple_new(start, dtype=dtype, name=name)
111111

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

116116
start = ensure_python_int(start) if start is not None else 0

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def timedelta_range(
780780
'5 days 00:00:00'],
781781
dtype='timedelta64[ns]', freq=None)
782782
"""
783-
if freq is None and com._any_none(periods, start, end):
783+
if freq is None and com.any_none(periods, start, end):
784784
freq = "D"
785785

786786
freq, freq_infer = dtl.maybe_infer_freq(freq)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs):
599599

600600
categories = kwargs.get("categories", None)
601601
ordered = kwargs.get("ordered", None)
602-
if com._any_not_none(categories, ordered):
602+
if com.any_not_none(categories, ordered):
603603
dtype = CategoricalDtype(categories, ordered)
604604

605605
if is_categorical_dtype(self.values):

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def __init__(
304304
raise ValueError("No objects to concatenate")
305305

306306
if keys is None:
307-
objs = list(com._not_none(*objs))
307+
objs = list(com.not_none(*objs))
308308
else:
309309
# #1649
310310
clean_keys = []

pandas/core/reshape/merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ def _should_fill(lname, rname):
19581958

19591959

19601960
def _any(x):
1961-
return x is not None and com._any_not_none(*x)
1961+
return x is not None and com.any_not_none(*x)
19621962

19631963

19641964
def validate_operand(obj):

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ def _get_with(self, key):
11821182

11831183
def _get_values_tuple(self, key):
11841184
# mpl hackaround
1185-
if com._any_none(*key):
1185+
if com.any_none(*key):
11861186
return self._get_values(key)
11871187

11881188
if not isinstance(self.index, MultiIndex):

pandas/io/formats/excel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def _format_hierarchical_rows(self):
611611
self.rowcounter += 1
612612

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

616616
for cidx, name in enumerate(index_labels):
617617
yield ExcelCell(self.rowcounter - 1, cidx, name, self.header_style)

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ def _cond(values):
17431743

17441744
def _has_names(index: Index) -> bool:
17451745
if isinstance(index, ABCMultiIndex):
1746-
return com._any_not_none(*index.names)
1746+
return com.any_not_none(*index.names)
17471747
else:
17481748
return index.name is not None
17491749

pandas/io/formats/style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def format_attr(pair):
316316

317317
if (
318318
self.data.index.names
319-
and com._any_not_none(*self.data.index.names)
319+
and com.any_not_none(*self.data.index.names)
320320
and not hidden_index
321321
):
322322
index_header_row = []
@@ -1405,7 +1405,7 @@ def pipe(self, func, *args, **kwargs):
14051405
... .pipe(format_conversion)
14061406
... .set_caption("Results with minimum conversion highlighted."))
14071407
"""
1408-
return com._pipe(self, func, *args, **kwargs)
1408+
return com.pipe(self, func, *args, **kwargs)
14091409

14101410

14111411
def _is_visible(idx_row, idx_col, lengths):

pandas/io/json/_table_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def as_json_table_type(x):
7676

7777
def set_default_names(data):
7878
"""Sets index names to 'index' for regular, or 'level_x' for Multi"""
79-
if com._all_not_none(*data.index.names):
79+
if com.all_not_none(*data.index.names):
8080
nms = data.index.names
8181
if len(nms) == 1 and data.index.name == "index":
8282
warnings.warn("Index name of 'index' is not round-trippable")

pandas/io/pytables.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ def remove(self, key, where=None, start=None, stop=None):
998998
return None
999999

10001000
# remove the node
1001-
if com._all_none(where, start, stop):
1001+
if com.all_none(where, start, stop):
10021002
s.group._f_remove(recursive=True)
10031003

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

pandas/plotting/_matplotlib/core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def _plot(cls, ax, x, y, style=None, is_errorbar=False, **kwds):
654654
def _get_index_name(self):
655655
if isinstance(self.data.index, ABCMultiIndex):
656656
name = self.data.index.names
657-
if com._any_not_none(*name):
657+
if com.any_not_none(*name):
658658
name = ",".join(pprint_thing(x) for x in name)
659659
else:
660660
name = None
@@ -1054,7 +1054,7 @@ def _make_plot(self):
10541054
it = self._iter_data()
10551055

10561056
stacking_id = self._get_stacking_id()
1057-
is_errorbar = com._any_not_none(*self.errors.values())
1057+
is_errorbar = com.any_not_none(*self.errors.values())
10581058

10591059
colors = self._get_colors()
10601060
for i, (label, y) in enumerate(it):

pandas/tests/frame/test_to_csv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def _make_frame(names=None):
655655
df = _make_frame(True)
656656
df.to_csv(path, index=False)
657657
result = read_csv(path, header=[0, 1])
658-
assert com._all_none(*result.columns.names)
658+
assert com.all_none(*result.columns.names)
659659
result.columns.names = df.columns.names
660660
assert_frame_equal(df, result)
661661

pandas/tests/scalar/interval/test_interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,6 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
254254
# GH 18538
255255
left = Timestamp("2017-01-01", tz=tz_left)
256256
right = Timestamp("2017-01-02", tz=tz_right)
257-
error = TypeError if com._any_none(tz_left, tz_right) else ValueError
257+
error = TypeError if com.any_none(tz_left, tz_right) else ValueError
258258
with pytest.raises(error):
259259
Interval(left, right)

pandas/tests/test_common.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def __call__(self):
3333

3434

3535
def test_any_none():
36-
assert com._any_none(1, 2, 3, None)
37-
assert not com._any_none(1, 2, 3, 4)
36+
assert com.any_none(1, 2, 3, None)
37+
assert not com.any_none(1, 2, 3, 4)
3838

3939

4040
def test_all_not_none():
41-
assert com._all_not_none(1, 2, 3, 4)
42-
assert not com._all_not_none(1, 2, 3, None)
43-
assert not com._all_not_none(None, None, None, None)
41+
assert com.all_not_none(1, 2, 3, 4)
42+
assert not com.all_not_none(1, 2, 3, None)
43+
assert not com.all_not_none(None, None, None, None)
4444

4545

4646
def test_random_state():

0 commit comments

Comments
 (0)