Reputation: 347
I'm trying to locate the indexes of a variable where a condition is given.
df.loc[df.variable[mask].diff() == 1]
But when I write it, it returns me an odd error
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
the variable
is a series of boolean values and the mask
is just an interval like this:
mask = df.time_var.dt.date == datetime.date(2019, 10, 22)
what am I doing wrong?
Upvotes: 1
Views: 128
Reputation: 2719
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4], [5, 6]])
print(df)
# 0 1
# 0 1 2
# 1 3 4
# 2 5 6
print(df.loc[pd.Series([True, False, True])])
# 0 1
# 0 1 2
# 2 5 6
print(df.loc[pd.Series([True, False])])
# Traceback (most recent call last):
# pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
df.variable[mask].diff() == 1
is boolean Series which has different length than df
so pandas can not decide which rows to choose. You probably can use this:
df.loc[(df.variable[mask].diff() == 1).index]
Upvotes: 1