Skip to content

Commit 83cfb20

Browse files
clamprCopilot
andauthored
Version 1.7.6 (#209) (#210)
* Fix KeyError when calculating dwpt dynamically (#209) * Initial plan * Fix KeyError in calculate_dwpt when required columns are missing Co-authored-by: clampr <12759785+clampr@users.noreply.github.com> * Fix linting issues: remove trailing whitespace and unused import Co-authored-by: clampr <12759785+clampr@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: clampr <12759785+clampr@users.noreply.github.com> * chore: bump version * fix: update issue link * chore: fix md linting --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: clampr <12759785+clampr@users.noreply.github.com>
1 parent 1508985 commit 83cfb20

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
mode: 'agent'
3+
description: 'Bump version prompt'
4+
---
5+
6+
# Bump Version
7+
8+
Bump the version of the Meteostat Python library.
9+
10+
New version number: ${input:version:Specify the new version number (e.g., 1.2.3)}
11+
12+
Update the following files:
13+
14+
- meteostat/__init__.py
15+
- setup.py

meteostat/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
__appname__ = "meteostat"
15-
__version__ = "1.7.5"
15+
__version__ = "1.7.6"
1616

1717
from .interface.base import Base
1818
from .interface.timeseries import TimeSeries

meteostat/utilities/mutations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def calculate_dwpt(df: pd.DataFrame, col: str) -> pd.DataFrame:
6767
"""
6868
Calculate dew point temperature
6969
"""
70+
# Check if required columns exist
71+
if "temp" not in df.columns or "rhum" not in df.columns:
72+
return df
73+
7074
magnus_const_a = 17.27
7175
magnus_const_b = 237.7 # degrees Celsius
7276

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Setup
1616
setup(
1717
name="meteostat",
18-
version="1.7.5",
18+
version="1.7.6",
1919
author="Meteostat",
2020
author_email="info@meteostat.net",
2121
description="Access and analyze historical weather and climate data with Python.",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
E2E Test - Point Class with Long Period
3+
4+
Tests that Point data retrieval works correctly over long periods
5+
where some stations may have missing data.
6+
7+
The code is licensed under the MIT license.
8+
"""
9+
10+
from datetime import datetime
11+
from meteostat import Point, Hourly
12+
13+
14+
def test_point_long_period():
15+
"""
16+
Test: Point data retrieval over long period (1990-2023)
17+
18+
This test verifies that the KeyError is not raised when trying to get
19+
hourly data from a point over a long time period where some stations
20+
may have missing data for certain years.
21+
22+
Issue: https://github.com/meteostat/meteostat-python/issues/208
23+
"""
24+
25+
# Create Point (coordinates from the original issue)
26+
location = Point(52.9, 11.5, 23)
27+
start = datetime(1990, 1, 1)
28+
end = datetime(2023, 12, 31)
29+
30+
# This should not raise a KeyError
31+
data = Hourly(location, start, end)
32+
result = data.fetch()
33+
34+
# Verify that we get some data (may be empty or have data, but shouldn't crash)
35+
assert result is not None
36+
assert isinstance(result.index.name, str) or result.index.name is None
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Unit Test - Mutations Module
3+
4+
Tests for the mutations utility functions.
5+
6+
The code is licensed under the MIT license.
7+
"""
8+
9+
import pandas as pd
10+
from meteostat.utilities.mutations import calculate_dwpt
11+
12+
13+
def test_calculate_dwpt_with_missing_columns():
14+
"""
15+
Test: calculate_dwpt() with missing required columns
16+
17+
When the dataframe doesn't have the required 'temp' or 'rhum' columns,
18+
the function should return the dataframe unchanged without raising a KeyError.
19+
"""
20+
21+
# Create an empty dataframe
22+
df = pd.DataFrame()
23+
result = calculate_dwpt(df, "dwpt")
24+
assert result.equals(df)
25+
26+
# Create a dataframe with only temp column
27+
df_temp_only = pd.DataFrame({"temp": [20.0, 21.0, 22.0]})
28+
result = calculate_dwpt(df_temp_only, "dwpt")
29+
assert result.equals(df_temp_only)
30+
assert "dwpt" not in result.columns
31+
32+
# Create a dataframe with only rhum column
33+
df_rhum_only = pd.DataFrame({"rhum": [80.0, 85.0, 90.0]})
34+
result = calculate_dwpt(df_rhum_only, "dwpt")
35+
assert result.equals(df_rhum_only)
36+
assert "dwpt" not in result.columns
37+
38+
39+
def test_calculate_dwpt_with_valid_columns():
40+
"""
41+
Test: calculate_dwpt() with valid required columns
42+
43+
When the dataframe has both 'temp' and 'rhum' columns,
44+
the function should calculate the dew point temperature.
45+
"""
46+
47+
# Create a dataframe with both required columns
48+
df = pd.DataFrame({
49+
"temp": [20.0, 15.0, 10.0],
50+
"rhum": [80.0, 70.0, 60.0],
51+
"temp_flag": ["A", "A", "B"],
52+
"rhum_flag": ["A", "B", "A"]
53+
})
54+
55+
result = calculate_dwpt(df, "dwpt")
56+
57+
# Check that dwpt column was added
58+
assert "dwpt" in result.columns
59+
assert "dwpt_flag" in result.columns
60+
61+
# Check that dwpt values are numeric and rounded to 1 decimal
62+
assert result["dwpt"].dtype == "float64"
63+
assert all(result["dwpt"].round(1) == result["dwpt"])
64+
65+
# Check that dwpt_flag was calculated from temp_flag and rhum_flag
66+
assert result["dwpt_flag"].tolist() == ["A", "B", "B"]

0 commit comments

Comments
 (0)