martinvonk/SPEI

Problems to run the code with my own data.

luizfiscina opened this issue · 1 comments

Hello, how are you?

I'm having trouble running the code with my own data. Could you help me? When I run the cell related to the calculation of SPEI, I get the following error:

ValueError: zero-size array to reduction operation minimum which has no identity

To check if the error is a problem with my database, I exported your precipitation data and imported it again for testing. When I do this, the same error occurs as well. Do you have any clues as to why this error is happening?

Details of my code below:

#%% SPEI CODE

import spei as si # si for standardized index
import pandas as pd
import scipy.stats as scs
import matplotlib.pyplot as plt

Load Time Series

fileurl = "https://raw.githubusercontent.com/pastas/pastas/master/doc/examples/data"
prec = (
pd.read_csv(f"{fileurl}/rain_nb1.csv", index_col=0, parse_dates=True)
.squeeze()
.multiply(1e3)
) # to mm
evap = (
pd.read_csv(f"{fileurl}/evap_nb1.csv", index_col=0, parse_dates=True)
.squeeze()
.multiply(1e3)
) # to mm
head = pd.read_csv(f"{fileurl}/head_nb1.csv", index_col=0, parse_dates=True).squeeze()

fig, ax = plt.subplots(3, 1, figsize=(12, 8), sharex=True)
prec.plot(ax=ax[0], legend=True, grid=True)
evap.plot(ax=ax[1], color="C1", legend=True, grid=True)
head.plot(ax=ax[2], color="k", legend=True, grid=True);

Calculate SPI

f = 90 # days
series = prec.rolling(f, min_periods=f).sum().dropna()
series

spi3_gamma = si.spi(series, dist=scs.gamma, fit_freq="M")
spi3_gamma

tmin, tmax = pd.to_datetime(["1994", "1998"])
plt.figure(figsize=(8, 4))
spi3_gamma.plot(label="gamma")
plt.xlim(tmin, tmax)
plt.legend()
plt.grid()
plt.title("SPI");

f, ax = plt.subplots(1, 1, figsize=(12, 8))
si.plot.si(spi3_gamma, ax=ax)
ax.set_xlim(pd.to_datetime(["1994", "1998"]))
ax.grid()
ax.set_ylabel("SPI3", fontsize=14)
plt.show()

#%% SPEI CODE WITH MY DATA

prec.to_csv('prec.csv')

rainfall_teste = pd.read_csv("C:/Users/luizf/OneDrive/Doutorado POLI-USP/10- Análise de Dados Pluviométricos_R01/SPI/prec.csv", sep=',')

rainfall_teste = rainfall_teste.set_index('date')['rain']

rainfall_teste.to_csv('rainfall_teste')

Calculate SPI

f = 90 # days
series_rainfall_teste = rainfall_teste.rolling(f, min_periods=f).sum().dropna()
series_rainfall_teste

series_rainfall_teste.info()

spi3_gamma_rainfall_teste = si.spi(series_rainfall_teste, dist=scs.gamma, fit_freq="M")
spi3_gamma_rainfall_teste

ValueError: zero-size array to reduction operation minimum which has no identity

No I don't know why that is happening without your data. Maybe you can share the data. Most of the time these issues are data related.