Skip to content

Commit

Permalink
DOC: Adding examples to update docstring (pandas-dev#16812)
Browse files Browse the repository at this point in the history
  • Loading branch information
reidy-p committed Oct 12, 2017
1 parent 7e159ae commit ebe32be
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ebe32be

Please sign in to comment.