Skip to content

Commit

Permalink
keep supporting older pandas versions
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonk committed Mar 12, 2024
1 parent e34655e commit c3a9f39
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "A simple Python package to calculate drought indices for time ser
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.9"
dependencies = ["numpy", "scipy", "matplotlib", "pandas>=2.1.0"]
dependencies = ["numpy", "scipy", "matplotlib", "pandas"]
classifiers = [
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.9',
Expand Down
19 changes: 6 additions & 13 deletions src/spei/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
from typing import Union

from numpy import array, nan
from pandas import (
DataFrame,
DatetimeIndex,
Grouper,
Index,
Series,
Timedelta,
concat,
infer_freq,
to_datetime,
)
from pandas import DataFrame, DatetimeIndex, Grouper, Index, Series, Timedelta
from pandas import __version__ as pd_version
from pandas import concat, infer_freq, to_datetime


def validate_series(series: Series) -> Series:
Expand Down Expand Up @@ -72,7 +64,7 @@ def infer_frequency(index: Union[Index, DatetimeIndex]) -> str:
logging.info(
"Could not infer frequency from index, using monthly frequency instead"
)
inf_freq = "ME"
inf_freq = "ME" if pd_version >= "2.1.0" else "M"
else:
logging.info(f"Inferred frequency '{inf_freq}' from index")

Expand All @@ -87,7 +79,8 @@ def group_yearly_df(series: Series) -> DataFrame:
"""Group series in a DataFrame with date (in the year 2000) as index and year as columns."""
strfstr: str = "%m-%d %H:%M:%S"
grs = {}
for year_timestamp, gry in series.groupby(Grouper(freq="YE")):
freq = "YE" if pd_version >= "2.1.0" else "Y"
for year_timestamp, gry in series.groupby(Grouper(freq=freq)):
index = validate_index(gry.index)
gry.index = to_datetime(
"2000-" + index.strftime(strfstr), format="%Y-" + strfstr
Expand Down

0 comments on commit c3a9f39

Please sign in to comment.