5

I have a pandas DataFrame, and set index to be the DateTime column:

data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')

which I need to interpolate the data. However, this indexing later prevents me from doing

data = data[pandas.to_datetime (data['DateTime']) <= cutoff]

where cutoff is some datetime. How can I go about this?

0

1 Answer 1

5

It seems you need .index for compare DatetimeIndex:

data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')
data = data[data.index <= cutoff]

Also is sorted DatetimeIndex use loc:

data1 = data1.loc[:cutoff]

Sample:

rng = pd.date_range('2017-04-03', periods=10)
data = pd.DataFrame({'a': range(10)}, index=rng)  
print (data)
            a
2017-04-03  0
2017-04-04  1
2017-04-05  2
2017-04-06  3
2017-04-07  4
2017-04-08  5
2017-04-09  6
2017-04-10  7
2017-04-11  8
2017-04-12  9

cutoff = '2017-04-08'
data1 = data[data.index <= cutoff]
print (data1)
            a
2017-04-03  0
2017-04-04  1
2017-04-05  2
2017-04-06  3
2017-04-07  4
2017-04-08  5

data1 = data1.loc[:cutoff]
print (data1)
            a
2017-04-03  0
2017-04-04  1
2017-04-05  2
2017-04-06  3
2017-04-07  4
2017-04-08  5

Thanks piRSquared:

data1 = data1[:cutoff]
print (data1)
            a
2017-04-03  0
2017-04-04  1
2017-04-05  2
2017-04-06  3
2017-04-07  4
2017-04-08  5
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing. It would have taken me days and I still wouldn't have tried this. I will accept the answer in 10 mins.
Thanks, @piRSquared. Is there a performance difference between both methods?
@John no. they should be about the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.