John
John

Reputation: 497

Pandas conditional on index column

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?

Upvotes: 5

Views: 15453

Answers (1)

jezrael
jezrael

Reputation: 863056

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

Upvotes: 5

Related Questions