Skip to content

Commit e1efaef

Browse files
committed
make start and enddate more flexible as per #88
1 parent 582b8a0 commit e1efaef

1 file changed

Lines changed: 46 additions & 23 deletions

File tree

src/spei/knmi.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ def get_cumulative_deficit(
122122
return cumdf
123123

124124

125-
def deficit_oct1(deficit: pd.Series) -> pd.Series:
125+
def deficit_oct1(
126+
deficit: pd.Series, startdate: pd.Timestamp | str = "2000-04-01"
127+
) -> pd.Series:
126128
"""Compute the cumulative deficit on October 1st.
127129
128130
This function computes the cumulative deficit for a given time series
@@ -136,6 +138,9 @@ def deficit_oct1(deficit: pd.Series) -> pd.Series:
136138
A pandas Series representing the deficit time series. The index
137139
should be datetime-like, and the values should represent the
138140
deficit amounts.
141+
startdate : pd.Timestamp, optional
142+
The start date for the cumulative deficit calculation. Defaults
143+
to April 1st.
139144
140145
Returns
141146
-------
@@ -146,11 +151,10 @@ def deficit_oct1(deficit: pd.Series) -> pd.Series:
146151
of the Series is "Doct1".
147152
148153
"""
149-
startdate = pd.Timestamp("2000-04-01")
150154
enddate = pd.Timestamp("2000-09-30")
151155
cumdf = get_cumulative_deficit(
152156
deficit=deficit,
153-
startdate=startdate,
157+
startdate=pd.Timestamp(startdate),
154158
enddate=enddate,
155159
allow_below_zero=False,
156160
)
@@ -163,7 +167,11 @@ def deficit_oct1(deficit: pd.Series) -> pd.Series:
163167
return doct1
164168

165169

166-
def deficit_max(deficit: pd.Series) -> pd.Series:
170+
def deficit_max(
171+
deficit: pd.Series,
172+
startdate: pd.Timestamp | str = "2000-04-01",
173+
enddate: pd.Timestamp | str = "2000-09-30",
174+
) -> pd.Series:
167175
"""Compute the maximum cumulative deficit within a specified period.
168176
169177
This function computes the maximum cumulative deficit for a given
@@ -175,6 +183,12 @@ def deficit_max(deficit: pd.Series) -> pd.Series:
175183
----------
176184
deficit : pd.Series
177185
A pandas Series representing the deficit values over time.
186+
startdate : pd.Timestamp, optional
187+
The start date for the cumulative deficit calculation. Defaults to
188+
April 1st.
189+
enddate : pd.Timestamp, optional
190+
The end date for the cumulative deficit calculation. Defaults to
191+
September 30th.
178192
179193
Returns
180194
-------
@@ -183,18 +197,18 @@ def deficit_max(deficit: pd.Series) -> pd.Series:
183197
within the specified period, labeled as "Dmax".
184198
185199
"""
186-
startdate = pd.Timestamp("2000-04-01")
187-
enddate = pd.Timestamp("2000-09-30")
188200
cumdf = get_cumulative_deficit(
189201
deficit=deficit,
190-
startdate=startdate,
191-
enddate=enddate,
202+
startdate=pd.Timestamp(startdate),
203+
enddate=pd.Timestamp(enddate),
192204
allow_below_zero=False,
193205
)
194206
return cumdf.max().rename("Dmax")
195207

196208

197-
def deficit_apr1(deficit: pd.Series) -> pd.Series:
209+
def deficit_apr1(
210+
deficit: pd.Series, enddate: pd.Timestamp | str = "2000-09-30"
211+
) -> pd.Series:
198212
"""Compute the maximum change in cumulative deficit within a specified date range.
199213
200214
This function computes the cumulative deficit for the given deficit series
@@ -206,6 +220,9 @@ def deficit_apr1(deficit: pd.Series) -> pd.Series:
206220
deficit : pd.Series
207221
A pandas Series representing the deficit values. The index is expected
208222
to be datetime-like.
223+
enddate : pd.Timestamp, optional
224+
The end date for the cumulative deficit calculation. Defaults to
225+
September 30th.
209226
210227
Returns
211228
-------
@@ -214,19 +231,20 @@ def deficit_apr1(deficit: pd.Series) -> pd.Series:
214231
over the specified period, labeled as "DIapr1".
215232
216233
"""
217-
startdate = pd.Timestamp("2000-04-01")
218-
enddate = pd.Timestamp("2000-09-30")
219234
cumdf = get_cumulative_deficit(
220235
deficit=deficit,
221-
startdate=startdate,
222-
enddate=enddate,
236+
startdate=pd.Timestamp("2000-04-01"),
237+
enddate=pd.Timestamp(enddate),
223238
allow_below_zero=True,
224239
)
225240
return (cumdf.max() - cumdf.min()).rename("DIapr1")
226241

227242

228243
def deficit_gdd(
229-
deficit: pd.Series, temp: pd.Series, threshold: float = 440.0
244+
deficit: pd.Series,
245+
temp: pd.Series,
246+
threshold: float = 440.0,
247+
enddate: pd.Timestamp | str = "2000-09-30",
230248
) -> pd.Series:
231249
"""Compute the maximum change in cumulative deficit.
232250
@@ -240,8 +258,11 @@ def deficit_gdd(
240258
temp : pd.Series
241259
A pandas Series representing the daily temperature values.
242260
threshold : float, optional
243-
The temperature sum GDD threshold to determine the starting date for the calculation.
244-
Defaults to 440.0.
261+
The temperature sum GDD threshold to determine the starting date for
262+
the calculation. Defaults to 440.0 degrees Celsius.
263+
enddate : pd.Timestamp, optional
264+
The end date for the cumulative deficit calculation. Defaults to
265+
September 30th, 2000.
245266
246267
Returns
247268
-------
@@ -252,17 +273,18 @@ def deficit_gdd(
252273
"""
253274
temp = validate_series(temp)
254275
startdate = get_yearly_temp_date(temp=temp, threshold=threshold)
255-
enddate = pd.Timestamp("2000-09-30")
256276
cumdf = get_cumulative_deficit(
257277
deficit=deficit,
258278
startdate=startdate,
259-
enddate=enddate,
279+
enddate=pd.Timestamp(enddate),
260280
allow_below_zero=True,
261281
)
262282
return (cumdf.max() - cumdf.min()).rename("DIgdd")
263283

264284

265-
def deficit_wet(deficit: pd.Series) -> pd.Series:
285+
def deficit_wet(
286+
deficit: pd.Series, enddate: pd.Timestamp | str = "2000-09-30"
287+
) -> pd.Series:
266288
"""Compute the maximum change in cumulative deficit for a specified period.
267289
268290
This function computes the maximum change in cumulative deficit from
@@ -274,6 +296,9 @@ def deficit_wet(deficit: pd.Series) -> pd.Series:
274296
----------
275297
deficit : pd.Series
276298
A pandas Series representing the deficit values over time.
299+
enddate : pd.Timestamp, optional
300+
The end date for the cumulative deficit calculation. Defaults to
301+
September 30th, 2000.
277302
278303
Returns
279304
-------
@@ -282,12 +307,10 @@ def deficit_wet(deficit: pd.Series) -> pd.Series:
282307
for the specified period, labeled as "DIwet".
283308
284309
"""
285-
startdate = pd.Timestamp("2000-01-01")
286-
enddate = pd.Timestamp("2000-09-30")
287310
cumdf = get_cumulative_deficit(
288311
deficit=deficit,
289-
startdate=startdate,
290-
enddate=enddate,
312+
startdate=pd.Timestamp("2000-01-01"),
313+
enddate=pd.Timestamp(enddate),
291314
allow_below_zero=True,
292315
)
293316
return (cumdf.max() - cumdf.min()).rename("DIwet")

0 commit comments

Comments
 (0)