From 8df9bdbeaca5342e550c5a074c00c85b54d35f71 Mon Sep 17 00:00:00 2001 From: Panos Mavrogiorgos Date: Thu, 1 Jun 2023 10:42:42 +0300 Subject: [PATCH] api: Stop throwing exception if you pass a timestamp without a variable in create_trimesh() --- tests/api_test.py | 2 +- thalassa/api.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/api_test.py b/tests/api_test.py index 6cfb945..188fea9 100644 --- a/tests/api_test.py +++ b/tests/api_test.py @@ -46,7 +46,7 @@ def test_main_api(): @pytest.mark.parametrize("variable", [None, "zeta"]) def test_create_trimesh(timestamp, variable): ds = api.open_dataset(ADCIRC_NC) - if timestamp == timestamp: + if timestamp == "timestamp": timestamp = ds.time[0] trimesh = api.create_trimesh(ds, variable=variable, timestamp=timestamp) diff --git a/thalassa/api.py b/thalassa/api.py index 699fb78..ba8ce2b 100644 --- a/thalassa/api.py +++ b/thalassa/api.py @@ -106,13 +106,15 @@ def create_trimesh( columns.append(variable) if layer is not None: ds = ds.isel(layer=layer) - if timestamp == "max": - points_df = ds[columns].max("time").to_dataframe() - elif timestamp == "min": - points_df = ds[columns].min("time").to_dataframe() - elif timestamp: - points_df = ds.sel({"time": timestamp})[columns].to_dataframe().drop(columns="time") + if variable and timestamp: + if timestamp == "max": + points_df = ds[columns].max("time").to_dataframe() + elif timestamp == "min": + points_df = ds[columns].min("time").to_dataframe() + else: + points_df = ds.sel({"time": timestamp})[columns].to_dataframe().drop(columns="time") else: + # Maybe throw an warning if user passed a timestamp but no variable? points_df = ds[columns].to_dataframe() points_df = points_df.reset_index(drop=True) if variable: