Pandas conditionally for index column
I have a pandas DataFrame and set the index as a DateTime column:
data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')
which I need for data interpolation. However, this indexing later prevents me from doing
data = data[pandas.to_datetime (data['DateTime']) <= cutoff]
where cutoff
is some datetime. How can i do this?
+3
source to share
1 answer
It seems you need .index
for comparison DatetimeIndex
:
data['DateTime'] = pandas.to_datetime (data['DateTime']) data = data.set_index('DateTime') data = data[data.index <= cutoff]
Also sorted to DatetimeIndex
use loc
:
data1 = data1.loc[:cutoff]
Example:
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
+2
source to share