Skip to content

Commit 9664c48

Browse files
authored
Merge branch 'main' into issue-50977
2 parents 0518313 + 5c745bd commit 9664c48

File tree

8 files changed

+48
-25
lines changed

8 files changed

+48
-25
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ Deprecations
781781
- :meth:`Index.is_interval` has been deprecated. Use :func:`pandas.api.types.is_intterval_dtype` instead (:issue:`50042`)
782782
- Deprecated ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes, use e.g. ``(obj != pd.Timestamp(0), tz=obj.tz).all()`` instead (:issue:`34479`)
783783
- Deprecated unused arguments ``*args`` and ``**kwargs`` in :class:`Resampler` (:issue:`50977`)
784+
- Deprecated calling ``float`` or ``int`` on a single element :class:`Series` to return a ``float`` or ``int`` respectively. Extract the element before calling ``float`` or ``int`` instead (:issue:`51101`)
784785

785786
.. ---------------------------------------------------------------------------
786787
.. _whatsnew_200.prior_deprecations:

pandas/core/internals/managers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,15 @@ def diff(self: T, n: int, axis: AxisInt) -> T:
409409
axis = self._normalize_axis(axis)
410410
return self.apply("diff", n=n, axis=axis)
411411

412-
def interpolate(self: T, **kwargs) -> T:
413-
return self.apply("interpolate", **kwargs)
412+
def interpolate(self: T, inplace: bool, **kwargs) -> T:
413+
if inplace:
414+
# TODO(CoW) can be optimized to only copy those blocks that have refs
415+
if using_copy_on_write() and any(
416+
not self._has_no_reference_block(i) for i in range(len(self.blocks))
417+
):
418+
self = self.copy()
419+
420+
return self.apply("interpolate", inplace=inplace, **kwargs)
414421

415422
def shift(self: T, periods: int, axis: AxisInt, fill_value) -> T:
416423
axis = self._normalize_axis(axis)

pandas/core/resample.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ def __init__(
153153
*,
154154
group_keys: bool | lib.NoDefault = lib.no_default,
155155
selection=None,
156-
**kwargs,
157156
) -> None:
158157
self._timegrouper = groupby
159158
self.keys = None
@@ -173,18 +172,6 @@ def __init__(
173172
else:
174173
self.exclusions = frozenset()
175174

176-
@final
177-
def _shallow_copy(self, obj, **kwargs):
178-
"""
179-
return a new object with the replacement attributes
180-
"""
181-
if isinstance(obj, self._constructor):
182-
obj = obj.obj
183-
for attr in self._attributes:
184-
if attr not in kwargs:
185-
kwargs[attr] = getattr(self, attr)
186-
return self._constructor(obj, **kwargs)
187-
188175
def __str__(self) -> str:
189176
"""
190177
Provide a nice str repr of our rolling object.
@@ -1196,7 +1183,7 @@ def _apply(self, f, *args, **kwargs):
11961183
"""
11971184

11981185
def func(x):
1199-
x = self._shallow_copy(x, groupby=self._timegrouper)
1186+
x = self._resampler_cls(x, groupby=self._timegrouper)
12001187

12011188
if isinstance(f, str):
12021189
return getattr(x, f)(**kwargs)
@@ -1379,7 +1366,7 @@ class DatetimeIndexResamplerGroupby(_GroupByMixin, DatetimeIndexResampler):
13791366
"""
13801367

13811368
@property
1382-
def _constructor(self):
1369+
def _resampler_cls(self):
13831370
return DatetimeIndexResampler
13841371

13851372

@@ -1491,7 +1478,7 @@ class PeriodIndexResamplerGroupby(_GroupByMixin, PeriodIndexResampler):
14911478
"""
14921479

14931480
@property
1494-
def _constructor(self):
1481+
def _resampler_cls(self):
14951482
return PeriodIndexResampler
14961483

14971484

@@ -1519,7 +1506,7 @@ class TimedeltaIndexResamplerGroupby(_GroupByMixin, TimedeltaIndexResampler):
15191506
"""
15201507

15211508
@property
1522-
def _constructor(self):
1509+
def _resampler_cls(self):
15231510
return TimedeltaIndexResampler
15241511

15251512

pandas/core/series.py

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
cast,
2020
overload,
2121
)
22+
import warnings
2223
import weakref
2324

2425
import numpy as np
@@ -81,6 +82,7 @@
8182
Substitution,
8283
doc,
8384
)
85+
from pandas.util._exceptions import find_stack_level
8486
from pandas.util._validators import (
8587
validate_ascending,
8688
validate_bool_kwarg,
@@ -216,6 +218,13 @@ def _coerce_method(converter):
216218

217219
def wrapper(self):
218220
if len(self) == 1:
221+
warnings.warn(
222+
f"Calling {converter.__name__} on a single element Series is "
223+
"deprecated and will raise a TypeError in the future. "
224+
f"Use {converter.__name__}(ser.iloc[0]) instead",
225+
FutureWarning,
226+
stacklevel=find_stack_level(),
227+
)
219228
return converter(self.iloc[0])
220229
raise TypeError(f"cannot convert the series to {converter}")
221230

pandas/tests/apply/test_invalid_arg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ def test_agg_none_to_type():
277277
df = DataFrame({"a": [None]})
278278
msg = re.escape("int() argument must be a string")
279279
with pytest.raises(TypeError, match=msg):
280-
df.agg({"a": int})
280+
df.agg({"a": lambda x: int(x.iloc[0])})
281281

282282

283283
def test_transform_none_to_type():
284284
# GH#34377
285285
df = DataFrame({"a": [None]})
286286
msg = "argument must be a"
287287
with pytest.raises(TypeError, match=msg):
288-
df.transform({"a": int})
288+
df.transform({"a": lambda x: int(x.iloc[0])})
289289

290290

291291
@pytest.mark.parametrize(

pandas/tests/copy_view/test_methods.py

+16
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,22 @@ def test_asfreq_noop(using_copy_on_write):
11941194
tm.assert_frame_equal(df, df_orig)
11951195

11961196

1197+
def test_interpolate_creates_copy(using_copy_on_write):
1198+
# GH#51126
1199+
df = DataFrame({"a": [1.5, np.nan, 3]})
1200+
view = df[:]
1201+
expected = df.copy()
1202+
1203+
df.ffill(inplace=True)
1204+
df.iloc[0, 0] = 100.5
1205+
1206+
if using_copy_on_write:
1207+
tm.assert_frame_equal(view, expected)
1208+
else:
1209+
expected = DataFrame({"a": [100.5, 1.5, 3]})
1210+
tm.assert_frame_equal(view, expected)
1211+
1212+
11971213
def test_isetitem(using_copy_on_write):
11981214
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
11991215
df_orig = df.copy()

pandas/tests/series/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,10 @@ def test_numeric_only(self, kernel, has_numeric_only, dtype):
287287
else:
288288
# reducer
289289
assert result == expected
290+
291+
292+
@pytest.mark.parametrize("converter", [int, float, complex])
293+
def test_float_int_deprecated(converter):
294+
# GH 51101
295+
with tm.assert_produces_warning(FutureWarning):
296+
assert converter(Series([1])) == converter(1)

pandas/tests/series/test_constructors.py

-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ def test_scalar_conversion(self):
145145
scalar = Series(0.5)
146146
assert not isinstance(scalar, float)
147147

148-
# Coercion
149-
assert float(Series([1.0])) == 1.0
150-
assert int(Series([1.0])) == 1
151-
152148
def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
153149
# GH 28401
154150

0 commit comments

Comments
 (0)