Trying to select rows for which the index label is a date in January 2016:
import numpy as np
import pandas as pd
index = pd.date_range('2016-01-28', freq='d', periods=6)
columns=list('A')
values = np.random.randint(1,10, size=(len(index), len(columns)))
df = pd.DataFrame(values, index=index, columns=columns)
print(df[df.index=='2016-01'])
Indeed this doesn’t work:
Empty DataFrame
Columns: [A]
Index: []
I could use df[(df.index>='2016-01') & (df.index<'2016-02')]
or perform a double comparison on year and month using the .dt
accessor, but this is way more verbose and not really scalable.
What is the syntax for a DateTime comparison ignoring the day attribute, e.g. df.index=='2016-02-*'
?
1