Skip to content

Commit 33298bf

Browse files
committed
DOC Added Index examples
Index.drop, identical, insert DOC: Fixing EX01 - Added examples (pandas-dev#53336) * Examples * Changed Index.copy * Corrected Float32Dtype & Float64Dtype Added is_, take, putmask, unique
1 parent 29f9499 commit 33298bf

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

ci/code_checks.sh

-14
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
170170
pandas.Period.asfreq \
171171
pandas.Period.now \
172172
pandas.arrays.PeriodArray \
173-
pandas.arrays.IntervalArray.from_arrays \
174-
pandas.arrays.IntervalArray.to_tuples \
175173
pandas.Int8Dtype \
176174
pandas.Int16Dtype \
177175
pandas.Int32Dtype \
@@ -181,8 +179,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
181179
pandas.UInt32Dtype \
182180
pandas.UInt64Dtype \
183181
pandas.NA \
184-
pandas.Float32Dtype \
185-
pandas.Float64Dtype \
186182
pandas.CategoricalDtype.categories \
187183
pandas.CategoricalDtype.ordered \
188184
pandas.Categorical.dtype \
@@ -258,16 +254,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
258254
pandas.util.hash_pandas_object \
259255
pandas_object \
260256
pandas.api.interchange.from_dataframe \
261-
pandas.Index.T \
262-
pandas.Index.memory_usage \
263-
pandas.Index.copy \
264-
pandas.Index.drop \
265-
pandas.Index.identical \
266-
pandas.Index.insert \
267-
pandas.Index.is_ \
268-
pandas.Index.take \
269-
pandas.Index.putmask \
270-
pandas.Index.unique \
271257
pandas.Index.fillna \
272258
pandas.Index.dropna \
273259
pandas.Index.astype \

pandas/core/arrays/floating.py

+14
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ class FloatingArray(NumericArray):
134134
Methods
135135
-------
136136
None
137+
138+
Examples
139+
--------
140+
For Float32Dtype:
141+
142+
>>> ser = pd.Series([2.25, pd.NA], dtype=pd.Float32Dtype())
143+
>>> ser.dtype
144+
Float32Dtype()
145+
146+
For Float64Dtype:
147+
148+
>>> ser = pd.Series([2.25, pd.NA], dtype=pd.Float64Dtype())
149+
>>> ser.dtype
150+
Float64Dtype()
137151
"""
138152

139153
# create the Dtype

pandas/core/arrays/interval.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ def from_breaks(
509509
"name": "",
510510
"examples": textwrap.dedent(
511511
"""\
512+
Examples
513+
--------
512514
>>> pd.arrays.IntervalArray.from_arrays([0, 1, 2], [1, 2, 3])
513515
<IntervalArray>
514516
[(0, 1], (1, 2], (2, 3]]
@@ -1635,9 +1637,8 @@ def __arrow_array__(self, type=None):
16351637

16361638
return pyarrow.ExtensionArray.from_storage(interval_type, storage_array)
16371639

1638-
_interval_shared_docs[
1639-
"to_tuples"
1640-
] = """
1640+
_interval_shared_docs["to_tuples"] = textwrap.dedent(
1641+
"""
16411642
Return an %(return_type)s of tuples of the form (left, right).
16421643
16431644
Parameters
@@ -1651,9 +1652,27 @@ def __arrow_array__(self, type=None):
16511652
tuples: %(return_type)s
16521653
%(examples)s\
16531654
"""
1655+
)
16541656

16551657
@Appender(
1656-
_interval_shared_docs["to_tuples"] % {"return_type": "ndarray", "examples": ""}
1658+
_interval_shared_docs["to_tuples"]
1659+
% {
1660+
"return_type": "ndarray",
1661+
"examples": textwrap.dedent(
1662+
"""\
1663+
1664+
Examples
1665+
--------
1666+
>>> idx = pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)])
1667+
>>> idx
1668+
<IntervalArray>
1669+
[(0, 1], (1, 2]]
1670+
Length: 2, dtype: interval[int64, right]
1671+
>>> idx.to_tuples()
1672+
array([(0, 1), (1, 2)], dtype=object)
1673+
"""
1674+
),
1675+
}
16571676
)
16581677
def to_tuples(self, na_tuple: bool = True) -> np.ndarray:
16591678
tuples = com.asarray_tuplesafe(zip(self._left, self._right))

pandas/core/base.py

+14
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ def transpose(self, *args, **kwargs) -> Self:
300300
301301
Examples
302302
--------
303+
For Series:
304+
303305
>>> s = pd.Series(['Ant', 'Bear', 'Cow'])
304306
>>> s
305307
0 Ant
@@ -311,6 +313,12 @@ def transpose(self, *args, **kwargs) -> Self:
311313
1 Bear
312314
2 Cow
313315
dtype: object
316+
317+
For Index:
318+
319+
>>> idx = pd.Index([1, 2, 3])
320+
>>> idx.T
321+
Index([1, 2, 3], dtype='int64')
314322
""",
315323
)
316324

