Skip to content

Commit e21dc66

Browse files
DeaMariaLeonim-vinicius
authored and
im-vinicius
committed
DOC: Fixing EX01 - Added examples (pandas-dev#53526)
Added examples
1 parent f76ef0d commit e21dc66

File tree

6 files changed

+100
-12
lines changed

6 files changed

+100
-12
lines changed

ci/code_checks.sh

-9
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
8282
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX01 --ignore_functions \
8383
pandas.Series.backfill \
8484
pandas.Series.pad \
85-
pandas.Series.sparse \
8685
pandas.DataFrame.sparse \
87-
pandas.Series.cat.codes \
88-
pandas.Series.cat.reorder_categories \
89-
pandas.Series.cat.set_categories \
90-
pandas.Series.cat.as_ordered \
91-
pandas.Series.cat.as_unordered \
92-
pandas.Series.sparse.fill_value \
93-
pandas.Flags \
9486
pandas.Series.attrs \
9587
pandas.Series.plot \
9688
pandas.Series.hist \
@@ -242,7 +234,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
242234
pandas.IntervalIndex.to_tuples \
243235
pandas.MultiIndex.dtypes \
244236
pandas.MultiIndex.drop \
245-
pandas.DatetimeIndex.indexer_between_time \
246237
pandas.DatetimeIndex.snap \
247238
pandas.DatetimeIndex.as_unit \
248239
pandas.DatetimeIndex.to_pydatetime \

pandas/core/arrays/categorical.py

+69
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,15 @@ def as_ordered(self) -> Self:
875875
-------
876876
Categorical
877877
Ordered Categorical.
878+
879+
Examples
880+
--------
881+
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
882+
>>> ser.cat.ordered
883+
False
884+
>>> ser = ser.cat.as_ordered()
885+
>>> ser.cat.ordered
886+
True
878887
"""
879888
return self.set_ordered(True)
880889

@@ -886,6 +895,15 @@ def as_unordered(self) -> Self:
886895
-------
887896
Categorical
888897
Unordered Categorical.
898+
899+
Examples
900+
--------
901+
>>> raw_cate = pd.Categorical(["a", "b", "c"],
902+
... categories=["a", "b", "c"], ordered=True)
903+
>>> ser = pd.Series(raw_cate)
904+
>>> ser = ser.cat.as_unordered()
905+
>>> ser.cat.ordered
906+
False
889907
"""
890908
return self.set_ordered(False)
891909

@@ -936,6 +954,26 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False):
936954
add_categories : Add new categories.
937955
remove_categories : Remove the specified categories.
938956
remove_unused_categories : Remove categories which are not used.
957+
958+
Examples
959+
--------
960+
>>> raw_cate = pd.Categorical(["a", "b", "c", "A"],
961+
... categories=["a", "b", "c"], ordered=True)
962+
>>> ser = pd.Series(raw_cate)
963+
>>> ser
964+
0 a
965+
1 b
966+
2 c
967+
3 NaN
968+
dtype: category
969+
Categories (3, object): ['a' < 'b' < 'c']
970+
>>> ser.cat.set_categories(["A", "B", "C"], rename=True)
971+
0 A
972+
1 B
973+
2 C
974+
3 NaN
975+
dtype: category
976+
Categories (3, object): ['A' < 'B' < 'C']
939977
"""
940978

941979
if ordered is None:
@@ -1062,6 +1100,26 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
10621100
remove_categories : Remove the specified categories.
10631101
remove_unused_categories : Remove categories which are not used.
10641102
set_categories : Set the categories to the specified ones.
1103+
1104+
Examples
1105+
--------
1106+
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
1107+
>>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True)
1108+
>>> ser
1109+
0 a
1110+
1 b
1111+
2 c
1112+
3 a
1113+
dtype: category
1114+
Categories (3, object): ['c' < 'b' < 'a']
1115+
>>> ser = ser.sort_values()
1116+
>>> ser
1117+
2 c
1118+
1 b
1119+
0 a
1120+
3 a
1121+
dtype: category
1122+
Categories (3, object): ['c' < 'b' < 'a']
10651123
"""
10661124
if (
10671125
len(self.categories) != len(new_categories)
@@ -2663,6 +2721,17 @@ def _delegate_property_set(self, name: str, new_values): # type: ignore[overrid
26632721
def codes(self) -> Series:
26642722
"""
26652723
Return Series of codes as well as the index.
2724+
2725+
Examples
2726+
--------
2727+
>>> raw_cate = pd.Categorical(["a", "b", "c", "a"], categories=["a", "b"])
2728+
>>> ser = pd.Series(raw_cate)
2729+
>>> ser.cat.codes
2730+
0 0
2731+
1 1
2732+
2 -1
2733+
3 0
2734+
dtype: int8
26662735
"""
26672736
from pandas import Series
26682737

pandas/core/arrays/sparse/accessor.py

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def _validate(self, data):
4040
class SparseAccessor(BaseAccessor, PandasDelegate):
4141
"""
4242
Accessor for SparseSparse from other sparse matrix data types.
43+
44+
Examples
45+
--------
46+
>>> ser = pd.Series([0, 0, 2, 2, 2], dtype="Sparse[int]")
47+
>>> ser.sparse.density
48+
0.6
49+
>>> ser.sparse.sp_values
50+
array([2, 2, 2])
4351
"""
4452

4553
def _validate(self, data):

pandas/core/arrays/sparse/array.py

+10
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,16 @@ def fill_value(self):
629629
Elements in `data` that are `fill_value` are not stored.
630630
631631
For memory savings, this should be the most common value in the array.
632+
633+
Examples
634+
--------
635+
>>> ser = pd.Series([0, 0, 2, 2, 2], dtype="Sparse[int]")
636+
>>> ser.sparse.fill_value
637+
0
638+
>>> spa_dtype = pd.SparseDtype(dtype=np.int32, fill_value=2)
639+
>>> ser = pd.Series([0, 0, 2, 2, 2], dtype=spa_dtype)
640+
>>> ser.sparse.fill_value
641+
2
632642
"""
633643
return self.dtype.fill_value
634644

pandas/core/flags.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class Flags:
3232
it is expected that every method taking or returning one or more
3333
DataFrame or Series objects will propagate ``allows_duplicate_labels``.
3434
35-
Notes
36-
-----
37-
Attributes can be set in two ways
35+
Examples
36+
--------
37+
Attributes can be set in two ways:
3838
3939
>>> df = pd.DataFrame()
4040
>>> df.flags

pandas/core/indexes/datetimes.py

+10
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,16 @@ def indexer_between_time(
764764
--------
765765
indexer_at_time : Get index locations of values at particular time of day.
766766
DataFrame.between_time : Select values between particular times of day.
767+
768+
Examples
769+
--------
770+
>>> idx = pd.date_range("2023-01-01", periods=4, freq="H")
771+
>>> idx
772+
DatetimeIndex(['2023-01-01 00:00:00', '2023-01-01 01:00:00',
773+
'2023-01-01 02:00:00', '2023-01-01 03:00:00'],
774+
dtype='datetime64[ns]', freq='H')
775+
>>> idx.indexer_between_time("00:00", "2:00", include_end=False)
776+
array([0, 1])
767777
"""
768778
start_time = to_time(start_time)
769779
end_time = to_time(end_time)

0 commit comments

Comments
 (0)