Skip to content

Commit 9183b31

Browse files
abhi-glitchhgaureliobarbosa
authored andcommitted
[Documentation] Added another example in df.clip documentation. (pandas-dev#55589)
* Update generic.py * Update generic.py * Update generic.py Fix for issue pandas-dev#55509 (updating intersection only) fix typo - black Added test
1 parent 52cdc34 commit 9183b31

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

pandas/core/frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8857,11 +8857,11 @@ def update(
88578857
if not isinstance(other, DataFrame):
88588858
other = DataFrame(other)
88598859

8860-
other = other.reindex(self.index)
8860+
indexes_intersection = self.index.intersection(other.index)
88618861

88628862
for col in self.columns.intersection(other.columns):
8863-
this = self[col]._values
8864-
that = other[col]._values
8863+
this = self[col].loc[indexes_intersection]._values
8864+
that = other[col].loc[indexes_intersection]._values
88658865

88668866
if filter_func is not None:
88678867
mask = ~filter_func(this) | isna(that)
@@ -8881,7 +8881,7 @@ def update(
88818881
if mask.all():
88828882
continue
88838883

8884-
self.loc[:, col] = expressions.where(mask, this, that)
8884+
self.loc[indexes_intersection, col] = expressions.where(mask, this, that)
88858885

88868886
# ----------------------------------------------------------------------
88878887
# Data reshaping

pandas/core/generic.py

+10
Original file line numberDiff line numberDiff line change
@@ -8785,6 +8785,16 @@ def clip(
87858785
3 -1 6
87868786
4 5 -4
87878787
8788+
Clips using specific lower and upper thresholds per column:
8789+
8790+
>>> df.clip([-2, -1], [4,5])
8791+
col_0 col_1
8792+
0 4 -1
8793+
1 -2 -1
8794+
2 0 5
8795+
3 -1 5
8796+
4 4 -1
8797+
87888798
Clips using specific lower and upper thresholds per column element:
87898799
87908800
>>> t = pd.Series([2, -4, -1, 6, 3])

pandas/tests/frame/methods/test_update.py

+9
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,12 @@ def test_update_dt_column_with_NaT_create_column(self):
177177
{"A": [1.0, 3.0], "B": [pd.NaT, pd.to_datetime("2016-01-01")]}
178178
)
179179
tm.assert_frame_equal(df, expected)
180+
181+
def test_update_preserve_column_dtype_bool(self):
182+
# GH#55509
183+
df = DataFrame({"A": [True, True]}, index=[1, 2])
184+
other = DataFrame({"A": [False]}, index=[1])
185+
expected = DataFrame({"A": [False, True]}, index=[1, 2])
186+
df.update(other)
187+
188+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)