rabbito
rabbito

Reputation: 73

Conditional selection on multiindex datetime

I'm working on a multiindex dataframe with the level 3 index being a datetime series. This datetime series are basically random dates with no particular order (they are dates of when a building construction was completed). How do i perform conditional selection using this datetime index? e.g. >=2014-1-1

(My clumsy solution so far is to convert this level 3 index into a column, then performing conditional selection based on this column e.g. df['Date'].dt.year>2013)

Upvotes: 0

Views: 731

Answers (3)

Phik
Phik

Reputation: 434

First, sort your index, with sort_index (eventually just for your level=3, depending on how you have generated the data)

Then using IndexSlice df.loc[pd.IndexSlice: [:, :, :, dt.datetime(2014,1,1):, :]

Upvotes: 2

BENY
BENY

Reputation: 323226

You can try something like :

df[df.index.get_level_values(2)>'2014-01-01']

Upvotes: 1

Josh D
Josh D

Reputation: 894

Use pandas.IndexSlice. I learned to use it from the end of this post.

Upvotes: 1

Related Questions