无法解决异常:“ ValueError:使用带UTC偏移量的日期字符串建立索引时,索引必须了解时区”

问题描述 投票:1回答:1

我有一个类似这样的时间序列:

enter image description here

y.index = pd.to_datetime(y.index)

y.index = y.index.tz_localize(None)

[当我尝试使用以下表达式对行进行切片时,出现以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-677-f1b3153cb92b> in <module>
----> 1 y['2020-02-24 10-11-12':]

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    863             key = check_bool_indexer(self.index, key)
    864 
--> 865         return self._get_with(key)
    866 
    867     def _get_with(self, key):

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\series.py in _get_with(self, key)
    868         # other: fancy integer or otherwise
    869         if isinstance(key, slice):
--> 870             return self._slice(key)
    871         elif isinstance(key, ABCDataFrame):
    872             raise TypeError(

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\series.py in _slice(self, slobj, axis, kind)
    818 
    819     def _slice(self, slobj: slice, axis: int = 0, kind=None):
--> 820         slobj = self.index._convert_slice_indexer(slobj, kind=kind or "getitem")
    821         return self._get_values(slobj)
    822 

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\base.py in _convert_slice_indexer(self, key, kind)
   2943             indexer = key
   2944         else:
-> 2945             indexer = self.slice_indexer(start, stop, step, kind=kind)
   2946 
   2947         return indexer

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\datetimes.py in slice_indexer(self, start, end, step, kind)
    806 
    807         try:
--> 808             return Index.slice_indexer(self, start, end, step, kind=kind)
    809         except KeyError:
    810             # For historical reasons DatetimeIndex by default supports

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\base.py in slice_indexer(self, start, end, step, kind)
   4675         slice(1, 3)
   4676         """
-> 4677         start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind)
   4678 
   4679         # return a slice

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\base.py in slice_locs(self, start, end, step, kind)
   4888         start_slice = None
   4889         if start is not None:
-> 4890             start_slice = self.get_slice_bound(start, "left", kind)
   4891         if start_slice is None:
   4892             start_slice = 0

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   4800         # For datetime indices label may be a string that has to be converted
   4801         # to datetime boundary according to its resolution.
-> 4802         label = self._maybe_cast_slice_bound(label, side, kind)
   4803 
   4804         # we need to look up the label

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\datetimes.py in _maybe_cast_slice_bound(self, label, side, kind)
    761             freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
    762             _, parsed, reso = parsing.parse_time_string(label, freq)
--> 763             lower, upper = self._parsed_string_to_bounds(reso, parsed)
    764             # lower, upper form the half-open interval:
    765             #   [parsed, parsed + 1 freq)

~\Anaconda3\envs\tf2\lib\site-packages\pandas\core\indexes\datetimes.py in _parsed_string_to_bounds(self, reso, parsed)
    569             if self.tz is None:
    570                 raise ValueError(
--> 571                     "The index must be timezone aware when indexing "
    572                     "with a date string with a UTC offset"
    573                 )

ValueError: The index must be timezone aware when indexing with a date string with a UTC offset

我出于可重复性目的提供了本系列的有限部分(json格式:

'{"1582539072500":1,"1582539073000":1,"1582539073500":1,"1582539074000":1,"1582539074500":1,"1582539075000":1,"1582539075500":1,"1582539076000":1,"1582539076500":1,"1582539077000":1,"1582539077500":1,"1582539078000":1,"1582539078500":1,"1582539080500":1,"1582539081000":1,"1582539081500":1,"1582539082000":1,"1582539082500":1,"1582539083000":1,"1582539083500":1,"1582539084000":1,"1582539084500":1,"1582539085000":1,"1582539085500":1,"1582539086000":1,"1582539086500":1,"1582539088500":1,"1582539089000":1,"1582539089500":1,"1582539090000":1,"1582539090500":1,"1582539091000":1,"1582539091500":1,"1582539092500":1,"1582539093000":1,"1582539093500":1,"1582539094000":1,"1582539094500":1,"1582539095000":1,"1582539095500":1,"1582539096000":1,"1582539097500":1,"1582539099500":1,"1582539101000":1,"1582539101500":1,"1582539104000":1,"1582539104500":1,"1582539105500":1,"1582539106000":1,"1582539109000":1}'

是什么导致错误,为什么我的操作不能解决该错误,我该怎么办?

python-3.x pandas slice datetimeindex
1个回答
0
投票

错误是“在使用带有UTC偏移量的日期字符串建立索引时,索引必须知道时区”。您有y['2020-02-24 10-11-12':],但其中有连字符。我重现了您的错误,并用冒号替换了时间部分的连字符,并使其能够在样本数据上运行。

y['2020-02-24 10:11:12':]应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.