Skip to content

CLN explicit casts to float to avoid implicit casts #50540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ def test_where_axis_multiple_dtypes(self):

# DataFrame vs DataFrame
d1 = df.copy().drop(1, axis=0)
expected = df.copy()
# Explicit cast to avoid implicit cast when setting value to np.nan
expected = df.copy().astype("float")
expected.loc[1, :] = np.nan

result = df.where(mask, d1)
Expand Down Expand Up @@ -669,7 +670,8 @@ def test_where_categorical_filtering(self):
df["b"] = df["b"].astype("category")

result = df.where(df["a"] > 0)
expected = df.copy()
# Explicitly cast to 'float' to avoid implicit cast when setting np.nan
expected = df.copy().astype({"a": "float"})
expected.loc[0, :] = np.nan

tm.assert_equal(result, expected)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def test_asfreq_fillvalue(self):

# setup
rng = date_range("1/1/2016", periods=10, freq="2S")
ts = Series(np.arange(len(rng)), index=rng)
# Explicit cast to 'float' to avoid implicit cast when setting None
ts = Series(np.arange(len(rng)), index=rng, dtype="float")
df = DataFrame({"one": ts})

# insert pre-existing missing value
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/frame/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def date_range_frame():

class TestFrameAsof:
def test_basic(self, date_range_frame):
df = date_range_frame
# Explicitly cast to float to avoid implicit cast when setting np.nan
df = date_range_frame.astype({"A": "float"})
N = 50
df.loc[df.index[15:30], "A"] = np.nan
dates = date_range("1/1/1990", periods=N * 3, freq="25s")
Expand All @@ -50,7 +51,8 @@ def test_basic(self, date_range_frame):

def test_subset(self, date_range_frame):
N = 10
df = date_range_frame.iloc[:N].copy()
# explicitly cast to float to avoid implicit upcast when setting to np.nan
df = date_range_frame.iloc[:N].copy().astype({"A": "float"})
df.loc[df.index[4:8], "A"] = np.nan
dates = date_range("1/1/1990", periods=N * 3, freq="25s")

Expand Down Expand Up @@ -163,7 +165,7 @@ def test_time_zone_aware_index(self, stamp, expected):
def test_is_copy(self, date_range_frame):
# GH-27357, GH-30784: ensure the result of asof is an actual copy and
# doesn't track the parent dataframe / doesn't give SettingWithCopy warnings
df = date_range_frame
df = date_range_frame.astype({"A": "float"})
N = 50
df.loc[df.index[15:30], "A"] = np.nan
dates = date_range("1/1/1990", periods=N * 3, freq="25s")
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,8 @@ def cast(val):
def test_unstack_nan_index2(self):
# GH7403
df = DataFrame({"A": list("aaaabbbb"), "B": range(8), "C": range(8)})
# Explicit cast to avoid implicit cast when setting to np.NaN
df = df.astype({"B": "float"})
df.iloc[3, 1] = np.NaN
left = df.set_index(["A", "B"]).unstack(0)

Expand All @@ -874,6 +876,8 @@ def test_unstack_nan_index2(self):
tm.assert_frame_equal(left, right)

df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)})
# Explicit cast to avoid implicit cast when setting to np.NaN
df = df.astype({"B": "float"})
df.iloc[2, 1] = np.NaN
left = df.set_index(["A", "B"]).unstack(0)

Expand All @@ -886,6 +890,8 @@ def test_unstack_nan_index2(self):
tm.assert_frame_equal(left, right)

df = DataFrame({"A": list("aaaabbbb"), "B": list(range(4)) * 2, "C": range(8)})
# Explicit cast to avoid implicit cast when setting to np.NaN
df = df.astype({"B": "float"})
df.iloc[3, 1] = np.NaN
left = df.set_index(["A", "B"]).unstack(0)

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/groupby/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def seed_df(seed_nans, n, m):
"3rd": np.random.randint(1, m + 1, n),
}
)
# Explicitly cast to float to avoid implicit cast when setting nan
frame["3rd"] = frame["3rd"].astype("float")
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be needed only if this hits if seed_nans?

Copy link
Member Author

Choose a reason for hiding this comment

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

you're right, thanks!


if seed_nans:
frame.loc[1::11, "1st"] = np.nan
Expand Down