I have a dataframe that looks like that:
I am trying to slice for a particular symbol and as from a date. doing
df.loc[symbol]
I obtain that dataframe:
So it goes from 2021-3-1 to 2023-1-31. I have a variable "date" = Timestamp('2021-03-01 00:00:00'), so the first date.
When I try to slice it like that:
df.loc[pd.IndexSlice[symbol, date:]]
I get the error:
KeyError: Timestamp('2021-03-01 00:00:00')
same for:
df.loc[pd.IndexSlice[symbol, :date]]
When I print
df.index[0]
('BOTTO/WETH', Timestamp('2023-01-31 00:00:00'))
to prove that the second level is indeed a timetamp.
Any idea how I could do please?
For me working well if added :
for select all columns before last ]
:
df = pd.DataFrame(
np.random.randn(20, 1),
columns=["A"],
index=pd.MultiIndex.from_product(
[["a", "b"], pd.date_range("20210228", periods=10, freq="D")]
),
)
symbol = 'a'
date = pd.Timestamp('2021-03-01 00:00:00')
df = df.loc[pd.IndexSlice[symbol, date:], :]
print (df)
A
a 2021-03-01 0.228366
2021-03-02 -1.092949
2021-03-03 -1.086836
2021-03-04 -1.155692
2021-03-05 0.382733
2021-03-06 -1.653822
2021-03-07 0.978806
2021-03-08 -1.537005
2021-03-09 -0.200136