Skip to content

Commit e0851da

Browse files
committed
option to raise warning when no match is found
See TUW-GEO#152
1 parent 79a3bb7 commit e0851da

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/pytesmo/temporal_matching.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def matching(reference, *args, **kwargs):
177177

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

308312
# postprocessing
309313
# --------------
314+
if checkna:
315+
if np.any(collocated.isnull().apply(np.all)):
316+
warnings.warn("No match has been found")
310317
if dropna:
311318
collocated.dropna(inplace=True)
312319

tests/test_temporal_matching.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ def test_data():
249249
test_dr["random_shift"] = dr_random_shift
250250

251251
# missing data
252-
drop_mask = np.zeros(len(ref_dr), dtype=np.bool)
252+
drop_mask = np.zeros(len(ref_dr), dtype=bool)
253253
drop_mask[100:200] = True
254254
dr_random_shift = dr_random_shift[~drop_mask]
255255
test_dr["missing"] = dr_random_shift
256256
missing_mask = random_mask | drop_mask
257257

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

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

486486
nptest.assert_allclose(np.array([0, 1, 2, 4]), matched.matched_data)
487487
assert len(matched) == 4
488+
489+
490+
def test_warning_on_no_match(test_data):
491+
# Issue #152
492+
ref_frame, test_frame, expected_nan = setup_data(test_data, "shifted_7")
493+
with pytest.warns(UserWarning):
494+
tmatching.temporal_collocation(
495+
ref_frame, test_frame, pd.Timedelta(6, "H"), checkna=True
496+
)
497+
498+
499+

0 commit comments

Comments
 (0)