From c68feec320115347cc574e08a6d2ec70f70a77b3 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Sat, 29 Sep 2018 19:14:10 -0700 Subject: [PATCH 1/3] DOC: #22899, Fixed docstring of itertuples in pandas/core/frame.py --- pandas/core/frame.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d5b273f37a3a2..f47eefce3bbab 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -883,8 +883,7 @@ def iterrows(self): def itertuples(self, index=True, name="Pandas"): """ - Iterate over DataFrame rows as namedtuples, with index value as first - element of the tuple. + Iterate over DataFrame rows as namedtuples. Parameters ---------- @@ -894,6 +893,10 @@ def itertuples(self, index=True, name="Pandas"): The name of the returned namedtuples or None to return regular tuples. + Returns + ------- + Returns namedtuples. + Notes ----- The column names will be renamed to positional names if they are @@ -907,9 +910,8 @@ def itertuples(self, index=True, name="Pandas"): Examples -------- - >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]}, - index=['a', 'b']) + ... index=['a', 'b']) >>> df col1 col2 a 1 0.1 @@ -917,9 +919,8 @@ def itertuples(self, index=True, name="Pandas"): >>> for row in df.itertuples(): ... print(row) ... - Pandas(Index='a', col1=1, col2=0.10000000000000001) - Pandas(Index='b', col1=2, col2=0.20000000000000001) - + Pandas(Index='a', col1=1, col2=0.1) + Pandas(Index='b', col1=2, col2=0.2) """ arrays = [] fields = [] From be1bee9ae49a66cdd31e3757f5ca6fc5f4ead59b Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Sun, 30 Sep 2018 09:45:16 -0700 Subject: [PATCH 2/3] DOC: #22899, updated docstring with more examples and fixed Returns to Yielded --- pandas/core/frame.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f47eefce3bbab..6e7b057a4d3bc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -887,15 +887,16 @@ def itertuples(self, index=True, name="Pandas"): Parameters ---------- - index : boolean, default True + index : bool, default True If True, return the index as the first element of the tuple. - name : string, default "Pandas" + name : str, default "Pandas" The name of the returned namedtuples or None to return regular tuples. - Returns + Yields ------- - Returns namedtuples. + collections.namedtuple + Yields namedtuples of corresponding index and column values. Notes ----- @@ -910,17 +911,35 @@ def itertuples(self, index=True, name="Pandas"): Examples -------- - >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]}, - ... index=['a', 'b']) + >>> df = pd.DataFrame({'num_legs': [4,2], 'num_wings': [0,2]}, + ... index=['dog','hawk']) >>> df - col1 col2 - a 1 0.1 - b 2 0.2 + num_legs num_wings + dog 4 0 + hawk 2 2 >>> for row in df.itertuples(): ... print(row) ... - Pandas(Index='a', col1=1, col2=0.1) - Pandas(Index='b', col1=2, col2=0.2) + Pandas(Index='dog', num_legs=4, num_wings=0) + Pandas(Index='hawk', num_legs=2, num_wings=2) + + By setting the `index` parameter to False we can remove the index + as the first element of the tuple: + + >>> for row in df.itertuples(index=False): + ... print(row) + ... + Pandas(num_legs=4, num_wings=0) + Pandas(num_legs=2, num_wings=2) + + With the `name` parameter set we set a custom name for the yielded + namedtuples: + + >>> for row in df.itertuples(name='Animal'): + ... print(row) + ... + Animal(Index='dog', num_legs=4, num_wings=0) + Animal(Index='hawk', num_legs=2, num_wings=2) """ arrays = [] fields = [] From 064cf33ea85d4e4ba467e898f4d2b3b06131b101 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Sun, 30 Sep 2018 10:11:36 -0700 Subject: [PATCH 3/3] DOC: #22899, fix example whitespacing and update formatting in See Also / Yields --- pandas/core/frame.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6e7b057a4d3bc..8c6b216ad196e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -896,7 +896,9 @@ def itertuples(self, index=True, name="Pandas"): Yields ------- collections.namedtuple - Yields namedtuples of corresponding index and column values. + Yields a namedtuple for each row in the DataFrame with the first + field possibly being the index and following fields being the + column values. Notes ----- @@ -904,15 +906,16 @@ def itertuples(self, index=True, name="Pandas"): invalid Python identifiers, repeated, or start with an underscore. With a large number of columns (>255), regular tuples are returned. - See also + See Also -------- - iterrows : Iterate over DataFrame rows as (index, Series) pairs. - iteritems : Iterate over (column name, Series) pairs. + DataFrame.iterrows : Iterate over DataFrame rows as (index, Series) + pairs. + DataFrame.iteritems : Iterate over (column name, Series) pairs. Examples -------- - >>> df = pd.DataFrame({'num_legs': [4,2], 'num_wings': [0,2]}, - ... index=['dog','hawk']) + >>> df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]}, + ... index=['dog', 'hawk']) >>> df num_legs num_wings dog 4 0