Skip to content

Commit e0d3061

Browse files
authored
DOC: Improve docstrings for Index.empty, Index.view and Index.names (#58642)
* DOC: Improve docstrings for Index.empty, Index.view and Index.names * pre-commit fixes
1 parent 4f743f9 commit e0d3061

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

pandas/core/base.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ def empty(self) -> bool:
703703
"""
704704
Indicator whether Index is empty.
705705
706+
An Index is considered empty if it has no elements. This property can be
707+
useful for quickly checking the state of an Index, especially in data
708+
processing and analysis workflows where handling of empty datasets might
709+
be required.
710+
706711
Returns
707712
-------
708713
bool
@@ -714,10 +719,10 @@ def empty(self) -> bool:
714719
715720
Examples
716721
--------
717-
>>> idx_empty = pd.Index([1, 2, 3])
718-
>>> idx_empty
722+
>>> idx = pd.Index([1, 2, 3])
723+
>>> idx
719724
Index([1, 2, 3], dtype='int64')
720-
>>> idx_empty.empty
725+
>>> idx.empty
721726
False
722727
723728
>>> idx_empty = pd.Index([])
@@ -728,10 +733,10 @@ def empty(self) -> bool:
728733
729734
If we only have NaNs in our DataFrame, it is not considered empty!
730735
731-
>>> idx_empty = pd.Index([np.nan, np.nan])
732-
>>> idx_empty
736+
>>> idx = pd.Index([np.nan, np.nan])
737+
>>> idx
733738
Index([nan, nan], dtype='float64')
734-
>>> idx_empty.empty
739+
>>> idx.empty
735740
False
736741
"""
737742
return not self.size

pandas/core/indexes/base.py

+46-14
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,13 @@ def ravel(self, order: str_t = "C") -> Self:
10211021

10221022
def view(self, cls=None):
10231023
"""
1024-
Return a view on self.
1024+
Return a view of the Index with the specified dtype or a new Index instance.
1025+
1026+
This method returns a view of the calling Index object if no arguments are
1027+
provided. If a dtype is specified through the `cls` argument, it attempts
1028+
to return a view of the Index with the specified dtype. Note that viewing
1029+
the Index as a different dtype reinterprets the underlying data, which can
1030+
lead to unexpected results for non-numeric or incompatible dtype conversions.
10251031
10261032
Parameters
10271033
----------
@@ -1034,27 +1040,38 @@ def view(self, cls=None):
10341040
10351041
Returns
10361042
-------
1037-
numpy.ndarray
1038-
A new view of the same data in memory.
1043+
Index or ndarray
1044+
A view of the Index. If `cls` is None, the returned object is an Index
1045+
view with the same dtype as the calling object. If a numeric `cls` is
1046+
specified an ndarray view with the new dtype is returned.
1047+
1048+
Raises
1049+
------
1050+
ValueError
1051+
If attempting to change to a dtype in a way that is not compatible with
1052+
the original dtype's memory layout, for example, viewing an 'int64' Index
1053+
as 'str'.
10391054
10401055
See Also
10411056
--------
1057+
Index.copy : Returns a copy of the Index.
10421058
numpy.ndarray.view : Returns a new view of array with the same data.
10431059
10441060
Examples
10451061
--------
1046-
>>> s = pd.Series([1, 2, 3], index=["1", "2", "3"])
1047-
>>> s.index.view("object")
1048-
array(['1', '2', '3'], dtype=object)
1062+
>>> idx = pd.Index([-1, 0, 1])
1063+
>>> idx.view()
1064+
Index([-1, 0, 1], dtype='int64')
10491065
1050-
>>> s = pd.Series([1, 2, 3], index=[-1, 0, 1])
1051-
>>> s.index.view(np.int64)
1052-
array([-1, 0, 1])
1053-
>>> s.index.view(np.float32)
1054-
array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32)
1055-
>>> s.index.view(np.uint64)
1066+
>>> idx.view(np.uint64)
10561067
array([18446744073709551615, 0, 1],
10571068
dtype=uint64)
1069+
1070+
Viewing as 'int32' or 'float32' reinterprets the memory, which may lead to
1071+
unexpected behavior:
1072+
1073+
>>> idx.view("float32")
1074+
array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32)
10581075
"""
10591076
# we need to see if we are subclassing an
10601077
# index type here
@@ -1809,19 +1826,34 @@ def _get_names(self) -> FrozenList:
18091826
"""
18101827
Get names on index.
18111828
1829+
This method returns a FrozenList containing the names of the object.
1830+
It's primarily intended for internal use.
1831+
1832+
Returns
1833+
-------
1834+
FrozenList
1835+
A FrozenList containing the object's names, contains None if the object
1836+
does not have a name.
1837+
18121838
See Also
18131839
--------
1814-
Index.name : Return Index or MultiIndex name.
1840+
Index.name : Index name as a string, or None for MultiIndex.
18151841
18161842
Examples
18171843
--------
18181844
>>> idx = pd.Index([1, 2, 3], name="x")
18191845
>>> idx.names
18201846
FrozenList(['x'])
18211847
1822-
>>> idx = s = pd.Index([1, 2, 3], name=("x", "y"))
1848+
>>> idx = pd.Index([1, 2, 3], name=("x", "y"))
18231849
>>> idx.names
18241850
FrozenList([('x', 'y')])
1851+
1852+
If the index does not have a name set:
1853+
1854+
>>> idx = pd.Index([1, 2, 3])
1855+
>>> idx.names
1856+
FrozenList([None])
18251857
"""
18261858
return FrozenList((self.name,))
18271859

0 commit comments

Comments
 (0)