Skip to content
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

Fix clean method in case var is nan. #164

Merged
merged 1 commit into from
Oct 25, 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 CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Lionel Roubeyrie - LIMAIR - https://github.com/LionelR - original author
- Bruno Ruas De Pinho - https://github.com/brunorpinho
- Samuël Weber - Institut des Géosciences de l'Environnement, Grenoble, France, https://github.com/weber-s
- 15b3 - https://github.com/15b3

[Full Github contributors list](https://github.com/python-windrose/windrose/graphs/contributors).

Expand Down
34 changes: 34 additions & 0 deletions tests/test_windrose.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
# from windrose import FIGSIZE_DEFAULT, DPI_DEFAULT
from windrose import wrbar, wrbox, wrcontour, wrcontourf, wrpdf, wrscatter
from windrose import plot_windrose
from windrose import clean, clean_df

import numpy as np
import pandas as pd
from numpy.testing import assert_allclose
from pandas.testing import assert_frame_equal


# Create wind speed and direction variables
Expand Down Expand Up @@ -208,3 +211,34 @@ def test_windrose_pd_not_default_names():
# #df = df.reset_index()
# #df = df.set_index([by, 'Timestamp'])
# #print(df)


def test_windrose_clean():
direction = np.array(
[1.0, 1.0, 1.0, np.nan, np.nan, np.nan]
)
var = np.array(
[2.0, 0.0, np.nan, 2.0, 0.0, np.nan]
)
actual_direction, actual_var = clean(direction, var)
expected_direction = np.array([1.0])
expected_var = np.array([2.0])
assert_allclose(actual_direction, expected_direction)
assert_allclose(actual_var, expected_var)


def test_windrose_clean_df():
df = pd.DataFrame(
{
"direction": [1.0, 1.0, 1.0, np.nan, np.nan, np.nan],
"speed": [2.0, 0.0, np.nan, 2.0, 0.0, np.nan],
}
)
actual_df = clean_df(df)
expected_df = pd.DataFrame(
{
"direction": [1.0],
"speed": [2.0],
}
)
assert_frame_equal(actual_df, expected_df)
6 changes: 3 additions & 3 deletions windrose/windrose.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ def clean_df(df, var=VAR_DEFAULT, direction=DIR_DEFAULT):
removed from DataFrame
if a direction is nan, this row is also removed from DataFrame
"""
return df[df[var].notnull() & df[var] != 0 & df[direction].notnull()]
return df[df[var].notnull() & (df[var] != 0) & df[direction].notnull()]


def clean(direction, var, index=False):
Expand All @@ -853,8 +853,8 @@ def clean(direction, var, index=False):
if a direction is nan, data is also removed from both array
"""
dirmask = np.isfinite(direction)
varmask = var != 0 & np.isfinite(var)
mask = dirmask * varmask
varmask = (var != 0) & np.isfinite(var)
mask = dirmask & varmask
if index is None:
index = np.arange(mask.sum())
return direction[mask], var[mask], index
Expand Down