From ebe32befc5242bc3f0eedc5ae37335c9f196b991 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Thu, 12 Oct 2017 23:17:50 +0100 Subject: [PATCH 1/3] DOC: Adding examples to update docstring (#16812) --- pandas/core/frame.py | 36 ++++++++++++++++++++++++++++++++++++ pandas/core/series.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c7e8c0da75e2c..21849dbd59d7f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4154,6 +4154,42 @@ def update(self, other, join='left', overwrite=True, filter_func=None, raise_conflict : boolean If True, will raise an error if the DataFrame and other both contain data in the same place. + + Examples + -------- + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], + 'B': [1, 2, 3]}) + >>> new_df = pd.DataFrame({'B': ['d', 'e', 'f'], + 'C': ['g', 'h', 'i']}) + >>> df.update(new_df) + >>> df + A B + 0 a d + 1 b e + 2 c f + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], + 'B': [1, 2, 3]}) + >>> new_column = pd.Series(['d', 'e', 'f'], name='B') + >>> df.update(new_column) + >>> df + A B + 0 a d + 1 b e + 2 c f + + If ``other`` contains NaNs the corresponding values are not updated + in the original dataframe. + + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], + 'B': [1, 2, 3]}) + >>> new_df = pd.DataFrame({'B': ['d', np.nan, 'f']}) + >>> df.update(new_df) + >>> df + A B + 0 a d + 1 b 2 + 2 c f + """ import pandas.core.computation.expressions as expressions # TODO: Support other joins diff --git a/pandas/core/series.py b/pandas/core/series.py index 8499f8b55d2d0..e8668a38b6b2a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1780,6 +1780,38 @@ def update(self, other): Parameters ---------- other : Series + + Examples + -------- + >>> s = pd.Series(['a', 'b', 'c', 'd']) + >>> s.update(pd.Series([1, 2, 3, 4])) + >>> s + 0 1 + 1 2 + 2 3 + 3 4 + dtype: object + + >>> s = pd.Series(['a', 'b', 'c', 'd']) + >>> s.update(pd.Series([1, 4], index=[0, 3])) + >>> s + 0 1 + 1 b + 2 c + 3 4 + dtype: object + + If ``other`` contains NaNs the corresponding values are not updated + in the original Series. + + >>> s = pd.Series(['a', 'b', 'c', 'd']) + >>> s.update(pd.Series([1, 2, np.nan, 4])) + >>> s + 0 1 + 1 2 + 2 c + 3 4 + dtype: object """ other = other.reindex_like(self) mask = notna(other) From 3c4c045ccca3d517716892462d38e8e3a4ba285f Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Fri, 13 Oct 2017 00:16:42 +0100 Subject: [PATCH 2/3] formatting issues --- pandas/core/frame.py | 1 + pandas/core/series.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 21849dbd59d7f..5d5926eb5e834 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4167,6 +4167,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None, 0 a d 1 b e 2 c f + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], 'B': [1, 2, 3]}) >>> new_column = pd.Series(['d', 'e', 'f'], name='B') diff --git a/pandas/core/series.py b/pandas/core/series.py index e8668a38b6b2a..d6687bf07489e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1780,7 +1780,7 @@ def update(self, other): Parameters ---------- other : Series - + Examples -------- >>> s = pd.Series(['a', 'b', 'c', 'd']) @@ -1791,7 +1791,7 @@ def update(self, other): 2 3 3 4 dtype: object - + >>> s = pd.Series(['a', 'b', 'c', 'd']) >>> s.update(pd.Series([1, 4], index=[0, 3])) >>> s @@ -1800,7 +1800,7 @@ def update(self, other): 2 c 3 4 dtype: object - + If ``other`` contains NaNs the corresponding values are not updated in the original Series. From 6e8fd9020a6d509d0641479b14945e3703dd2367 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Fri, 13 Oct 2017 21:20:11 +0100 Subject: [PATCH 3/3] improving examples --- pandas/core/frame.py | 48 +++++++++++++++++++++++++++++-------------- pandas/core/series.py | 42 +++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5d5926eb5e834..250978f79fba1 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4157,10 +4157,20 @@ def update(self, other, join='left', overwrite=True, filter_func=None, Examples -------- + >>> df = pd.DataFrame({'A': [1, 2, 3], + ... 'B': [400, 500, 600]}) + >>> new_df = pd.DataFrame({'B': [4, 5, 6], + ... 'C': [7, 8, 9]}) + >>> df.update(new_df) + >>> df + A B + 0 1 4 + 1 2 5 + 2 3 6 + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], - 'B': [1, 2, 3]}) - >>> new_df = pd.DataFrame({'B': ['d', 'e', 'f'], - 'C': ['g', 'h', 'i']}) + ... 'B': ['x', 'y', 'z']}) + >>> new_df = pd.DataFrame({'B': ['d', 'e', 'f', 'g', 'h', 'i']}) >>> df.update(new_df) >>> df A B @@ -4169,28 +4179,36 @@ def update(self, other, join='left', overwrite=True, filter_func=None, 2 c f >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], - 'B': [1, 2, 3]}) - >>> new_column = pd.Series(['d', 'e', 'f'], name='B') + ... 'B': ['x', 'y', 'z']}) + >>> new_column = pd.Series(['d', 'e'], name='B', index=[0, 2]) >>> df.update(new_column) >>> df A B 0 a d - 1 b e - 2 c f + 1 b y + 2 c e + >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], + ... 'B': ['x', 'y', 'z']}) + >>> new_df = pd.DataFrame({'B': ['d', 'e']}, index=[1, 2]) + >>> df.update(new_df) + >>> df + A B + 0 a x + 1 b d + 2 c e If ``other`` contains NaNs the corresponding values are not updated in the original dataframe. - >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], - 'B': [1, 2, 3]}) - >>> new_df = pd.DataFrame({'B': ['d', np.nan, 'f']}) + >>> df = pd.DataFrame({'A': [1, 2, 3], + ... 'B': [400, 500, 600]}) + >>> new_df = pd.DataFrame({'B': [4, np.nan, 6]}) >>> df.update(new_df) >>> df - A B - 0 a d - 1 b 2 - 2 c f - + A B + 0 1 4.0 + 1 2 500.0 + 2 3 6.0 """ import pandas.core.computation.expressions as expressions # TODO: Support other joins diff --git a/pandas/core/series.py b/pandas/core/series.py index d6687bf07489e..902ba586fce00 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1783,35 +1783,41 @@ def update(self, other): Examples -------- - >>> s = pd.Series(['a', 'b', 'c', 'd']) - >>> s.update(pd.Series([1, 2, 3, 4])) + >>> s = pd.Series([1, 2, 3]) + >>> s.update(pd.Series([4, 5, 6])) >>> s - 0 1 - 1 2 - 2 3 - 3 4 - dtype: object + 0 4 + 1 5 + 2 6 + dtype: int64 - >>> s = pd.Series(['a', 'b', 'c', 'd']) - >>> s.update(pd.Series([1, 4], index=[0, 3])) + >>> s = pd.Series(['a', 'b', 'c']) + >>> s.update(pd.Series(['d', 'e'], index=[0, 2])) >>> s - 0 1 + 0 d 1 b - 2 c - 3 4 + 2 e dtype: object + >>> s = pd.Series([1, 2, 3]) + >>> s.update(pd.Series([4, 5, 6, 7, 8])) + >>> s + 0 4 + 1 5 + 2 6 + dtype: int64 + If ``other`` contains NaNs the corresponding values are not updated in the original Series. - >>> s = pd.Series(['a', 'b', 'c', 'd']) - >>> s.update(pd.Series([1, 2, np.nan, 4])) + >>> s = pd.Series([1, 2, 3]) + >>> s.update(pd.Series([4, np.nan, 6])) >>> s - 0 1 + 0 4 1 2 - 2 c - 3 4 - dtype: object + 2 6 + dtype: int64 + """ other = other.reindex_like(self) mask = notna(other)