ValueError: cannot set using a list-like indexer with a different length than the value
filipematos95 opened this issue · 3 comments
Hello,
I am having the following error when running si.spei() with a series without any missing value and the index as datetimeindex: ValueError: cannot set using a list-like indexer with a different length than the value
Here is the traceback:
`File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/spei/si.py:170, in spei(series, dist)
143 def spei(series: Series, dist: ContinuousDist = fisk) -> Series:
144 """Method to compute the Standardized Precipitation Evaporation Index
145 [spei_2010]_.
146
(...)
167 Journal of Climate, 23, 1696-1718, 2010.
168 """
--> 170 return compute_si_ppf(series=series, dist=dist)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/spei/si.py:56, in compute_si_ppf(series, dist, index, sgi, prob_zero)
54 cdf = compute_cdf_nsf(data=data)
55 ppf = norm.ppf(cdf)
---> 56 si.loc[data.index] = ppf
57 return si
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/indexing.py:849, in _LocationIndexer.setitem(self, key, value)
846 self._has_valid_setitem_indexer(key)
848 iloc = self if self.name == "iloc" else self.obj.iloc
--> 849 iloc._setitem_with_indexer(indexer, value, self.name)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/indexing.py:1837, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
1835 self._setitem_with_indexer_split_path(indexer, value, name)
1836 else:
-> 1837 self._setitem_single_block(indexer, value, name)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/indexing.py:2077, in _iLocIndexer._setitem_single_block(self, indexer, value, name)
2074 self.obj._check_is_chained_assignment_possible()
2076 # actually do the set
-> 2077 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
2078 self.obj._maybe_update_cacher(clear=True, inplace=True)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/internals/managers.py:394, in BaseBlockManager.setitem(self, indexer, value)
389 if using_copy_on_write() and not self._has_no_reference(0):
390 # if being referenced -> perform Copy-on-Write and clear the reference
391 # this method is only called if there is a single block -> hardcoded 0
392 self = self.copy()
--> 394 return self.apply("setitem", indexer=indexer, value=value)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/internals/managers.py:352, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
350 applied = b.apply(f, **kwargs)
351 else:
--> 352 applied = getattr(b, f)(**kwargs)
353 result_blocks = extend_blocks(applied, result_blocks)
355 out = type(self).from_blocks(result_blocks, self.axes)
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/internals/blocks.py:1041, in Block.setitem(self, indexer, value, using_cow)
1038 values = values.T
1040 # length checking
-> 1041 check_setitem_lengths(indexer, value, values)
1043 value = extract_array(value, extract_numpy=True)
1044 try:
File ~/miniconda3/envs/FluxNet/lib/python3.8/site-packages/pandas/core/indexers/utils.py:168, in check_setitem_lengths(indexer, value, values)
162 indexer = np.array(indexer)
163 if not (
164 isinstance(indexer, np.ndarray)
165 and indexer.dtype == np.bool_
166 and indexer.sum() == len(value)
167 ):
--> 168 raise ValueError(
169 "cannot set using a list-like indexer "
170 "with a different length than the value"
171 )
172 if not len(indexer):
173 no_op = True
ValueError: cannot set using a list-like indexer with a different length than the value`
I am running Python 3.8, Numpy 1.24.3, Pandas 2.0.3 and Scipy 1.10.1
I cannot solve this if you don't share the input series.
Here goes the series,
thanks
Your input series has multiple pe values for the same date:
'2019-11-01', '2019-12-01', '2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01', '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01'
Please make sure your input is correct. You can remove them with: series[~series.index.duplicated(keep ="first/last")].