Skip to content

Commit

Permalink
add check for duplicated index
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonk committed Mar 7, 2024
1 parent ce5cf4d commit e2730e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/spei/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def validate_index(index: Index) -> DatetimeIndex:
)
index = DatetimeIndex(to_datetime(index))

if index.has_duplicates:
msg = (
"Duplicated indices found. Please remove them. For instance by"
" using `series = "
"series.loc[~series.index.duplicated(keep='first/last')]`"
)
logging.error(msg)
raise ValueError(msg)

return index


Expand Down
20 changes: 17 additions & 3 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import logging

import pytest
from pandas import DataFrame, Series, to_datetime
from pandas import DataFrame, DatetimeIndex, Series, Timestamp, to_datetime

from spei.utils import validate_index, validate_series


def test_validate_index(caplog) -> None:
caplog.set_level(logging.INFO)
series = Series([1, 2, 3], index=["2018", "2019", "2020"])
series = Series([1.0, 2.0, 3.0], index=["2018", "2019", "2020"])
validate_index(series.index)
msg = (
f"Expected the index to be a DatetimeIndex. Automatically converted "
f"{type(series.index)} using pd.to_datetime(Index)\n"
)
print(f"{caplog.text=}")
assert msg in caplog.text


def test_validate_index_duplicated(caplog) -> None:
caplog.set_level(logging.ERROR)
series = Series(
[1.0, 1.0],
index=DatetimeIndex([Timestamp("2000-01-01"), Timestamp("2000-01-01")]),
)
with pytest.raises(ValueError):
validate_index(series.index)
msg = (
"Duplicated indices found. Please remove them. For instance by using"
"`series = series.loc[~series.index.duplicated(keep='first/last')]`"
)
assert msg in caplog.text


def test_validate_series() -> None:
with pytest.raises(TypeError):
validate_series([1, 2, 3])
Expand Down

0 comments on commit e2730e2

Please sign in to comment.