Skip to content

Commit d4f9453

Browse files
reidy-pKrzysztof Chomski
authored and
Krzysztof Chomski
committed
DOC: Adding examples to update docstring (pandas-dev#16812) (pandas-dev#17859)
* DOC: Adding examples to update docstring (pandas-dev#16812) * formatting issues * improving examples
1 parent 104b8e3 commit d4f9453

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

pandas/core/frame.py

+55
Original file line numberDiff line numberDiff line change
@@ -4154,6 +4154,61 @@ 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': [1, 2, 3],
4161+
... 'B': [400, 500, 600]})
4162+
>>> new_df = pd.DataFrame({'B': [4, 5, 6],
4163+
... 'C': [7, 8, 9]})
4164+
>>> df.update(new_df)
4165+
>>> df
4166+
A B
4167+
0 1 4
4168+
1 2 5
4169+
2 3 6
4170+
4171+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4172+
... 'B': ['x', 'y', 'z']})
4173+
>>> new_df = pd.DataFrame({'B': ['d', 'e', 'f', 'g', 'h', 'i']})
4174+
>>> df.update(new_df)
4175+
>>> df
4176+
A B
4177+
0 a d
4178+
1 b e
4179+
2 c f
4180+
4181+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4182+
... 'B': ['x', 'y', 'z']})
4183+
>>> new_column = pd.Series(['d', 'e'], name='B', index=[0, 2])
4184+
>>> df.update(new_column)
4185+
>>> df
4186+
A B
4187+
0 a d
4188+
1 b y
4189+
2 c e
4190+
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
4191+
... 'B': ['x', 'y', 'z']})
4192+
>>> new_df = pd.DataFrame({'B': ['d', 'e']}, index=[1, 2])
4193+
>>> df.update(new_df)
4194+
>>> df
4195+
A B
4196+
0 a x
4197+
1 b d
4198+
2 c e
4199+
4200+
If ``other`` contains NaNs the corresponding values are not updated
4201+
in the original dataframe.
4202+
4203+
>>> df = pd.DataFrame({'A': [1, 2, 3],
4204+
... 'B': [400, 500, 600]})
4205+
>>> new_df = pd.DataFrame({'B': [4, np.nan, 6]})
4206+
>>> df.update(new_df)
4207+
>>> df
4208+
A B
4209+
0 1 4.0
4210+
1 2 500.0
4211+
2 3 6.0
41574212
"""
41584213
import pandas.core.computation.expressions as expressions
41594214
# TODO: Support other joins

pandas/core/series.py

+38
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,44 @@ def update(self, other):
17811781
Parameters
17821782
----------
17831783
other : Series
1784+
1785+
Examples
1786+
--------
1787+
>>> s = pd.Series([1, 2, 3])
1788+
>>> s.update(pd.Series([4, 5, 6]))
1789+
>>> s
1790+
0 4
1791+
1 5
1792+
2 6
1793+
dtype: int64
1794+
1795+
>>> s = pd.Series(['a', 'b', 'c'])
1796+
>>> s.update(pd.Series(['d', 'e'], index=[0, 2]))
1797+
>>> s
1798+
0 d
1799+
1 b
1800+
2 e
1801+
dtype: object
1802+
1803+
>>> s = pd.Series([1, 2, 3])
1804+
>>> s.update(pd.Series([4, 5, 6, 7, 8]))
1805+
>>> s
1806+
0 4
1807+
1 5
1808+
2 6
1809+
dtype: int64
1810+
1811+
If ``other`` contains NaNs the corresponding values are not updated
1812+
in the original Series.
1813+
1814+
>>> s = pd.Series([1, 2, 3])
1815+
>>> s.update(pd.Series([4, np.nan, 6]))
1816+
>>> s
1817+
0 4
1818+
1 2
1819+
2 6
1820+
dtype: int64
1821+
17841822
"""
17851823
other = other.reindex_like(self)
17861824
mask = notna(other)

0 commit comments

Comments
 (0)