@@ -237,6 +237,11 @@ def __init__(
237237
238238 self ._stations = pd .Index (loc )
239239
240+ # Check period
241+ if (start and end ) and (end - start != 29 or end %
242+ 10 != 0 or end >= datetime .now ().year ):
243+ raise ValueError ('Invalid reference period' )
244+
240245 # Set period
241246 self ._start = start
242247 self ._end = end
@@ -263,28 +268,35 @@ def normalize(self):
263268 if self .count () == 0 :
264269 warn ('Pointless normalization of empty DataFrame' )
265270
266- else :
267- # Go through list of weather stations
268- for station in temp ._stations :
269- # Get periods
271+ # Go through list of weather stations
272+ for station in temp ._stations :
273+ # The list of periods
274+ periods : pd .Index = pd .Index ([])
275+ # Get periods
276+ if self .count () > 0 :
270277 periods = temp ._data [temp ._data .index .get_level_values (
271278 'station' ) == station ].index .unique ('end' )
272- # Go through all periods
273- for period in periods :
274- # Create DataFrame
275- df = pd .DataFrame (
276- columns = temp ._columns [temp ._first_met_col :])
277- # Populate index columns
278- df ['month' ] = range (1 , 13 )
279- df ['station' ] = station
280- df ['start' ] = period - 29
281- df ['end' ] = period
282- # Set index
283- df .set_index (
284- ['station' , 'start' , 'end' , 'month' ], inplace = True )
285- # Merge data
286- temp ._data = pd .concat ([temp ._data , df ], axis = 0 ).groupby (
287- ['station' , 'start' , 'end' , 'month' ], as_index = True ).first ()
279+ elif periods .size == 0 and self ._end :
280+ periods = pd .Index ([self ._end ])
281+ # Go through all periods
282+ for period in periods :
283+ # Create DataFrame
284+ df = pd .DataFrame (
285+ columns = temp ._columns [temp ._first_met_col :])
286+ # Populate index columns
287+ df ['month' ] = range (1 , 13 )
288+ df ['station' ] = station
289+ df ['start' ] = period - 29
290+ df ['end' ] = period
291+ # Set index
292+ df .set_index (
293+ ['station' , 'start' , 'end' , 'month' ], inplace = True )
294+ # Merge data
295+ temp ._data = pd .concat ([temp ._data , df ], axis = 0 ).groupby (
296+ ['station' , 'start' , 'end' , 'month' ], as_index = True ).first ()
297+
298+ # None -> NaN
299+ temp ._data = temp ._data .fillna (np .NaN )
288300
289301 # Return class instance
290302 return temp
@@ -306,7 +318,7 @@ def fetch(self) -> pd.DataFrame:
306318 temp = temp .reset_index (level = 'station' , drop = True )
307319
308320 # Remove start & end year if period is set
309- if self ._start and self ._end :
321+ if self ._start and self ._end and self . count () > 0 :
310322 temp = temp .reset_index (level = 'start' , drop = True )
311323 temp = temp .reset_index (level = 'end' , drop = True )
312324
0 commit comments