pandas ValueError:索引必须单调递增或递减:添加毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42510412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:05:44  来源:igfitidea点击:

ValueError: index must be monotonic increasing or decreasing : Adding milliseconds

pythonpandas

提问by Burf2000

So I had everything working with my data until I added milliseconds to the date/time field.

因此,在我将毫秒添加到日期/时间字段之前,我的所有数据都可以使用我的数据。

selected = selected['2017-02-20 16:10:05':'2017-02-20 16:20:06']is my statement to filter my data_frame**

selected = selected['2017-02-20 16:10:05':'2017-02-20 16:20:06']是我过滤数据框的语句**

The below data works when milliseconds are 0000

以下数据在毫秒为 0000 时有效

enter image description here

在此处输入图片说明

The below data errors when milliseconds are properly populated

正确填充毫秒时出现以下数据错误

enter image description here

在此处输入图片说明

Update: I have tried

更新:我试过了

selected.sort_index()
selected.ix['2017-02-20 16:10:05':'2017-02-20 16:20:06']
selected.ix[datetime.date(year=2017,month=2,day=20):datetime.date(year=2017,month=2,day=21)] 

TypeError: '>' not supported between instances of 'str' and 'datetime.date'

类型错误:“str”和“datetime.date”的实例之间不支持“>”

Then I get the following

然后我得到以下信息

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in get_slice_bound(self, label, side, kind)
   3121             try:
-> 3122                 return self._searchsorted_monotonic(label, side)
   3123             except ValueError:

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in _searchsorted_monotonic(self, label, side)
   3087 
-> 3088         raise ValueError('index must be monotonic increasing or decreasing')
   3089 

ValueError: index must be monotonic increasing or decreasing

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-43-a7bf11322082> in <module>()
     11 selected = selected.query('y_acc < 0.4 and z_acc > -0.4')
     12 selected = selected.query('z_acc < 0.4 and z_acc > -0.4')
---> 13 selected = selected['2017-02-20 16:10:05':'2017-02-20 16:20:06']
     14 
     15 #%pylab inline

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2045 
   2046         # see if we can slice the rows
-> 2047         indexer = convert_to_index_sliceable(self, key)
   2048         if indexer is not None:
   2049             return self._getitem_slice(indexer)

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in convert_to_index_sliceable(obj, key)
   1772     idx = obj.index
   1773     if isinstance(key, slice):
-> 1774         return idx._convert_slice_indexer(key, kind='getitem')
   1775 
   1776     elif isinstance(key, compat.string_types):

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in _convert_slice_indexer(self, key, kind)
   1236         else:
   1237             try:
-> 1238                 indexer = self.slice_indexer(start, stop, step, kind=kind)
   1239             except Exception:
   1240                 if is_index_slice:

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in slice_indexer(self, start, end, step, kind)
   2995         """
   2996         start_slice, end_slice = self.slice_locs(start, end, step=step,
-> 2997                                                  kind=kind)
   2998 
   2999         # return a slice

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in slice_locs(self, start, end, step, kind)
   3174         start_slice = None
   3175         if start is not None:
-> 3176             start_slice = self.get_slice_bound(start, 'left', kind)
   3177         if start_slice is None:
   3178             start_slice = 0

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in get_slice_bound(self, label, side, kind)
   3123             except ValueError:
   3124                 # raise the original KeyError
-> 3125                 raise err
   3126 
   3127         if isinstance(slc, np.ndarray):

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in get_slice_bound(self, label, side, kind)
   3117         # we need to look up the label
   3118         try:
-> 3119             slc = self.get_loc(label)
   3120         except KeyError as err:
   3121             try:

/usr/local/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   2134                 return self._engine.get_loc(key)
   2135             except KeyError:
-> 2136                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2137 
   2138         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)()

KeyError: '2017-02-20 16:10:05'

回答by Vikash Singh

To filter by datetime index

按日期时间索引过滤

selected_subset = selected.ix['2017-02-20 16:10:05':'2017-02-20 16:20:06']

reference: filtering pandas dataframes on dates

参考:过滤日期上的Pandas数据框

I tried this and it worked for me:

我试过这个,它对我有用:

import pandas as pd
import numpy as np
from datetime import datetime

date_time_1 = datetime.now()

date_time_2 = datetime.now()

data = [{'time': date_time_1, 'val': 1}, {'time': date_time_2, 'val':2}]

df = pd.DataFrame.from_dict(data, orient='columns')

df.set_index(['time'], inplace=True)

df = df.sort_index() # This is an important step if 'time' field is not sorted
df


                            val
time    
2017-02-28 19:19:00.777225  1
2017-02-28 19:19:04.605302  2

df.ix['2017-02-28 19:19:00':'2017-02-28 19:19:03']


                            val
time    
2017-02-28 19:19:00.777225  1

回答by Burf2000

The issue turned out to be it thought the date index was a string. I found this out using:

问题原来是它认为日期索引是一个字符串。我发现这一点使用:

selected.ix[datetime.date(year=2017,month=2,day=20):datetime.date(year=2017,month=2,day=21)]

Which returned the error: TypeError: '>' not supported between instances of 'str' and 'datetime.date'

返回错误:TypeError: '>' not supported between 'str' and 'datetime.date'

I then added parse_dates=Truero my read_csv commandand that fixed the issue.

然后我添加了parse_dates=Truero my read_csv commandand 解决了这个问题。

I could then just use

然后我可以使用

selected = selected['2017-02-20 16:10:05':'2017-02-20 16:11:06']