Skip to content

Commit 64e605f

Browse files
DeaMariaLeonim-vinicius
authored and
im-vinicius
committed
DOC: Fixing EX01 - Added examples (pandas-dev#53906)
* Examples Resampler.__iter__, groups, indices, get_group, ffill * Improved explanation for ffill
1 parent 1c06cb8 commit 64e605f

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

ci/code_checks.sh

-7
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
106106
pandas.test \
107107
pandas.NaT \
108108
pandas.io.formats.style.Styler.to_html \
109-
pandas.HDFStore.groups \
110-
pandas.HDFStore.walk \
111109
pandas.read_feather \
112110
pandas.DataFrame.to_feather \
113111
pandas.read_parquet \
@@ -120,11 +118,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
120118
pandas.io.stata.StataReader.value_labels \
121119
pandas.io.stata.StataReader.variable_labels \
122120
pandas.io.stata.StataWriter.write_file \
123-
pandas.core.resample.Resampler.__iter__ \
124-
pandas.core.resample.Resampler.groups \
125-
pandas.core.resample.Resampler.indices \
126-
pandas.core.resample.Resampler.get_group \
127-
pandas.core.resample.Resampler.ffill \
128121
pandas.core.resample.Resampler.asfreq \
129122
pandas.core.resample.Resampler.count \
130123
pandas.core.resample.Resampler.nunique \

pandas/core/groupby/groupby.py

+63
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,19 @@ def groups(self) -> dict[Hashable, np.ndarray]:
753753
2 7 8 9
754754
>>> df.groupby(by=["a"]).groups
755755
{1: [0, 1], 7: [2]}
756+
757+
For Resampler:
758+
759+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
760+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
761+
>>> ser
762+
2023-01-01 1
763+
2023-01-15 2
764+
2023-02-01 3
765+
2023-02-15 4
766+
dtype: int64
767+
>>> ser.resample('M').groups
768+
{Timestamp('2023-01-31 00:00:00'): 2, Timestamp('2023-02-28 00:00:00'): 4}
756769
"""
757770
return self.grouper.groups
758771

@@ -794,6 +807,20 @@ def indices(self) -> dict[Hashable, npt.NDArray[np.intp]]:
794807
eagle 7 8 9
795808
>>> df.groupby(by=["a"]).indices
796809
{1: array([0, 1]), 7: array([2])}
810+
811+
For Resampler:
812+
813+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
814+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
815+
>>> ser
816+
2023-01-01 1
817+
2023-01-15 2
818+
2023-02-01 3
819+
2023-02-15 4
820+
dtype: int64
821+
>>> ser.resample('M').indices
822+
defaultdict(<class 'list'>, {Timestamp('2023-01-31 00:00:00'): [0, 1],
823+
Timestamp('2023-02-28 00:00:00'): [2, 3]})
797824
"""
798825
return self.grouper.indices
799826

@@ -965,6 +992,21 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
965992
a b c
966993
owl 1 2 3
967994
toucan 1 5 6
995+
996+
For Resampler:
997+
998+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
999+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
1000+
>>> ser
1001+
2023-01-01 1
1002+
2023-01-15 2
1003+
2023-02-01 3
1004+
2023-02-15 4
1005+
dtype: int64
1006+
>>> ser.resample('M').get_group('2023-01-31')
1007+
2023-01-01 1
1008+
2023-01-15 2
1009+
dtype: int64
9681010
"""
9691011
inds = self._get_index(name)
9701012
if not len(inds):
@@ -1032,6 +1074,27 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
10321074
(7,)
10331075
a b c
10341076
2 7 8 9
1077+
1078+
For Resampler:
1079+
1080+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
1081+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
1082+
>>> ser
1083+
2023-01-01 1
1084+
2023-01-15 2
1085+
2023-02-01 3
1086+
2023-02-15 4
1087+
dtype: int64
1088+
>>> for x, y in ser.resample('M'):
1089+
... print(f'{x}\\n{y}\\n')
1090+
2023-01-31 00:00:00
1091+
2023-01-01 1
1092+
2023-01-15 2
1093+
dtype: int64
1094+
2023-02-28 00:00:00
1095+
2023-02-01 3
1096+
2023-02-15 4
1097+
dtype: int64
10351098
"""
10361099
keys = self.keys
10371100
level = self.level

pandas/core/resample.py

+48
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,54 @@ def ffill(self, limit: int | None = None):
503503
--------
504504
Series.fillna: Fill NA/NaN values using the specified method.
505505
DataFrame.fillna: Fill NA/NaN values using the specified method.
506+
507+
Examples
508+
--------
509+
Here we only create a ``Series``.
510+
511+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
512+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
513+
>>> ser
514+
2023-01-01 1
515+
2023-01-15 2
516+
2023-02-01 3
517+
2023-02-15 4
518+
dtype: int64
519+
520+
Example for ``ffill`` with downsampling (we have fewer dates after resampling):
521+
522+
>>> ser.resample('M').ffill()
523+
2023-01-31 2
524+
2023-02-28 4
525+
Freq: M, dtype: int64
526+
527+
Example for ``ffill`` with upsampling (fill the new dates with
528+
the previous value):
529+
530+
>>> ser.resample('W').ffill()
531+
2023-01-01 1
532+
2023-01-08 1
533+
2023-01-15 2
534+
2023-01-22 2
535+
2023-01-29 2
536+
2023-02-05 3
537+
2023-02-12 3
538+
2023-02-19 4
539+
Freq: W-SUN, dtype: int64
540+
541+
With upsampling and limiting (only fill the first new date with the
542+
previous value):
543+
544+
>>> ser.resample('W').ffill(limit=1)
545+
2023-01-01 1.0
546+
2023-01-08 1.0
547+
2023-01-15 2.0
548+
2023-01-22 2.0
549+
2023-01-29 NaN
550+
2023-02-05 3.0
551+
2023-02-12 NaN
552+
2023-02-19 4.0
553+
Freq: W-SUN, dtype: float64
506554
"""
507555
return self._upsample("ffill", limit=limit)
508556

0 commit comments

Comments
 (0)