From a5fd1c33adeeabc1c4c6ecfb68c18f5959398ba3 Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Wed, 3 Oct 2018 01:28:14 -0700 Subject: [PATCH 1/9] DOC: Fixed the doctsring for _set_axis_name (GH 22895) --- pandas/core/generic.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 393e7caae5fab..de8ed795dd5fa 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1207,31 +1207,35 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): def _set_axis_name(self, name, axis=0, inplace=False): """ - Alter the name or names of the axis. + Alter the label(s) of the axis. Parameters ---------- name : str or list of str - Name for the Index, or list of names for the MultiIndex - axis : int or str - 0 or 'index' for the index; 1 or 'columns' for the columns - inplace : bool - whether to modify `self` directly or return a copy + Labels(s) to set. + axis : int or str, default 0 + The axis to set the label. The value 0 or 'index' specifies index, + and the value 1 or 'columns' specifies columns. + inplace : bool, default False + Whether to modify `self` directly or return a copy. .. versionadded:: 0.21.0 Returns ------- - renamed : same type as caller or None if inplace=True + Series, DataFrame, Panel, or None + The same type as the caller or `None` if `inplace` is `True`. See Also -------- - pandas.DataFrame.rename - pandas.Series.rename - pandas.Index.rename + pandas.DataFrame.rename : Alter the labels of :class:`DataFrame`. + pandas.Series.rename : Alter the name or the labels :class:`Series`. + pandas.Index.rename : Alter the name of :class:`Index` + or :class:`MultiIndex`. Examples -------- + >>> df = pd.DataFrame({"A": [1, 2, 3]}) >>> df._set_axis_name("foo") A foo From 5dbbbd1c71d1074dab641d7913a55e925b3f9f34 Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Thu, 4 Oct 2018 20:14:22 -0700 Subject: [PATCH 2/9] Added a new line after the first triple quotes --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index de8ed795dd5fa..c96c6c3df9834 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1207,6 +1207,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): def _set_axis_name(self, name, axis=0, inplace=False): """ + Alter the label(s) of the axis. Parameters From bc865d478ee9b0819e9fbb6db050dc8593757f8a Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Thu, 4 Oct 2018 20:14:43 -0700 Subject: [PATCH 3/9] Removed the new line after the first triple quotes --- pandas/core/generic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c96c6c3df9834..de8ed795dd5fa 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1207,7 +1207,6 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): def _set_axis_name(self, name, axis=0, inplace=False): """ - Alter the label(s) of the axis. Parameters From 067fa8d31842d5f3e899855e59f98055ecdd63ba Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Mon, 8 Oct 2018 21:07:27 -0700 Subject: [PATCH 4/9] Modified the description of parameters. Changed the example to animals. --- pandas/core/generic.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5f776173059d1..e1bb89412c51f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1200,11 +1200,11 @@ def _set_axis_name(self, name, axis=0, inplace=False): ---------- name : str or list of str Labels(s) to set. - axis : int or str, default 0 + axis : {0 or 'index', 1 or 'columns'}, default 0 The axis to set the label. The value 0 or 'index' specifies index, and the value 1 or 'columns' specifies columns. - inplace : bool, default False - Whether to modify `self` directly or return a copy. + inplace : boolean, default False + If `True`, do operation inplace and return None. .. versionadded:: 0.21.0 @@ -1222,20 +1222,24 @@ def _set_axis_name(self, name, axis=0, inplace=False): Examples -------- - >>> df = pd.DataFrame({"A": [1, 2, 3]}) - >>> df._set_axis_name("foo") - A - foo - 0 1 - 1 2 - 2 3 - >>> df.index = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c']]) - >>> df._set_axis_name(["bar", "baz"]) - A - bar baz - A a 1 - b 2 - c 3 + >>> df = pd.DataFrame({"legs": [4, 4, 2]}) + legs + 0 4 + 1 4 + 2 2 + >>> df._set_axis_name("animal") + legs + animal + 0 4 + 1 2 + 2 0 + >>> df.index = pd.MultiIndex.from_product([["mammal"], ['dog', 'cat', 'monkey']]) + >>> df._set_axis_name(["type", "name"]) + legs + type name + mammal dog 4 + cat 4 + monkey 2 """ axis = self._get_axis_number(axis) idx = self._get_axis(axis).set_names(name) From 71f39094295102efb45b0becc90175fddb67830c Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Mon, 8 Oct 2018 22:20:40 -0700 Subject: [PATCH 5/9] Modified the example for DataFrame.rename_axis() --- pandas/core/generic.py | 43 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e1bb89412c51f..5e3e523b6c2be 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1154,29 +1154,32 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): -------- **Series** - >>> s = pd.Series([1, 2, 3]) - >>> s.rename_axis("foo") - foo - 0 1 - 1 2 - 2 3 - dtype: int64 + >>> s = pd.Series(["dog", "cat", "monkey"]) + >>> s.rename_axis("animal") + animal + 0 dog + 1 cat + 2 monkey + dtype: object **DataFrame** - >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) - >>> df.rename_axis("foo") - A B - foo - 0 1 4 - 1 2 5 - 2 3 6 - - >>> df.rename_axis("bar", axis="columns") - bar A B - 0 1 4 - 1 2 5 - 2 3 6 + >>> df = pd.DataFrame({"name": ["dog", "cat", "monkey"], "legs": [4, 4, 2]}) + name legs + 0 dog 4 + 1 cat 4 + 2 monkey 2 + >>> df.rename_axis("animal") + name legs + animal + 0 dog 4 + 1 cat 4 + 2 monkey 2 + >>> df.rename_axis("id", axis="columns") + id name legs + 0 dog 4 + 1 cat 4 + 2 monkey 2 """ inplace = validate_bool_kwarg(inplace, 'inplace') non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not From a7eb3d8121a32119f2b0974e8b1f9234d1c6206f Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Tue, 9 Oct 2018 01:33:33 -0700 Subject: [PATCH 6/9] Changed the example so that it makes sense --- pandas/core/generic.py | 52 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5e3e523b6c2be..edfc9f4f1fe36 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1164,22 +1164,24 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): **DataFrame** - >>> df = pd.DataFrame({"name": ["dog", "cat", "monkey"], "legs": [4, 4, 2]}) - name legs - 0 dog 4 - 1 cat 4 - 2 monkey 2 + >>> df = pd.DataFrame({"num_legs": [4, 4, 2], + ... "num_arms": [0, 0, 2]}, + ... ["dog", "cat", "monkey"]) + num_legs num_arms + dog 4 0 + cat 4 0 + monkey 2 2 >>> df.rename_axis("animal") - name legs + num_legs num_arms animal - 0 dog 4 - 1 cat 4 - 2 monkey 2 - >>> df.rename_axis("id", axis="columns") - id name legs - 0 dog 4 - 1 cat 4 - 2 monkey 2 + dog 4 0 + cat 4 0 + monkey 2 2 + >>> df.rename_axis("limbs", axis="columns") + limbs num_legs num_arms + dog 4 0 + cat 4 0 + monkey 2 2 """ inplace = validate_bool_kwarg(inplace, 'inplace') non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not @@ -1225,18 +1227,20 @@ def _set_axis_name(self, name, axis=0, inplace=False): Examples -------- - >>> df = pd.DataFrame({"legs": [4, 4, 2]}) - legs - 0 4 - 1 4 - 2 2 + >>> df = pd.DataFrame({"num_legs": [4, 4, 2]}, + ... ["dog", "cat", "monkey"]) + num_legs + dog 4 + cat 4 + monkey 2 >>> df._set_axis_name("animal") - legs + num_legs animal - 0 4 - 1 2 - 2 0 - >>> df.index = pd.MultiIndex.from_product([["mammal"], ['dog', 'cat', 'monkey']]) + dog 4 + cat 4 + monkey 2 + >>> df.index = pd.MultiIndex.from_product( + ... [["mammal"], ['dog', 'cat', 'monkey']]) >>> df._set_axis_name(["type", "name"]) legs type name From 96a5dd11957400bc76788d668e11cbe2f7dc67ae Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Tue, 9 Oct 2018 23:53:30 -0700 Subject: [PATCH 7/9] Fixed the indentation and changed the word "alter" to "set" where appropriate. --- pandas/core/generic.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index edfc9f4f1fe36..b83ee5795377c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1118,7 +1118,7 @@ def f(x): def rename_axis(self, mapper, axis=0, copy=True, inplace=False): """ - Alter the name of the index or columns. + Set the name of the index or columns. Parameters ---------- @@ -1155,6 +1155,11 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): **Series** >>> s = pd.Series(["dog", "cat", "monkey"]) + >>> s + 0 dog + 1 cat + 2 monkey + dtype: object >>> s.rename_axis("animal") animal 0 dog @@ -1165,8 +1170,8 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): **DataFrame** >>> df = pd.DataFrame({"num_legs": [4, 4, 2], - ... "num_arms": [0, 0, 2]}, - ... ["dog", "cat", "monkey"]) + ... "num_arms": [0, 0, 2]}, + ... ["dog", "cat", "monkey"]) num_legs num_arms dog 4 0 cat 4 0 @@ -1199,12 +1204,12 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): def _set_axis_name(self, name, axis=0, inplace=False): """ - Alter the label(s) of the axis. + Set the name(s) of the axis. Parameters ---------- name : str or list of str - Labels(s) to set. + Name(s) to set. axis : {0 or 'index', 1 or 'columns'}, default 0 The axis to set the label. The value 0 or 'index' specifies index, and the value 1 or 'columns' specifies columns. @@ -1215,20 +1220,22 @@ def _set_axis_name(self, name, axis=0, inplace=False): Returns ------- - Series, DataFrame, Panel, or None + Series, DataFrame, or None The same type as the caller or `None` if `inplace` is `True`. See Also -------- - pandas.DataFrame.rename : Alter the labels of :class:`DataFrame`. - pandas.Series.rename : Alter the name or the labels :class:`Series`. - pandas.Index.rename : Alter the name of :class:`Index` + pandas.DataFrame.rename : Alter the axis labels of :class:`DataFrame`. + pandas.Series.rename : Alter the index labels or set the index name + of :class:`Series`. + pandas.Index.rename : Set the name of :class:`Index` or :class:`MultiIndex`. Examples -------- >>> df = pd.DataFrame({"num_legs": [4, 4, 2]}, ... ["dog", "cat", "monkey"]) + >>> df num_legs dog 4 cat 4 @@ -1240,7 +1247,7 @@ def _set_axis_name(self, name, axis=0, inplace=False): cat 4 monkey 2 >>> df.index = pd.MultiIndex.from_product( - ... [["mammal"], ['dog', 'cat', 'monkey']]) + ... [["mammal"], ['dog', 'cat', 'monkey']]) >>> df._set_axis_name(["type", "name"]) legs type name @@ -1248,6 +1255,7 @@ def _set_axis_name(self, name, axis=0, inplace=False): cat 4 monkey 2 """ + pd.MultiIndex.from_product([["mammal"], ['dog', 'cat', 'monkey']]) axis = self._get_axis_number(axis) idx = self._get_axis(axis).set_names(name) From b030a6b7f71b64e8d0171f64ddc00112f73256ee Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Wed, 10 Oct 2018 20:45:54 -0700 Subject: [PATCH 8/9] Fixed failing docstring test --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b83ee5795377c..ce532c77b6908 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1172,6 +1172,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): >>> df = pd.DataFrame({"num_legs": [4, 4, 2], ... "num_arms": [0, 0, 2]}, ... ["dog", "cat", "monkey"]) + >>> df num_legs num_arms dog 4 0 cat 4 0 From adc26949618954abc0695f681682ef7a9d80a67e Mon Sep 17 00:00:00 2001 From: Brian Choi Date: Thu, 11 Oct 2018 00:32:42 -0700 Subject: [PATCH 9/9] Adjusted indentation --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d815bcf9be4b9..2e3c91c6667a5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1171,7 +1171,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): >>> df = pd.DataFrame({"num_legs": [4, 4, 2], ... "num_arms": [0, 0, 2]}, - ... ["dog", "cat", "monkey"]) + ... ["dog", "cat", "monkey"]) >>> df num_legs num_arms dog 4 0