Skip to content

Commit 8be69a4

Browse files
MarcoGorelliim-vinicius
authored and
im-vinicius
committed
CLN: Dont upcast where unnecessary (PDEP6 precursor) (pandas-dev#52957)
1 parent 6f8e79d commit 8be69a4

File tree

8 files changed

+26
-18
lines changed

8 files changed

+26
-18
lines changed

doc/source/user_guide/cookbook.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Building criteria
125125

126126
.. ipython:: python
127127
128-
df.loc[(df["BBB"] > 25) | (df["CCC"] >= 75), "AAA"] = 0.1
128+
df.loc[(df["BBB"] > 25) | (df["CCC"] >= 75), "AAA"] = 999
129129
df
130130
131131
`Select rows with data closest to certain value using argsort

doc/source/user_guide/missing_data.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ the missing value type chosen:
123123

124124
.. ipython:: python
125125
126-
s = pd.Series([1, 2, 3])
126+
s = pd.Series([1., 2., 3.])
127127
s.loc[0] = None
128128
s
129129

doc/source/whatsnew/v0.15.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ Other notable API changes:
748748

749749
.. ipython:: python
750750
751-
s = pd.Series([1, 2, 3])
751+
s = pd.Series([1., 2., 3.])
752752
s.loc[0] = None
753753
s
754754

doc/source/whatsnew/v0.17.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ Boolean comparisons of a ``Series`` vs ``None`` will now be equivalent to compar
738738

739739
.. ipython:: python
740740
741-
s = pd.Series(range(3))
741+
s = pd.Series(range(3), dtype="float")
742742
s.iloc[1] = None
743743
s
744744

pandas/_testing/contexts.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,24 @@ def use_numexpr(use, min_elements=None) -> Generator[None, None, None]:
205205
set_option("compute.use_numexpr", olduse)
206206

207207

208-
def raises_chained_assignment_error():
209-
if PYPY:
208+
def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
209+
from pandas._testing import assert_produces_warning
210+
211+
if PYPY and not extra_warnings:
210212
from contextlib import nullcontext
211213

212214
return nullcontext()
215+
elif PYPY and extra_warnings:
216+
return assert_produces_warning(
217+
extra_warnings,
218+
match="|".join(extra_match),
219+
)
213220
else:
214-
from pandas._testing import assert_produces_warning
215-
221+
match = (
222+
"A value is trying to be set on a copy of a DataFrame or Series "
223+
"through chained assignment"
224+
)
216225
return assert_produces_warning(
217-
ChainedAssignmentError,
218-
match=(
219-
"A value is trying to be set on a copy of a DataFrame or Series "
220-
"through chained assignment"
221-
),
226+
(ChainedAssignmentError, *extra_warnings),
227+
match="|".join((match, *extra_match)),
222228
)

pandas/core/generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7894,7 +7894,7 @@ def asof(self, where, subset=None):
78947894
78957895
Take all columns into consideration
78967896
7897-
>>> df = pd.DataFrame({'a': [10, 20, 30, 40, 50],
7897+
>>> df = pd.DataFrame({'a': [10., 20., 30., 40., 50.],
78987898
... 'b': [None, None, None, None, 500]},
78997899
... index=pd.DatetimeIndex(['2018-02-27 09:01:00',
79007900
... '2018-02-27 09:02:00',
@@ -7912,9 +7912,9 @@ def asof(self, where, subset=None):
79127912
>>> df.asof(pd.DatetimeIndex(['2018-02-27 09:03:30',
79137913
... '2018-02-27 09:04:30']),
79147914
... subset=['a'])
7915-
a b
7916-
2018-02-27 09:03:30 30 NaN
7917-
2018-02-27 09:04:30 40 NaN
7915+
a b
7916+
2018-02-27 09:03:30 30.0 NaN
7917+
2018-02-27 09:04:30 40.0 NaN
79187918
"""
79197919
if isinstance(where, str):
79207920
where = Timestamp(where)

pandas/tests/groupby/test_groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ def f_2(grp):
184184
msg = "DataFrameGroupBy.apply operated on the grouping columns"
185185
with tm.assert_produces_warning(FutureWarning, match=msg):
186186
result = df.groupby("A").apply(f_2)[["B"]]
187-
e = expected.copy()
187+
# Explicit cast to float to avoid implicit cast when setting nan
188+
e = expected.copy().astype({"B": "float"})
188189
e.loc["Pony"] = np.nan
189190
tm.assert_frame_equal(result, e)
190191

pandas/tests/groupby/test_nunique.py

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def check_nunique(df, keys, as_index=True):
5151
check_nunique(frame, ["jim"])
5252
check_nunique(frame, ["jim", "joe"])
5353

54+
frame = frame.astype({"julie": float}) # Explicit cast to avoid implicit cast below
5455
frame.loc[1::17, "jim"] = None
5556
frame.loc[3::37, "joe"] = None
5657
frame.loc[7::19, "julie"] = None

0 commit comments

Comments
 (0)