diff --git a/pandas/core/base.py b/pandas/core/base.py index bee9b3f13c593..5cdbde8c64c47 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -703,6 +703,11 @@ def empty(self) -> bool: """ Indicator whether Index is empty. + An Index is considered empty if it has no elements. This property can be + useful for quickly checking the state of an Index, especially in data + processing and analysis workflows where handling of empty datasets might + be required. + Returns ------- bool @@ -714,10 +719,10 @@ def empty(self) -> bool: Examples -------- - >>> idx_empty = pd.Index([1, 2, 3]) - >>> idx_empty + >>> idx = pd.Index([1, 2, 3]) + >>> idx Index([1, 2, 3], dtype='int64') - >>> idx_empty.empty + >>> idx.empty False >>> idx_empty = pd.Index([]) @@ -728,10 +733,10 @@ def empty(self) -> bool: If we only have NaNs in our DataFrame, it is not considered empty! - >>> idx_empty = pd.Index([np.nan, np.nan]) - >>> idx_empty + >>> idx = pd.Index([np.nan, np.nan]) + >>> idx Index([nan, nan], dtype='float64') - >>> idx_empty.empty + >>> idx.empty False """ return not self.size diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ef0d25d0f55ac..d128139e0f1c1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1021,7 +1021,13 @@ def ravel(self, order: str_t = "C") -> Self: def view(self, cls=None): """ - Return a view on self. + Return a view of the Index with the specified dtype or a new Index instance. + + This method returns a view of the calling Index object if no arguments are + provided. If a dtype is specified through the `cls` argument, it attempts + to return a view of the Index with the specified dtype. Note that viewing + the Index as a different dtype reinterprets the underlying data, which can + lead to unexpected results for non-numeric or incompatible dtype conversions. Parameters ---------- @@ -1034,27 +1040,38 @@ def view(self, cls=None): Returns ------- - numpy.ndarray - A new view of the same data in memory. + Index or ndarray + A view of the Index. If `cls` is None, the returned object is an Index + view with the same dtype as the calling object. If a numeric `cls` is + specified an ndarray view with the new dtype is returned. + + Raises + ------ + ValueError + If attempting to change to a dtype in a way that is not compatible with + the original dtype's memory layout, for example, viewing an 'int64' Index + as 'str'. See Also -------- + Index.copy : Returns a copy of the Index. numpy.ndarray.view : Returns a new view of array with the same data. Examples -------- - >>> s = pd.Series([1, 2, 3], index=["1", "2", "3"]) - >>> s.index.view("object") - array(['1', '2', '3'], dtype=object) + >>> idx = pd.Index([-1, 0, 1]) + >>> idx.view() + Index([-1, 0, 1], dtype='int64') - >>> s = pd.Series([1, 2, 3], index=[-1, 0, 1]) - >>> s.index.view(np.int64) - array([-1, 0, 1]) - >>> s.index.view(np.float32) - array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32) - >>> s.index.view(np.uint64) + >>> idx.view(np.uint64) array([18446744073709551615, 0, 1], dtype=uint64) + + Viewing as 'int32' or 'float32' reinterprets the memory, which may lead to + unexpected behavior: + + >>> idx.view("float32") + array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32) """ # we need to see if we are subclassing an # index type here @@ -1809,9 +1826,18 @@ def _get_names(self) -> FrozenList: """ Get names on index. + This method returns a FrozenList containing the names of the object. + It's primarily intended for internal use. + + Returns + ------- + FrozenList + A FrozenList containing the object's names, contains None if the object + does not have a name. + See Also -------- - Index.name : Return Index or MultiIndex name. + Index.name : Index name as a string, or None for MultiIndex. Examples -------- @@ -1819,9 +1845,15 @@ def _get_names(self) -> FrozenList: >>> idx.names FrozenList(['x']) - >>> idx = s = pd.Index([1, 2, 3], name=("x", "y")) + >>> idx = pd.Index([1, 2, 3], name=("x", "y")) >>> idx.names FrozenList([('x', 'y')]) + + If the index does not have a name set: + + >>> idx = pd.Index([1, 2, 3]) + >>> idx.names + FrozenList([None]) """ return FrozenList((self.name,))