Skip to content

Commit be1556c

Browse files
no_default (#30788)
1 parent a51a562 commit be1556c

File tree

15 files changed

+43
-49
lines changed

15 files changed

+43
-49
lines changed

doc/source/reference/extensions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ behaves correctly.
6969
api.indexers.check_bool_array_indexer
7070

7171

72-
The sentinel ``pandas.api.extensions._no_default`` is used as the default
72+
The sentinel ``pandas.api.extensions.no_default`` is used as the default
7373
value in some methods. Use an ``is`` comparison to check if the user
7474
provides a non-default value.

pandas/_libs/lib.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2232,14 +2232,14 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
22322232
return objects
22332233

22342234

2235-
# Note: _no_default is exported to the public API in pandas.api.extensions
2236-
_no_default = object() #: Sentinel indicating the default value.
2235+
# Note: no_default is exported to the public API in pandas.api.extensions
2236+
no_default = object() #: Sentinel indicating the default value.
22372237

22382238

22392239
@cython.boundscheck(False)
22402240
@cython.wraparound(False)
22412241
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=1,
2242-
object na_value=_no_default, object dtype=object):
2242+
object na_value=no_default, object dtype=object):
22432243
"""
22442244
Substitute for np.vectorize with pandas-friendly dtype inference.
22452245
@@ -2270,7 +2270,7 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=1,
22702270
result = np.empty(n, dtype=dtype)
22712271
for i in range(n):
22722272
if mask[i]:
2273-
if na_value is _no_default:
2273+
if na_value is no_default:
22742274
val = arr[i]
22752275
else:
22762276
val = na_value

pandas/api/extensions/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Public API for extending pandas objects."""
2-
from pandas._libs.lib import _no_default # noqa: F401
2+
from pandas._libs.lib import no_default # noqa: F401
33

44
from pandas.core.dtypes.dtypes import ( # noqa: F401
55
ExtensionDtype,

pandas/core/arrays/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def __iter__(self):
351351
for i in range(len(self)):
352352
yield self[i]
353353

354-
def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default):
354+
def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default):
355355
"""
356356
Convert to a NumPy ndarray.
357357
@@ -378,9 +378,9 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default):
378378
numpy.ndarray
379379
"""
380380
result = np.asarray(self, dtype=dtype)
381-
if copy or na_value is not lib._no_default:
381+
if copy or na_value is not lib.no_default:
382382
result = result.copy()
383-
if na_value is not lib._no_default:
383+
if na_value is not lib.no_default:
384384
result[self.isna()] = na_value
385385
return result
386386

pandas/core/arrays/boolean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def __getitem__(self, item):
317317
return type(self)(self._data[item], self._mask[item])
318318

319319
def to_numpy(
320-
self, dtype=None, copy=False, na_value: "Scalar" = lib._no_default,
320+
self, dtype=None, copy=False, na_value: "Scalar" = lib.no_default,
321321
):
322322
"""
323323
Convert to a NumPy Array.
@@ -377,7 +377,7 @@ def to_numpy(
377377
>>> a.to_numpy(dtype="bool", na_value=False)
378378
array([ True, False, False])
379379
"""
380-
if na_value is lib._no_default:
380+
if na_value is lib.no_default:
381381
na_value = libmissing.NA
382382
if dtype is None:
383383
dtype = object

pandas/core/arrays/integer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,16 @@ def __getitem__(self, item):
376376

377377
return type(self)(self._data[item], self._mask[item])
378378

379-
def _coerce_to_ndarray(self, dtype=None, na_value=lib._no_default):
379+
def _coerce_to_ndarray(self, dtype=None, na_value=lib.no_default):
380380
"""
381381
coerce to an ndarary of object dtype
382382
"""
383383
if dtype is None:
384384
dtype = object
385385

386-
if na_value is lib._no_default and is_float_dtype(dtype):
386+
if na_value is lib.no_default and is_float_dtype(dtype):
387387
na_value = np.nan
388-
elif na_value is lib._no_default:
388+
elif na_value is lib.no_default:
389389
na_value = libmissing.NA
390390

391391
if is_integer_dtype(dtype):

pandas/core/arrays/numpy_.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,13 @@ def skew(self, axis=None, dtype=None, out=None, keepdims=False, skipna=True):
421421

422422
# ------------------------------------------------------------------------
423423
# Additional Methods
424-
def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default):
424+
def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default):
425425
result = np.asarray(self._ndarray, dtype=dtype)
426426

427-
if (copy or na_value is not lib._no_default) and result is self._ndarray:
427+
if (copy or na_value is not lib.no_default) and result is self._ndarray:
428428
result = result.copy()
429429

430-
if na_value is not lib._no_default:
430+
if na_value is not lib.no_default:
431431
result[self.isna()] = na_value
432432

433433
return result

