From fde5ed8e4078a4b59fa2889a882b27684f89b506 Mon Sep 17 00:00:00 2001 From: SaraInCode <42293506+SaraInCode@users.noreply.github.com> Date: Sun, 23 Feb 2025 10:50:51 +0100 Subject: [PATCH 1/4] Updated set_index doc with a warning --- pandas/core/frame.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3199733cfb85f..7393dd748542e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5868,6 +5868,10 @@ def set_index( columns or arrays (of the correct length). The index can replace the existing index or expand on it. + .. warning:: + Setting a new index will remove the current index column, + unless you first call `reset_index`. + Parameters ---------- keys : label or array-like or list of labels/arrays From fa65ba5d0f1eb7fb519c8f2eea9786fef3202b30 Mon Sep 17 00:00:00 2001 From: SaraInCode <42293506+SaraInCode@users.noreply.github.com> Date: Sun, 2 Mar 2025 11:10:23 +0100 Subject: [PATCH 2/4] Updated set_index parameter append along with an example --- pandas/core/frame.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 82989e9a3904d..b9e28395d9092 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5868,10 +5868,6 @@ def set_index( columns or arrays (of the correct length). The index can replace the existing index or expand on it. - .. warning:: - Setting a new index will remove the current index column, - unless you first call `reset_index`. - Parameters ---------- keys : label or array-like or list of labels/arrays @@ -5884,6 +5880,8 @@ def set_index( Delete columns to be used as the new index. append : bool, default False Whether to append columns to existing index. + Setting to True will add the new columns to existing index. + When set to False, the current index will be dropped from the DataFrame. inplace : bool, default False Whether to modify the DataFrame rather than creating a new one. verify_integrity : bool, default False @@ -5957,6 +5955,25 @@ def set_index( 2 4 4 2014 40 3 9 7 2013 84 4 16 10 2014 31 + + Append a column to the existing index: + + >>> df.set_index("month", inplace=True) + >>> df.set_index("year", append=True) + sale + month year + 1 2012 55 + 4 2014 40 + 7 2013 84 + 10 2014 31 + + >>> df.set_index("year", append=False) + sale + year + 2012 55 + 2014 40 + 2013 84 + 2014 31 """ inplace = validate_bool_kwarg(inplace, "inplace") self._check_inplace_and_allows_duplicate_labels(inplace) From d053e6cea37c93ecafbab27f646b32d13df44fdc Mon Sep 17 00:00:00 2001 From: SaraInCode <42293506+SaraInCode@users.noreply.github.com> Date: Sun, 2 Mar 2025 19:42:39 +0100 Subject: [PATCH 3/4] Updated set_index example for append --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b9e28395d9092..5cf924cfa6c81 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5958,8 +5958,8 @@ def set_index( Append a column to the existing index: - >>> df.set_index("month", inplace=True) - >>> df.set_index("year", append=True) + >>> a = df.set_index("month") + >>> a.set_index("year", append=True) sale month year 1 2012 55 @@ -5967,7 +5967,7 @@ def set_index( 7 2013 84 10 2014 31 - >>> df.set_index("year", append=False) + >>> a.set_index("year", append=False) sale year 2012 55 From ff0a8055370093afc82ca58603968bb460e10011 Mon Sep 17 00:00:00 2001 From: SaraInCode <42293506+SaraInCode@users.noreply.github.com> Date: Tue, 4 Mar 2025 19:14:36 +0100 Subject: [PATCH 4/4] Updated set_index example --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5cf924cfa6c81..28485bf5fdfd8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5958,8 +5958,8 @@ def set_index( Append a column to the existing index: - >>> a = df.set_index("month") - >>> a.set_index("year", append=True) + >>> df = df.set_index("month") + >>> df.set_index("year", append=True) sale month year 1 2012 55 @@ -5967,7 +5967,7 @@ def set_index( 7 2013 84 10 2014 31 - >>> a.set_index("year", append=False) + >>> df.set_index("year", append=False) sale year 2012 55