From 8581f971ded7fe2cf781906fd40a8751ea1ec87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A9rez-Su=C3=A1rez?= Date: Tue, 12 Dec 2017 19:37:26 +0000 Subject: [PATCH 1/5] Add example to `Dataframe.head()` method. --- pandas/core/generic.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index da550dccc9c89..e7e7474195cff 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3563,6 +3563,30 @@ def head(self, n=5): ------- obj_head : type of caller The first n rows of the caller object. + + Examples + -------- + >>> df = pd.DataFrame({ + ... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'], + ... 'col2' : [2, 1, 9, 8, 7, 4], + ... 'col3': [0, 1, 9, 4, 2, 3], + ... }) + >>> df + col1 col2 col3 + 0 A 2 0 + 1 A 1 1 + 2 B 9 9 + 3 NaN 8 4 + 4 D 7 2 + 5 C 4 3 + + Viewing the first n lines + >>> df.head(n=3) + col1 col2 col3 + 0 A 2 0 + 1 A 1 1 + 2 B 9 9 + """ return self.iloc[:n] From f61d73561da209e554c01ef909f4f2211fd1e0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A9rez-Su=C3=A1rez?= Date: Tue, 12 Dec 2017 19:52:43 +0000 Subject: [PATCH 2/5] Adds also the default case --- pandas/core/generic.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e7e7474195cff..7d890407c9885 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3567,9 +3567,9 @@ def head(self, n=5): Examples -------- >>> df = pd.DataFrame({ - ... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'], - ... 'col2' : [2, 1, 9, 8, 7, 4], - ... 'col3': [0, 1, 9, 4, 2, 3], + ... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C', 'E', 'E', 'F'], + ... 'col2' : [2, 1, 9, 8, 7, 4, 0, 2, 2], + ... 'col3': [0, 1, 9, 4, 2, 3, 5, 7, 3], ... }) >>> df col1 col2 col3 @@ -3579,8 +3579,20 @@ def head(self, n=5): 3 NaN 8 4 4 D 7 2 5 C 4 3 + 6 E 0 5 + 7 E 2 7 + 8 F 2 3 - Viewing the first n lines + Viewing the first 5 lines (the default) + >>> df.head() + col1 col2 col3 + 0 A 2 0 + 1 A 1 1 + 2 B 9 9 + 3 NaN 8 4 + 4 D 7 2 + + Viewing the first n lines (three in this case) >>> df.head(n=3) col1 col2 col3 0 A 2 0 From 439f6be025d55ac487b12df399359e272a4a5442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A9rez-Su=C3=A1rez?= Date: Tue, 12 Dec 2017 20:06:13 +0000 Subject: [PATCH 3/5] Fixed comments by @GiantsLoveDeathMetal --- pandas/core/generic.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7d890407c9885..17b31a7464ec3 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3567,8 +3567,8 @@ def head(self, n=5): Examples -------- >>> df = pd.DataFrame({ - ... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C', 'E', 'E', 'F'], - ... 'col2' : [2, 1, 9, 8, 7, 4, 0, 2, 2], + ... 'col1': ['A', 'A', 'B', 'B', 'D', 'C', 'E', 'E', 'F'], + ... 'col2': [2, 1, 9, 8, 7, 4, 0, 2, 2], ... 'col3': [0, 1, 9, 4, 2, 3, 5, 7, 3], ... }) >>> df @@ -3576,12 +3576,12 @@ def head(self, n=5): 0 A 2 0 1 A 1 1 2 B 9 9 - 3 NaN 8 4 + 3 B 8 4 4 D 7 2 5 C 4 3 - 6 E 0 5 - 7 E 2 7 - 8 F 2 3 + 6 E 0 5 + 7 E 2 7 + 8 F 2 3 Viewing the first 5 lines (the default) >>> df.head() @@ -3589,11 +3589,11 @@ def head(self, n=5): 0 A 2 0 1 A 1 1 2 B 9 9 - 3 NaN 8 4 + 3 B 8 4 4 D 7 2 Viewing the first n lines (three in this case) - >>> df.head(n=3) + >>> df.head(3) col1 col2 col3 0 A 2 0 1 A 1 1 From 94f270295325b6b7e7f19aedcbb6bb9f7de8d346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A9rez-Su=C3=A1rez?= Date: Tue, 12 Dec 2017 20:07:56 +0000 Subject: [PATCH 4/5] pep8ify example from sort as in `head` --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 17b31a7464ec3..b3e4a299b581f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2966,8 +2966,8 @@ def add_suffix(self, suffix): Examples -------- >>> df = pd.DataFrame({ - ... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'], - ... 'col2' : [2, 1, 9, 8, 7, 4], + ... 'col1': ['A', 'A', 'B', np.nan, 'D', 'C'], + ... 'col2': [2, 1, 9, 8, 7, 4], ... 'col3': [0, 1, 9, 4, 2, 3], ... }) >>> df From 0ac5fab930373148db405b843ef29a3bdfe80e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A9rez-Su=C3=A1rez?= Date: Tue, 12 Dec 2017 20:10:33 +0000 Subject: [PATCH 5/5] alignments problems to make doctest not failing --- pandas/core/generic.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b3e4a299b581f..071dda090fddd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3579,18 +3579,18 @@ def head(self, n=5): 3 B 8 4 4 D 7 2 5 C 4 3 - 6 E 0 5 - 7 E 2 7 - 8 F 2 3 + 6 E 0 5 + 7 E 2 7 + 8 F 2 3 Viewing the first 5 lines (the default) >>> df.head() - col1 col2 col3 - 0 A 2 0 - 1 A 1 1 - 2 B 9 9 - 3 B 8 4 - 4 D 7 2 + col1 col2 col3 + 0 A 2 0 + 1 A 1 1 + 2 B 9 9 + 3 B 8 4 + 4 D 7 2 Viewing the first n lines (three in this case) >>> df.head(3)