From 8c4759b084be45ff4582db2563c649ae8e6f93b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Thu, 1 Jun 2023 12:10:49 +0200 Subject: [PATCH 1/3] Added examples Series.decode --- pandas/core/generic.py | 10 +++++++++ pandas/core/strings/accessor.py | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bcfbfa1a2b713..0159844efac8f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7167,6 +7167,16 @@ def ffill( ------- {klass} or None Object with missing values filled or None if ``inplace=True``. + + Examples + -------- + >>> ser = pd.Series([1, np.NaN, 2, 3]) + >>> ser.ffill() + 0 1.0 + 1 1.0 + 2 2.0 + 3 3.0 + dtype: float64 """ return self.fillna( method="ffill", axis=axis, inplace=inplace, limit=limit, downcast=downcast diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index c8430b832f782..0477345b959e4 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1616,6 +1616,35 @@ def pad( Returns ------- Series/Index of objects. + + Examples + -------- + For Series.str.center: + + >>> ser = pd.Series(['dog', 'bird', 'mouse']) + >>> ser.str.center(8, fillchar='.') + 0 ..dog... + 1 ..bird.. + 2 .mouse.. + dtype: object + + For Series.str.ljust: + + >>> ser = pd.Series(['dog', 'bird', 'mouse']) + >>> ser.str.ljust(8, fillchar='.') + 0 dog..... + 1 bird.... + 2 mouse... + dtype: object + + For Series.str.rjust: + + >>> ser = pd.Series(['dog', 'bird', 'mouse']) + >>> ser.str.rjust(8, fillchar='.') + 0 .....dog + 1 ....bird + 2 ...mouse + dtype: object """ @Appender(_shared_docs["str_pad"] % {"side": "left and right", "method": "center"}) @@ -1867,6 +1896,16 @@ def decode(self, encoding, errors: str = "strict"): Returns ------- Series or Index + + Examples + -------- + For Series: + + >>> ser = pd.Series([b'Hello', b'123']) + >>> ser.str.decode('ascii') + 0 Hello + 1 123 + dtype: object """ # TODO: Add a similar _bytes interface. if encoding in _cpython_optimized_decoders: From 3cf98710bb57cb8beea8ba2e1c1e2d5c494dbca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Thu, 1 Jun 2023 18:30:51 +0200 Subject: [PATCH 2/3] Added more examples --- pandas/core/strings/accessor.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 0477345b959e4..5dd90640fbb28 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1901,10 +1901,11 @@ def decode(self, encoding, errors: str = "strict"): -------- For Series: - >>> ser = pd.Series([b'Hello', b'123']) + >>> ser = pd.Series([b'cow', b'123', b'()']) >>> ser.str.decode('ascii') - 0 Hello + 0 cow 1 123 + 2 () dtype: object """ # TODO: Add a similar _bytes interface. @@ -1934,6 +1935,15 @@ def encode(self, encoding, errors: str = "strict"): Returns ------- Series/Index of objects + + Examples + -------- + >>> ser = pd.Series(['cow', '123', '()']) + >>> ser.str.encode(encoding='ascii') + 0 b'cow' + 1 b'123' + 2 b'()' + dtype: object """ result = self._data.array._str_encode(encoding, errors) return self._wrap_result(result, returns_string=False) @@ -2769,6 +2779,15 @@ def extractall(self, pat, flags: int = 0) -> DataFrame: See Also -------- %(also)s + + Examples + -------- + >>> ser = pd.Series(["cow_", "duck_", "do_ve"]) + >>> ser.str.find("_") + 0 3 + 1 4 + 2 2 + dtype: int64 """ @Appender( From a13125693d587fe09d0c4b9998f0b2e5f1ecf4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Fri, 2 Jun 2023 10:09:10 +0200 Subject: [PATCH 3/3] Added Series.str examples --- ci/code_checks.sh | 10 ---------- pandas/core/strings/accessor.py | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index e095d97146d03..4915997277006 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -81,20 +81,10 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then MSG='Partially validate docstrings (EX01)' ; echo $MSG $BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX01 --ignore_functions \ pandas.Series.backfill \ - pandas.Series.ffill \ pandas.Series.pad \ - pandas.Series.str.center \ - pandas.Series.str.decode \ - pandas.Series.str.encode \ - pandas.Series.str.find \ - pandas.Series.str.fullmatch \ - pandas.Series.str.index \ - pandas.Series.str.ljust \ - pandas.Series.str.match \ pandas.Series.str.normalize \ pandas.Series.str.rfind \ pandas.Series.str.rindex \ - pandas.Series.str.rjust \ pandas.Series.str.translate \ pandas.Series.sparse \ pandas.DataFrame.sparse \ diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 5dd90640fbb28..e8769370ca88d 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -1293,6 +1293,15 @@ def match(self, pat, case: bool = True, flags: int = 0, na=None): contains : Analogous, but less strict, relying on re.search instead of re.match. extract : Extract matched groups. + + Examples + -------- + >>> ser = pd.Series(["horse", "eagle", "donkey"]) + >>> ser.str.match("e") + 0 False + 1 True + 2 False + dtype: bool """ result = self._data.array._str_match(pat, case=case, flags=flags, na=na) return self._wrap_result(result, fill_value=na, returns_string=False) @@ -1324,6 +1333,15 @@ def fullmatch(self, pat, case: bool = True, flags: int = 0, na=None): match : Similar, but also returns `True` when only a *prefix* of the string matches the regular expression. extract : Extract matched groups. + + Examples + -------- + >>> ser = pd.Series(["cat", "duck", "dove"]) + >>> ser.str.fullmatch(r'd.+') + 0 False + 1 True + 2 True + dtype: bool """ result = self._data.array._str_fullmatch(pat, case=case, flags=flags, na=na) return self._wrap_result(result, fill_value=na, returns_string=False) @@ -2871,6 +2889,15 @@ def normalize(self, form): See Also -------- %(also)s + + Examples + -------- + >>> ser = pd.Series(["horse", "eagle", "donkey"]) + >>> ser.str.index("e") + 0 4 + 1 0 + 2 4 + dtype: int64 """ @Appender(