Skip to content

Commit

Permalink
option to raise warning when no match is found
Browse files Browse the repository at this point in the history
  • Loading branch information
s-scherrer committed Feb 12, 2021
1 parent 79a3bb7 commit e0851da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/pytesmo/temporal_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def matching(reference, *args, **kwargs):

def temporal_collocation(reference, other, window, method="nearest",
return_index=False, return_distance=False,
dropduplicates=False, dropna=False, flag=None,
use_invalid=False):
dropduplicates=False, dropna=False, checkna=False,
flag=None, use_invalid=False):
"""
Temporally collocates values to reference.
Expand Down Expand Up @@ -218,7 +218,11 @@ def temporal_collocation(reference, other, window, method="nearest",
dropna : bool, optional
Whether to drop NaNs from the resulting dataframe (arising for example
from duplicates with ``duplicates_nan=True`` or from missing values).
Default is ``False``
Default is ``False``.
checkna: bool, optional
Whether to check if only NaNs are returned (i.e. no match has been
found). If set to ``True``, raises a ``UserWarning`` in case no match
has been found. Default is ``False``.
flag : np.ndarray, str or None, optional
Flag column as array or name of the flag column in `other`. If this is
given, the column will be interpreted as validity indicator. Any
Expand Down Expand Up @@ -307,6 +311,9 @@ def collocate(df):

# postprocessing
# --------------
if checkna:
if np.any(collocated.isnull().apply(np.all)):
warnings.warn("No match has been found")
if dropna:
collocated.dropna(inplace=True)

Expand Down
18 changes: 15 additions & 3 deletions tests/test_temporal_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ def test_data():
test_dr["random_shift"] = dr_random_shift

# missing data
drop_mask = np.zeros(len(ref_dr), dtype=np.bool)
drop_mask = np.zeros(len(ref_dr), dtype=bool)
drop_mask[100:200] = True
dr_random_shift = dr_random_shift[~drop_mask]
test_dr["missing"] = dr_random_shift
missing_mask = random_mask | drop_mask

# with duplicates
test_dr["duplicates"] = deepcopy(test_dr["shifted_3"])
duplicates_mask = np.zeros(len(ref_dr), dtype=np.bool)
duplicates_mask = np.zeros(len(ref_dr), dtype=bool)
for idx in np.random.randint(0, len(test_dr["duplicates"]) - 1, 5):
test_dr["duplicates"].values[idx] = test_dr["duplicates"].values[
idx + 1
Expand All @@ -274,7 +274,7 @@ def test_data():
ref_frame = pd.DataFrame(np.random.randn(len(ref_dr), 3), index=ref_dr)

# mask for where we expect nans in the output
all_nan = np.ones(len(ref_dr), dtype=np.bool)
all_nan = np.ones(len(ref_dr), dtype=bool)
expected_nan = {
"shifted_3": ~all_nan,
"shifted_7": all_nan,
Expand Down Expand Up @@ -485,3 +485,15 @@ def test_timezone_handling():

nptest.assert_allclose(np.array([0, 1, 2, 4]), matched.matched_data)
assert len(matched) == 4


def test_warning_on_no_match(test_data):
# Issue #152
ref_frame, test_frame, expected_nan = setup_data(test_data, "shifted_7")
with pytest.warns(UserWarning):
tmatching.temporal_collocation(
ref_frame, test_frame, pd.Timedelta(6, "H"), checkna=True
)



0 comments on commit e0851da

Please sign in to comment.