Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ Numeric
- Bug in :class:`DataFrame` arithmetic ops with a subclass whose :meth:`_constructor` attribute is a callable other than the subclass itself (:issue:`43201`)
- Bug in arithmetic operations involving :class:`RangeIndex` where the result would have the incorrect ``name`` (:issue:`43962`)
- Bug in arithmetic operations involving :class:`Series` where the result could have the incorrect ``name`` when the operands having matching NA or matching tuple names (:issue:`44459`)
- Bug in :meth:`Series.clip` raising if bounds are a :class:`Series` with ``NA`` values for datetimes or nullable integer dtypes (:issue:`44785`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move to 1.5

- Bug in division with ``IntegerDtype`` or ``BooleanDtype`` array and NA scalar incorrectly raising (:issue:`44685`)
- Bug in multiplying a :class:`Series` with ``FloatingDtype`` with a timedelta-like scalar incorrectly raising (:issue:`44772`)
-
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7381,7 +7381,13 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
# GH 40420
# Treat missing thresholds as no bounds, not clipping the values
if is_list_like(threshold):
fill_value = np.inf if method.__name__ == "le" else -np.inf
method_name_le = method.__name__ == "le"
if is_datetime64_any_dtype(self.dtype):
fill_value = Timestamp.max if method_name_le else Timestamp.min
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for dt64tz shouldn't fill_value be tzaware?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a timezone aware Timestamp ts where ts > Timestamp.max would be true?

The fill value is not show in the result, it is just a placeholder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a timezone aware Timestamp ts where ts > Timestamp.max would be true?

no, that comparison would always raise TypeError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm ok, then we could use the max case for timzeones too

elif is_extension_array_dtype(self.dtype):
fill_value = self.max() if method_name_le else self.min()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to do this for ea ints?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas/core/dtypes/missing/na_value_for_dtype

else:
fill_value = np.inf if method_name_le else -np.inf
threshold_inf = threshold.fillna(fill_value)
else:
threshold_inf = threshold
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/methods/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ def test_clip_against_series(self):
tm.assert_series_equal(s.clip(lower, upper), Series([1.0, 2.0, 3.5]))
tm.assert_series_equal(s.clip(1.5, upper), Series([1.5, 1.5, 3.5]))

@pytest.mark.parametrize("bound_values", [[pd.NA, 1], [1, pd.NA]])
def test_clip_against_series_ea_int_dtype(self, any_int_ea_dtype, bound_values):
# GH#44785
ser = Series([1, 1], dtype=any_int_ea_dtype)
bounds = Series(bound_values, dtype=any_int_ea_dtype)
expected = ser.copy()
result = ser.clip(bounds)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize("upper", [[1, 2, 3], np.asarray([1, 2, 3])])
def test_clip_against_list_like(self, inplace, upper):
Expand Down Expand Up @@ -138,6 +147,18 @@ def test_clip_with_timestamps_and_oob_datetimes(self):

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"bound_values",
[[pd.NaT, Timestamp("1970-01-01")], [Timestamp("1970-01-01"), pd.NaT]],
)
def test_clip_timestamp_and_na(self, bound_values):
# GH#44785
ser = Series([Timestamp("1970-01-01")] * 2)
bounds = Series(bound_values)
expected = ser.copy()
result = ser.clip(bounds)
tm.assert_series_equal(result, expected)

def test_clip_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
ser = Series([1, 2, 3])
Expand Down