Skip to content

Commit ebe32be

Browse files
committed
DOC: Adding examples to update docstring (pandas-dev#16812)
1 parent 7e159ae commit ebe32be

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pandas/core/frame.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4154,6 +4154,42 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
41544154
raise_conflict : boolean
41554155
If True, will raise an error if the DataFrame and other both
41564156
contain data in the same place.
4157+
4158+
Examples
4159+
--------
4160+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4161+
'B': [1, 2, 3]})
4162+
>>> new_df = pd.DataFrame({'B': ['d', 'e', 'f'],
4163+
'C': ['g', 'h', 'i']})
4164+
>>> df.update(new_df)
4165+
>>> df
4166+
A B
4167+
0 a d
4168+
1 b e
4169+
2 c f
4170+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4171+
'B': [1, 2, 3]})
4172+
>>> new_column = pd.Series(['d', 'e', 'f'], name='B')
4173+
>>> df.update(new_column)
4174+
>>> df
4175+
A B
4176+
0 a d
4177+
1 b e
4178+
2 c f
4179+
4180+
If ``other`` contains NaNs the corresponding values are not updated
4181+
in the original dataframe.
4182+
4183+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4184+
'B': [1, 2, 3]})
4185+
>>> new_df = pd.DataFrame({'B': ['d', np.nan, 'f']})
4186+
>>> df.update(new_df)
4187+
>>> df
4188+
A B
4189+
0 a d
4190+
1 b 2
4191+
2 c f
4192+
41574193
"""
41584194
import pandas.core.computation.expressions as expressions
41594195
# TODO: Support other joins

pandas/core/series.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,38 @@ def update(self, other):
17801780
Parameters
17811781
----------
17821782
other : Series
1783+
1784+
Examples
1785+
--------
1786+
>>> s = pd.Series(['a', 'b', 'c', 'd'])
1787+
>>> s.update(pd.Series([1, 2, 3, 4]))
1788+
>>> s
1789+
0 1
1790+
1 2
1791+
2 3
1792+
3 4
1793+
dtype: object
1794+
1795+
>>> s = pd.Series(['a', 'b', 'c', 'd'])
1796+
>>> s.update(pd.Series([1, 4], index=[0, 3]))
1797+
>>> s
1798+
0 1
1799+
1 b
1800+
2 c
1801+
3 4
1802+
dtype: object
1803+
1804+
If ``other`` contains NaNs the corresponding values are not updated
1805+
in the original Series.
1806+
1807+
>>> s = pd.Series(['a', 'b', 'c', 'd'])
1808+
>>> s.update(pd.Series([1, 2, np.nan, 4]))
1809+
>>> s
1810+
0 1
1811+
1 2
1812+
2 c
1813+
3 4
1814+
dtype: object
17831815
"""
17841816
other = other.reindex_like(self)
17851817
mask = notna(other)

0 commit comments

Comments
 (0)