-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: support non-nano times in ewm #56262
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,13 +12,15 @@ | |||||
from pandas.util._decorators import doc | ||||||
|
||||||
from pandas.core.dtypes.common import ( | ||||||
is_datetime64_ns_dtype, | ||||||
is_datetime64_dtype, | ||||||
is_numeric_dtype, | ||||||
) | ||||||
from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||||||
from pandas.core.dtypes.generic import ABCSeries | ||||||
from pandas.core.dtypes.missing import isna | ||||||
|
||||||
from pandas.core import common | ||||||
from pandas.core.arrays.datetimelike import dtype_to_unit | ||||||
from pandas.core.indexers.objects import ( | ||||||
BaseIndexer, | ||||||
ExponentialMovingWindowIndexer, | ||||||
|
@@ -56,6 +58,7 @@ | |||||
from pandas._typing import ( | ||||||
Axis, | ||||||
TimedeltaConvertibleTypes, | ||||||
npt, | ||||||
) | ||||||
|
||||||
from pandas import ( | ||||||
|
@@ -101,7 +104,7 @@ def get_center_of_mass( | |||||
def _calculate_deltas( | ||||||
times: np.ndarray | NDFrame, | ||||||
halflife: float | TimedeltaConvertibleTypes | None, | ||||||
) -> np.ndarray: | ||||||
) -> npt.NDArray[np.float64]: | ||||||
""" | ||||||
Return the diff of the times divided by the half-life. These values are used in | ||||||
the calculation of the ewm mean. | ||||||
|
@@ -119,11 +122,11 @@ def _calculate_deltas( | |||||
np.ndarray | ||||||
Diff of the times divided by the half-life | ||||||
""" | ||||||
unit = dtype_to_unit(times.dtype) | ||||||
if isinstance(times, ABCSeries): | ||||||
times = times._values | ||||||
_times = np.asarray(times.view(np.int64), dtype=np.float64) | ||||||
# TODO: generalize to non-nano? | ||||||
_halflife = float(Timedelta(halflife).as_unit("ns")._value) | ||||||
_halflife = float(Timedelta(halflife).as_unit(unit)._value) | ||||||
return np.diff(_times) / _halflife | ||||||
|
||||||
|
||||||
|
@@ -366,7 +369,11 @@ def __init__( | |||||
if self.times is not None: | ||||||
if not self.adjust: | ||||||
raise NotImplementedError("times is not supported with adjust=False.") | ||||||
if not is_datetime64_ns_dtype(self.times): | ||||||
times_dtype = getattr(self.times, "dtype", None) | ||||||
if not ( | ||||||
is_datetime64_dtype(times_dtype) | ||||||
or isinstance(times_dtype, DatetimeTZDtype) | ||||||
): | ||||||
raise ValueError("times must be datetime64[ns] dtype.") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if len(self.times) != len(obj): | ||||||
raise ValueError("times must be the same length as the object.") | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.