Skip to content

Commit

Permalink
DOC: Improve docstrings for Index.empty, Index.view and Index.names (#…
Browse files Browse the repository at this point in the history
…58642)

* DOC: Improve docstrings for Index.empty, Index.view and Index.names

* pre-commit fixes
  • Loading branch information
Aloqeely authored May 8, 2024
1 parent 4f743f9 commit e0d3061
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
17 changes: 11 additions & 6 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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([])
Expand All @@ -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
Expand Down
60 changes: 46 additions & 14 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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
Expand Down Expand Up @@ -1809,19 +1826,34 @@ 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
--------
>>> idx = pd.Index([1, 2, 3], name="x")
>>> 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,))

Expand Down

0 comments on commit e0d3061

Please sign in to comment.