From 6792632fc458f8a3b4713a0b7aec97e928f8c7a0 Mon Sep 17 00:00:00 2001 From: Jonah Lee Date: Sat, 24 May 2025 13:56:58 -0700 Subject: [PATCH 1/2] fix & simplify return_datetime logic for solcast query --- data_tools/query/_solcast.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_tools/query/_solcast.py b/data_tools/query/_solcast.py index c3dcd16..26eb12c 100644 --- a/data_tools/query/_solcast.py +++ b/data_tools/query/_solcast.py @@ -388,10 +388,11 @@ def query( return weather_df # If we want to return datetimes, parse pandas.Timestamp to datetime, otherwise parse into POSIX timestamp - def time_parser(timestamp: pd.Timestamp) -> float | datetime: - return timestamp.timestamp() if not return_datetime else timestamp.to_pydatetime() + if return_datetime: + time_axis: NDArray = np.array([ts.to_pydatetime() for ts in weather_df.index]) + else: + time_axis: NDArray = np.array([ts.timestamp() for ts in weather_df.index]) - time_axis: NDArray = np.fromiter(map(time_parser, weather_df.index), dtype=float) data_arrays: list[NDArray] = [ weather_df[str(output_parameter)].to_numpy() for output_parameter in output_parameters ] From a8b31c0d31467abc90cd077476f0c46ce6e93970 Mon Sep 17 00:00:00 2001 From: Jonah Lee Date: Sat, 24 May 2025 13:57:31 -0700 Subject: [PATCH 2/2] add test for datetime axes --- tests/test_solcast_query.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_solcast_query.py b/tests/test_solcast_query.py index 04109ae..ce90dd9 100644 --- a/tests/test_solcast_query.py +++ b/tests/test_solcast_query.py @@ -96,3 +96,29 @@ def test_solcast_query(): # Everything should be non-null assert np.all(~np.isnan(ghi10[3:])) assert np.all(~np.isnan(ghi)) + +def test_datetime_x_axis(): + api_key = os.getenv("SOLCAST_API_KEY") + client = SolcastClient(api_key) + + test_location = UNMETERED_LOCATIONS["Sydney Opera House"] + + start_time = datetime.now(UTC) - timedelta(hours=3) + end_time = datetime.now(UTC) + timedelta(hours=3) + + desired_outputs = [SolcastOutput(output) for output in ["ghi", "ghi10"]] + + time, ghi, ghi10 = client.query( + latitude=test_location["latitude"], + longitude=test_location["longitude"], + period=SolcastPeriod.PT60M, + output_parameters=desired_outputs, + tilt=0, + azimuth=0, + start_time=start_time, + end_time=end_time, + return_datetime=True, + ) + + assert len(time) == len(ghi) == len(ghi10) == 1 + 3 + 3 # Number of forecasts = 1 + future hours + past hours + assert isinstance(time[0], datetime) \ No newline at end of file