Skip to content

BUG: EWM silently failed float32 #42650

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 8 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -270,6 +270,7 @@ Groupby/resample/rolling
- Bug in :meth:`Series.rolling.apply`, :meth:`DataFrame.rolling.apply`, :meth:`Series.expanding.apply` and :meth:`DataFrame.expanding.apply` with ``engine="numba"`` where ``*args`` were being cached with the user passed function (:issue:`42287`)
- Bug in :meth:`DataFrame.groupby.rolling.var` would calculate the rolling variance only on the first group (:issue:`42442`)
- Bug in :meth:`GroupBy.shift` that would return the grouping columns if ``fill_value`` was not None (:issue:`41556`)
- Bug in :meth:`pandas.DataFrame.ewm`, where non-float64 dtypes were silently failing (:issue:`42452`)

Reshaping
^^^^^^^^^
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,11 @@ def check_int_infer_dtype(dtypes):
# error: Argument 1 to "append" of "list" has incompatible type
# "Type[signedinteger[Any]]"; expected "Type[signedinteger[Any]]"
converted_dtypes.append(np.int64) # type: ignore[arg-type]
elif dtype == "float" or dtype is float:
# GH#42452 : np.dtype("float") coerces to np.float64 from Numpy 1.20
converted_dtypes.extend(
[np.float64, np.float32] # type: ignore[list-item]
)
else:
# error: Argument 1 to "append" of "list" has incompatible type
# "Union[dtype[Any], ExtensionDtype]"; expected
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/frame/methods/test_select_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,37 @@ def test_select_dtypes_numeric_nullable_string(self, nullable_string_dtype):
df = DataFrame(arr)
is_selected = df.select_dtypes(np.number).shape == df.shape
assert not is_selected

@pytest.mark.parametrize(
"expected, float_dtypes",
[
[
DataFrame(
{"A": range(3), "B": range(5, 8), "C": range(10, 7, -1)}
).astype(dtype={"A": float, "B": np.float64, "C": np.float32}),
float,
],
[
DataFrame(
{"A": range(3), "B": range(5, 8), "C": range(10, 7, -1)}
).astype(dtype={"A": float, "B": np.float64, "C": np.float32}),
"float",
],
[DataFrame({"C": range(10, 7, -1)}, dtype=np.float32), np.float32],
[
DataFrame({"A": range(3), "B": range(5, 8)}).astype(
dtype={"A": float, "B": np.float64}
),
np.float64,
],
],
)
def test_select_dtypes_float_dtype(self, expected, float_dtypes):
# GH#42452
dtype_dict = {"A": float, "B": np.float64, "C": np.float32}
df = DataFrame(
{"A": range(3), "B": range(5, 8), "C": range(10, 7, -1)},
)
df = df.astype(dtype_dict)
result = df.select_dtypes(include=float_dtypes)
tm.assert_frame_equal(result, expected)
48 changes: 48 additions & 0 deletions pandas/tests/window/test_ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,51 @@ def test_ewma_times_adjust_false_raises():
Series(range(1)).ewm(
0.1, adjust=False, times=date_range("2000", freq="D", periods=1)
)


@pytest.mark.parametrize(
"func, expected",
[
[
"mean",
DataFrame(
{
0: range(5),
1: range(4, 9),
2: [7.428571, 9, 10.571429, 12.142857, 13.714286],
},
dtype=float,
),
],
[
"std",
DataFrame(
{
0: [np.nan] * 5,
1: [4.242641] * 5,
2: [4.6291, 5.196152, 5.781745, 6.380775, 6.989788],
}
),
],
[
"var",
DataFrame(
{
0: [np.nan] * 5,
1: [18.0] * 5,
2: [21.428571, 27, 33.428571, 40.714286, 48.857143],
}
),
],
],
)
def test_float_dtype_ewma(func, expected, float_dtype):
# GH#42452

df = DataFrame(
{0: range(5), 1: range(6, 11), 2: range(10, 20, 2)}, dtype=float_dtype
)
e = df.ewm(alpha=0.5, axis=1)
result = getattr(e, func)()

tm.assert_frame_equal(result, expected)
8 changes: 8 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,3 +1424,11 @@ def test_rolling_zero_window():
result = s.rolling(0).min()
expected = Series([np.nan])
tm.assert_series_equal(result, expected)


def test_rolling_float_dtype(float_dtype):
Copy link
Member

Choose a reason for hiding this comment

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

this worked in 1.1.5, see #42452 (comment)

we may want to consider backporting this fix.

# GH#42452
df = DataFrame({"A": range(5), "B": range(10, 15)}, dtype=float_dtype)
expected = DataFrame({"A": [np.nan] * 5, "B": range(10, 20, 2)}, dtype=float_dtype)
result = df.rolling(2, axis=1).sum()
tm.assert_frame_equal(result, expected, check_dtype=False)