pandas/core/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def array(self) -> ExtensionArray:
768768

769769
return result
770770

771-
def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default, **kwargs):
771+
def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
772772
"""
773773
A NumPy ndarray representing the values in this Series or Index.
774774
@@ -874,9 +874,9 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default, **kwargs):
874874

875875
result = np.asarray(self._values, dtype=dtype)
876876
# TODO(GH-24345): Avoid potential double copy
877-
if copy or na_value is not lib._no_default:
877+
if copy or na_value is not lib.no_default:
878878
result = result.copy()
879-
if na_value is not lib._no_default:
879+
if na_value is not lib.no_default:
880880
result[self.isna()] = na_value
881881
return result
882882

pandas/core/computation/eval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Optional
99
import warnings
1010

11-
from pandas._libs.lib import _no_default
11+
from pandas._libs.lib import no_default
1212
from pandas.util._validators import validate_bool_kwarg
1313

1414
from pandas.core.computation.engines import _engines
@@ -171,7 +171,7 @@ def eval(
171171
expr,
172172
parser="pandas",
173173
engine: Optional[str] = None,
174-
truediv=_no_default,
174+
truediv=no_default,
175175
local_dict=None,
176176
global_dict=None,
177177
resolvers=(),
@@ -288,7 +288,7 @@ def eval(
288288

289289
inplace = validate_bool_kwarg(inplace, "inplace")
290290

291-
if truediv is not _no_default:
291+
if truediv is not no_default:
292292
warnings.warn(
293293
"The `truediv` parameter in pd.eval is deprecated and will be "
294294
"removed in a future version.",

pandas/core/generic.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@
106106
Name or list of names to sort by""",
107107
)
108108

109-
# sentinel value to use as kwarg in place of None when None has special meaning
110-
# and needs to be distinguished from a user explicitly passing None.
111-
sentinel = object()
112-
113109

114110
def _single_replace(self, to_replace, method, inplace, limit):
115111
"""
@@ -1086,7 +1082,7 @@ def rename(self, *args, **kwargs):
10861082
return result.__finalize__(self)
10871083

10881084
@rewrite_axis_style_signature("mapper", [("copy", True), ("inplace", False)])
1089-
def rename_axis(self, mapper=sentinel, **kwargs):
1085+
def rename_axis(self, mapper=lib.no_default, **kwargs):
10901086
"""
10911087
Set the name of the axis for the index or columns.
10921088
@@ -1211,7 +1207,7 @@ class name
12111207
monkey 2 2
12121208
"""
12131209
axes, kwargs = self._construct_axes_from_arguments(
1214-
(), kwargs, sentinel=sentinel
1210+
(), kwargs, sentinel=lib.no_default
12151211
)
12161212
copy = kwargs.pop("copy", True)
12171213
inplace = kwargs.pop("inplace", False)
@@ -1227,7 +1223,7 @@ class name
12271223

12281224
inplace = validate_bool_kwarg(inplace, "inplace")
12291225

1230-
if mapper is not sentinel:
1226+
if mapper is not lib.no_default:
12311227
# Use v0.23 behavior if a scalar or list
12321228
non_mapper = is_scalar(mapper) or (
12331229
is_list_like(mapper) and not is_dict_like(mapper)
@@ -1243,7 +1239,7 @@ class name
12431239

12441240
for axis in range(self._AXIS_LEN):
12451241
v = axes.get(self._AXIS_NAMES[axis])
1246-
if v is sentinel:
1242+
if v is lib.no_default:
12471243
continue
12481244
non_mapper = is_scalar(v) or (is_list_like(v) and not is_dict_like(v))
12491245
if non_mapper:

pandas/core/indexes/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def _get_time_micros(self):
421421
values = self._data._local_timestamps()
422422
return fields.get_time_micros(values)
423423

424-
def to_series(self, keep_tz=lib._no_default, index=None, name=None):
424+
def to_series(self, keep_tz=lib.no_default, index=None, name=None):
425425
"""
426426
Create a Series with both index and values equal to the index keys
427427
useful with map for returning an indexer based on an index.
@@ -466,7 +466,7 @@ def to_series(self, keep_tz=lib._no_default, index=None, name=None):
466466
if name is None:
467467
name = self.name
468468

469-
if keep_tz is not lib._no_default:
469+
if keep_tz is not lib.no_default:
470470
if keep_tz:
471471
warnings.warn(
472472
"The 'keep_tz' keyword in DatetimeIndex.to_series "

pandas/core/indexes/multi.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
dict(klass="MultiIndex", target_klass="MultiIndex or list of tuples")
6262
)
6363

64-
_no_default_names = object()
65-
6664