@@ -1088,6 +1096,12 @@ def _memory_usage(self, deep: bool = False) -> int:
10881096
-----
10891097
Memory usage does not include memory consumed by elements that
10901098
are not components of the array if deep=False or if used on PyPy
1099+
1100+
Examples
1101+
--------
1102+
>>> idx = pd.Index([1, 2, 3])
1103+
>>> idx.memory_usage()
1104+
24
10911105
"""
10921106
if hasattr(self.array, "memory_usage"):
10931107
return self.array.memory_usage( # pyright: ignore[reportGeneralTypeIssues]

pandas/core/indexes/base.py

+62
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,18 @@ def is_(self, other) -> bool:
799799
See Also
800800
--------
801801
Index.identical : Works like ``Index.is_`` but also checks metadata.
802+
803+
Examples
804+
--------
805+
>>> idx1 = pd.Index(['1', '2', '3'])
806+
>>> idx2 = pd.Index(['1', '2', '3'])
807+
>>> idx2.is_(idx1)
808+
False
809+
810+
>>> idx1 = pd.Index(['1', '2', '3'])
811+
>>> new_name = idx1
812+
>>> new_name.is_(idx1)
813+
True
802814
"""
803815
if self is other:
804816
return True
@@ -1089,6 +1101,12 @@ def astype(self, dtype, copy: bool = True):
10891101
--------
10901102
numpy.ndarray.take: Return an array formed from the
10911103
elements of a at the given indices.
1104+
1105+
Examples
1106+
--------
1107+
>>> idx = pd.Index(['a', 'b', 'c'])
1108+
>>> idx.take([2])
1109+
Index(['c'], dtype='object')
10921110
"""
10931111

10941112
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
@@ -1221,6 +1239,13 @@ def copy(
12211239
-----
12221240
In most cases, there should be no functional difference from using
12231241
``deep``, but if ``deep`` is passed it will attempt to deepcopy.
1242+
1243+
Examples
1244+
--------
1245+
>>> idx = pd.Index(['a', 'b', 'c'])
1246+
>>> new_idx = idx.copy()
1247+
>>> idx is new_idx
1248+
False
12241249
"""
12251250

12261251
name = self._validate_names(name=name, deep=deep)[0]
@@ -2959,6 +2984,12 @@ def unique(self, level: Hashable | None = None) -> Self:
29592984
--------
29602985
unique : Numpy array of unique values in that column.
29612986
Series.unique : Return unique values of Series object.
2987+
2988+
Examples
2989+
--------
2990+
>>> idx = pd.Index([1, 1, 2, 3, 3])
2991+
>>> idx.unique()
2992+
Index([1, 2, 3], dtype='int64')
29622993
"""
29632994
if level is not None:
29642995
self._validate_index_level(level)
@@ -5338,6 +5369,13 @@ def putmask(self, mask, value) -> Index:
53385369
--------
53395370
numpy.ndarray.putmask : Changes elements of an array
53405371
based on conditional and input values.
5372+
5373+
Examples
5374+
--------
5375+
>>> idx1 = pd.Index([1, 2, 3])
5376+
>>> idx2 = pd.Index([5, 6, 7])
5377+
>>> idx1.putmask([True, False, False], idx2)
5378+
Index([5, 2, 3], dtype='int64')
53415379
"""
53425380
mask, noop = validate_putmask(self._values, mask)
53435381
if noop:
@@ -5467,6 +5505,18 @@ def identical(self, other) -> bool:
54675505
bool
54685506
If two Index objects have equal elements and same type True,
54695507
otherwise False.
5508+
5509+
Examples
5510+
--------
5511+
>>> idx1 = pd.Index(['1', '2', '3'])
5512+
>>> idx2 = pd.Index(['1', '2', '3'])
5513+
>>> idx2.identical(idx1)
5514+
True
5515+
5516+
>>> idx1 = pd.Index(['1', '2', '3'], name="A")
5517+
>>> idx2 = pd.Index(['1', '2', '3'], name="B")
5518+
>>> idx2.identical(idx1)
5519+
False
54705520
"""
54715521
return (
54725522
self.equals(other)
@@ -6687,6 +6737,12 @@ def insert(self, loc: int, item) -> Index:
66876737
Returns
66886738
-------
66896739
Index
6740+
6741+
Examples
6742+
--------
6743+
>>> idx = pd.Index(['a', 'b', 'c'])
6744+
>>> idx.insert(1, 'x')
6745+
Index(['a', 'x', 'b', 'c'], dtype='object')
66906746
"""
66916747
item = lib.item_from_zerodim(item)
66926748
if is_valid_na_for_dtype(item, self.dtype) and self.dtype != object:
@@ -6748,6 +6804,12 @@ def drop(
67486804
------
67496805
KeyError
67506806
If not all of the labels are found in the selected axis
6807+
6808+
Examples
6809+
--------
6810+
>>> idx = pd.Index(['a', 'b', 'c'])
6811+
>>> idx.drop(['a'])
6812+
Index(['b', 'c'], dtype='object')
67516813
"""
67526814
if not isinstance(labels, Index):
67536815
# avoid materializing e.g. RangeIndex

0 commit comments

Comments
 (0)