I have a dataframe with multi index as follows
arrays = [
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.DataFrame(np.random.randn(8), index=index).T
which looks like this
bar baz foo qux
one two one two one two one two
0 -0.144135 0.625481 -2.139184 -1.066893 -0.123791 -1.058165 0.495627 -0.654353
to which the documentation says to index in the following way
df.loc[:, (slice("bar", "two"), ...)]
and so I do
s.loc[:, (slice("bar", "two"):(slice("baz", "two"))]
which gives me a SyntaxError.
Cell In[98], line 3
s.loc[:, (slice("bar", "two"):(slice("baz", "two")))]
^
SyntaxError: invalid syntax
In my specific use-case [albeit beyond the scope of this question], the level 1 indices are of type timestamp [Year], but I figure the answer should be the same. What is the proper way to access a range of multi-indexed items via a multi-index column?