6765
class MultiIndexUIntEngine(libindex.BaseMultiIndexCodesEngine, libindex.UInt64Engine):
6866
"""
@@ -373,7 +371,7 @@ def _verify_integrity(
373371
return new_codes
374372

375373
@classmethod
376-
def from_arrays(cls, arrays, sortorder=None, names=_no_default_names):
374+
def from_arrays(cls, arrays, sortorder=None, names=lib.no_default):
377375
"""
378376
Convert arrays to MultiIndex.
379377
@@ -427,7 +425,7 @@ def from_arrays(cls, arrays, sortorder=None, names=_no_default_names):
427425
raise ValueError("all arrays must be same length")
428426

429427
codes, levels = factorize_from_iterables(arrays)
430-
if names is _no_default_names:
428+
if names is lib.no_default:
431429
names = [getattr(arr, "name", None) for arr in arrays]
432430

433431
return MultiIndex(
@@ -497,7 +495,7 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
497495
return MultiIndex.from_arrays(arrays, sortorder=sortorder, names=names)
498496

499497
@classmethod
500-
def from_product(cls, iterables, sortorder=None, names=_no_default_names):
498+
def from_product(cls, iterables, sortorder=None, names=lib.no_default):
501499
"""
502500
Make a MultiIndex from the cartesian product of multiple iterables.
503501
@@ -548,7 +546,7 @@ def from_product(cls, iterables, sortorder=None, names=_no_default_names):
548546
iterables = list(iterables)
549547

550548
codes, levels = factorize_from_iterables(iterables)
551-
if names is _no_default_names:
549+
if names is lib.no_default:
552550
names = [getattr(it, "name", None) for it in iterables]
553551

554552
codes = cartesian_product(codes)

pandas/io/formats/html.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from pandas._config import get_option
99

10+
from pandas._libs import lib
11+
1012
from pandas.core.dtypes.generic import ABCMultiIndex
1113

1214
from pandas import option_context
@@ -245,7 +247,7 @@ def _write_col_header(self, indent: int) -> None:
245247

246248
if self.fmt.sparsify:
247249
# GH3547
248-
sentinel = object()
250+
sentinel = lib.no_default
249251
else:
250252
sentinel = False
251253
levels = self.columns.format(sparsify=sentinel, adjoin=False, names=False)
@@ -451,7 +453,7 @@ def _write_hierarchical_rows(
451453

452454
if self.fmt.sparsify:
453455
# GH3547
454-
sentinel = object()
456+
sentinel = lib.no_default
455457
levels = frame.index.format(sparsify=sentinel, adjoin=False, names=False)
456458

457459
level_lengths = get_level_lengths(levels, sentinel)

pandas/io/formats/style.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from pandas._config import get_option
1717

18+
from pandas._libs import lib
1819
from pandas.compat._optional import import_optional_dependency
1920
from pandas.util._decorators import Appender
2021

@@ -1475,8 +1476,7 @@ def _get_level_lengths(index, hidden_elements=None):
14751476
14761477
Result is a dictionary of (level, initial_position): span
14771478
"""
1478-
sentinel = object()
1479-
levels = index.format(sparsify=sentinel, adjoin=False, names=False)
1479+
levels = index.format(sparsify=lib.no_default, adjoin=False, names=False)
14801480

14811481
if hidden_elements is None:
14821482
hidden_elements = []
@@ -1492,10 +1492,10 @@ def _get_level_lengths(index, hidden_elements=None):
14921492
for j, row in enumerate(lvl):
14931493
if not get_option("display.multi_sparse"):
14941494
lengths[(i, j)] = 1
1495-
elif (row != sentinel) and (j not in hidden_elements):
1495+
elif (row is not lib.no_default) and (j not in hidden_elements):
14961496
last_label = j
14971497
lengths[(i, last_label)] = 1
1498-
elif row != sentinel:
1498+
elif row is not lib.no_default:
14991499
# even if its hidden, keep track of it in case
15001500
# length >1 and later elements are visible
15011501
last_label = j

pandas/tests/extension/decimal/array.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
import numpy as np
77

8-
from pandas._libs import lib
9-
108
from pandas.core.dtypes.base import ExtensionDtype
119

1210
import pandas as pd
13-
from pandas.api.extensions import register_extension_dtype
11+
from pandas.api.extensions import no_default, register_extension_dtype
1412
from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin
1513

1614

@@ -86,7 +84,7 @@ def _from_factorized(cls, values, original):
8684

8785
_HANDLED_TYPES = (decimal.Decimal, numbers.Number, np.ndarray)
8886

89-
def to_numpy(self, dtype=None, copy=False, na_value=lib._no_default, decimals=None):
87+
def to_numpy(self, dtype=None, copy=False, na_value=no_default, decimals=None):
9088
result = np.asarray(self, dtype=dtype)
9189
if decimals is not None:
9290
result = np.asarray([round(x, decimals) for x in result])

0 commit comments

Comments
 (0)