Skip to content

DOC: Add missing docstrings for Index.empty, Index.view and Index.names #57546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d489b6b
Add missing docstring for pandas.Index.empty
merlinymy Feb 21, 2024
f855eb5
Add docstring for pandas.Index.names
merlinymy Feb 21, 2024
bcce3ff
Add docstring for pandas.Index.view
merlinymy Feb 21, 2024
7fda5f5
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 5, 2024
4b79d59
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
0fb33e4
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
07abdbc
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
59e25f6
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
e7f9285
Update pandas/core/base.py
datapythonista Mar 5, 2024
6cb9cd8
Update pandas/core/base.py
datapythonista Mar 5, 2024
a2e456a
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
92302df
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
4babd7d
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
1e188bc
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
79a009a
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
ee928ea
Update pandas/core/base.py
datapythonista Mar 5, 2024
4a4e8d1
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 5, 2024
e16c557
Update pandas/core/indexes/base.py
datapythonista Mar 5, 2024
fd90389
Update pandas/core/indexes/base.py
datapythonista Mar 6, 2024
bf87610
Update pandas/core/base.py
datapythonista Mar 6, 2024
7d8a295
Update pandas/core/base.py
datapythonista Mar 6, 2024
946c80e
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 14, 2024
ca55f90
Update pandas/core/base.py
datapythonista Mar 14, 2024
06e88a4
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
3380ce0
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
f369d95
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
29c0611
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 16, 2024
a7c5a95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2024
e9c46eb
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
937dc05
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
72ce2d8
Update pandas/core/indexes/base.py
datapythonista Mar 16, 2024
f361480
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 16, 2024
e39ee39
Update pandas/core/indexes/base.py
datapythonista Mar 17, 2024
358c804
Update pandas/core/indexes/base.py
datapythonista Mar 17, 2024
0aba2d6
Merge branch 'main' into merlin-GL08-doc
datapythonista Mar 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Index.dropna RT03,SA01" \
-i "pandas.Index.dtype SA01" \
-i "pandas.Index.duplicated RT03" \
-i "pandas.Index.empty GL08" \
-i "pandas.Index.equals SA01" \
-i "pandas.Index.fillna RT03" \
-i "pandas.Index.get_indexer PR07,SA01" \
Expand All @@ -209,7 +208,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Index.map SA01" \
-i "pandas.Index.memory_usage RT03" \
-i "pandas.Index.name SA01" \
-i "pandas.Index.names GL08" \
-i "pandas.Index.nbytes SA01" \
-i "pandas.Index.ndim SA01" \
-i "pandas.Index.nunique RT03" \
Expand All @@ -227,7 +225,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Index.union PR07,RT03,SA01" \
-i "pandas.Index.unique RT03" \
-i "pandas.Index.value_counts RT03" \
-i "pandas.Index.view GL08" \
-i "pandas.Int16Dtype SA01" \
-i "pandas.Int32Dtype SA01" \
-i "pandas.Int64Dtype SA01" \
Expand Down
27 changes: 27 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,33 @@ def to_numpy(
@final
@property
def empty(self) -> bool:
"""
Check if the 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
``True`` if the Index is empty, ``False`` otherwise.

See Also
--------
Index.size : Return the number of elements in the Index.

Examples
--------
>>> index = pd.Index([])
>>> index.empty
True

>>> non_empty_index = pd.Index([1, 2, 3])
>>> non_empty_index.empty
False
"""
return not self.size

@doc(op="max", oppose="min", value="largest")
Expand Down
78 changes: 78 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,53 @@ def ravel(self, order: str_t = "C") -> Self:
return self[:]

def view(self, cls=None):
"""
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
----------
cls : dtype, optional
The dtype for the returned view of the Index. If ``None`` (default), a view
of the calling Index is returned without altering the dtype. If a numeric
dtype is specified, the method attempts to reinterpret the Index data as
the specified dtype.

Returns
-------
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.

Examples
--------
>>> idx = pd.Index([1, 2, 3])
>>> idx.view()
Index([1, 2, 3], dtype='int64')

Viewing as 'int32' reinterprets the memory, which may lead to unexpected
behavior:

>>> idx.view("int32")
array([1, 0, 2, 0, 3, 0], dtype=int32)
"""
# we need to see if we are subclassing an
# index type here
if cls is not None and not hasattr(cls, "_typ"):
Expand Down Expand Up @@ -1735,6 +1782,37 @@ def _get_default_index_names(
return names

def _get_names(self) -> tuple[Hashable | None, ...]:
"""
Retrieve the names associated with the object.

This method returns a tuple containing the name of the object.
It's primarily intended for internal use.

Returns
-------
tuple[Hashable | None, ...]
A tuple containing the object's name, or None if the object does not have a
name.

See Also
--------
Index.name : Index name as a string instead of a tuple, or None for MultiIndex.
Series.name : Series name.

Examples
--------
Create an index with a name and retrieve its names:

>>> index = pd.Index([1, 2, 3], name="example_name")
>>> index.names
('example_name',)

If the index does not have a name set:

>>> index = pd.Index([1, 2, 3])
>>> index.names
(None,)
"""
return (self.name,)

def _set_names(self, values, *, level=None) -> None:
Expand Down