junkdf:大熊貓日期時間切片:junkdf.ix [ '2015年8月3日': '2015年8月6日']不工作
rev
dtime
2015-08-03 20.45
2015-08-04 -2.57
2015-08-05 12.53
2015-08-06 -8.16
2015-08-07 -4.41
junkdf.reset_index()to_dict(」 REC「)
[{'dtime': datetime.date(2015, 8, 3), 'rev': 20.45},
{'dtime': datetime.date(2015, 8, 4), 'rev': -2.5699999999999994},
{'dtime': datetime.date(2015, 8, 5), 'rev': 12.53},
{'dtime': datetime.date(2015, 8, 6), 'rev': -8.16},
{'dtime': datetime.date(2015, 8, 7), 'rev': -4.41}]
junkdf.set_index('dtime',inplace=True)
爲什麼我不能做任何的日期時間切片一樣,在描述:
python-pandas-dataframe-slicing-by-date-conditions
junkdf [ '2015年8月3日':]
C:\Users\blah\Anaconda3\lib\site-packages\pandas\core\base.py in searchsorted(self, key, side, sorter)
1112 def searchsorted(self, key, side='left', sorter=None):
1113 # needs coercion on the key (DatetimeIndex does already)
-> 1114 return self.values.searchsorted(key, side=side, sorter=sorter)
1115
1116 _shared_docs['drop_duplicates'] = (
TypeError: unorderable types: datetime.date() > str()
junkdf.ix [ '2015年8月3日': '2015年8月6日']
C:\Users\blah\Anaconda3\lib\site-packages\pandas\core\base.py in searchsorted(self, key, side, sorter)
1112 def searchsorted(self, key, side='left', sorter=None):
1113 # needs coercion on the key (DatetimeIndex does already)
-> 1114 return self.values.searchsorted(key, side=side, sorter=sorter)
1115
1116 _shared_docs['drop_duplicates'] = (
TypeError: unorderable types: datetime.date() > str()
開始= junkdf.index.searchsorted(dt.datetime(2015,8,4))
C:\Users\blah\Anaconda3\lib\site-packages\pandas\core\base.py in searchsorted(self, key, side, sorter)
1112 def searchsorted(self, key, side='left', sorter=None):
1113 # needs coercion on the key (DatetimeIndex does already)
-> 1114 return self.values.searchsorted(key, side=side, sorter=sorter)
1115
1116 _shared_docs['drop_duplicates'] = (
TypeError: can't compare datetime.datetime to datetime.date))
但是,下面的作品,如果我用dt.date():
start = junkdf.index.searchsorted(dt.date(2015, 8, 4))
end = junkdf.index.searchsorted(dt.date(2015, 8, 6))
junkdf.ix[start:end]
rev
dtime
2015-08-04 -2.57
2015-08-05 12.53
UPDATE:
junkdf = df[['dtime','rev']].groupby((df.dtime).dt.date).sum().copy()
其中df[['dtime','rev']]
樣子:
dtime rev
0 2015-08-03 07:59:59 -0.18
1 2015-08-03 08:59:59 -0.11
2 2015-08-03 09:59:59 -0.29
3 2015-08-03 10:59:59 -0.08
4 2015-08-03 11:59:59 0.69
UPDATE2:
我想:
df[['dtime','rev']].head()
dtime rev
0 2015-08-03 07:59:59 -0.18
1 2015-08-03 08:59:59 -0.11
2 2015-08-03 09:59:59 -0.29
3 2015-08-03 10:59:59 -0.08
4 2015-08-03 11:59:59 0.69
df[['dtime','rev']].groupby(pd.TimeGrouper('D', key=df.dtime)).sum()
C:\Users\blah\Anaconda3\lib\site-packages\pandas\core\generic.py in __hash__(self)
804 def __hash__(self):
805 raise TypeError('{0!r} objects are mutable, thus they cannot be'
--> 806 ' hashed'.format(self.__class__.__name__))
807
808 def __iter__(self):
TypeError: 'Series' objects are mutable, thus they cannot be hashed
它正常工作對我來說。 'print(junkdf.index.dtype)'的輸出是什麼? – MaxU
print(junkdf.index.dtype)=對象 – codingknob
您的索引是字符串dtype。你必須先將它轉換爲datetime – MaxU