I am running an outlier check on a pandas Series object with two passes using different standard deviation criteria. However, I use two loops for that and it run extremely slow. I wonder if there is any pandas "tricks" to speed-up this step.
Here is the code I am using (warning really ugly code!):
def find_outlier(point, window, n):
return np.abs(point - nanmean(window)) >= n * nanstd(window)
def despike(self, std1=2, std2=20, block=100, keep=0):
res = self.values.copy()
# First run with std1:
for k, point in enumerate(res):
if k <= block:
window = res[k:k + block]
elif k >= len(res) - block:
window = res[k - block:k]
else:
window = res[k - block:k + block]
window = window[~np.isnan(window)]
if np.abs(point - window.mean()) >= std1 * window.std():
res[k] = np.NaN
# Second run with std2:
for k, point in enumerate(res):
if k <= block:
window = res[k:k + block]
elif k >= len(res) - block:
window = res[k - block:k]
else:
window = res[k - block:k + block]
window = window[~np.isnan(window)]
if np.abs(point - window.mean()) >= std2 * window.std():
res[k] = np.NaN
return Series(res, index=self.index, name=self.name)
I'm not sure what you're doing with that block piece, but finding outliers in a Series should be as easy as:
In [1]: s > s.std() * 3
Where s is your series and 3 ishow many standard deviations to exceed for outlier status. This expression will return a series of boolean values that you can then index the series by:
In [2]: s.head(10)
Out[2]:
0 1.181462
1 -0.112049
2 0.864603
3 -0.220569
4 1.985747
5 4.000000
6 -0.632631
7 -0.397940
8 0.881585
9 0.484691
Name: val
In [3]: s[s > s.std() * 3]
Out[3]:
5 4
Name: val
UPDATE:
Addressing the comment about block. I think you can use pd.rolling_std()
in this case:
In [53]: pd.rolling_std(s, window=5).head(10)
Out[53]:
0 NaN
1 NaN
2 NaN
3 NaN
4 0.871541
5 0.925348
6 0.920313
7 0.370928
8 0.467932
9 0.391485
In [55]: abs(s) > pd.rolling_std(s, window=5) * 3
Docstring:
Unbiased moving standard deviation
Parameters
----------
arg : Series, DataFrame
window : Number of observations used for calculating statistic
min_periods : int
Minimum number of observations in window required to have a value
freq : None or string alias / date offset object, default=None
Frequency to conform to before computing statistic
time_rule is a legacy alias for freq
Returns
-------
y : type of input argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With