pandas 无法将“时间戳”类型与“int”类型进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39068813/
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 01:51:58  来源:igfitidea点击:

Cannot compare type 'Timestamp' with type 'int'

pythonpandas

提问by ade1e

When running the following code:

运行以下代码时:

for row,hit in hits.iterrows():
    forwardRows = data[data.index.values > row];

I get this error:

我收到此错误:

TypeError: Cannot compare type 'Timestamp' with type 'int'

If I look into what is being compared here I have these variables:

如果我查看这里比较的内容,我有这些变量:

type(row)
pandas.tslib.Timestamp

row
Timestamp('2015-09-01 09:30:00')

is being compared with:

正在与:

type(data.index.values[0])
numpy.datetime64

data.index.values[0]
numpy.datetime64('2015-09-01T10:30:00.000000000+0100')

I would like to understand whether this is something that can be easily fixed or should I upload a subset of my data? thanks

我想了解这是否可以轻松修复,还是应该上传我的数据子集?谢谢

采纳答案by piRSquared

When using valuesyou put it into numpyworld. Instead, try

使用时,values您将其放入numpy世界。相反,尝试

for row,hit in hits.iterrows():
    forwardRows = data[data.index > row];

回答by Kris

Although this isn't a direct answer to your question, I have a feeling that this is what you're looking for: pandas.DataFrame.truncate

虽然这不是您问题的直接答案,但我有一种感觉,这就是您要寻找的:pandas.DataFrame.truncate

You could use it as follows:

您可以按如下方式使用它:

for row, hit in hits.iterrows():
    forwardRows = data.truncate(before=row)


Here's a little toy example of how you might use it in general:

这是一个关于如何使用它的小玩具示例:

import pandas as pd

# let's create some data to play with
df = pd.DataFrame(
    index=pd.date_range(start='2016-01-01', end='2016-06-01', freq='M'),
    columns=['x'],
    data=np.random.random(5)
)

# example: truncate rows before Mar 1
df.truncate(before='2016-03-01')

# example: truncate rows after Mar 1
df.truncate(after='2016-03-01')