Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compare type 'Timestamp' with type 'int'

Tags:

python

pandas

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

like image 642
nipy Avatar asked Aug 21 '16 20:08

nipy


1 Answers

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

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')
like image 94
Kris Avatar answered Sep 20 '22 14:09

Kris