Let's say I have the following DataFrame:
df = pd.DataFrame({'item': ['Subway', 'Pasta', 'Chipotle'],
'cost': [10, 5, 9],
'date': ['2017-12-01', '2017-11-01', '2017-10-01']})
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
I'm able to get all items in 2017-10
(only one item in this case):
print(df.set_index('date')['2017-10'])
According to the pandas documentation and this SO answer, I should be able to get all items from 2017-10
to 2017-11
(2 items in this case) with the following command but I'm getting an empty DataFrame:
print(df.set_index('date')['2017-10':'2017-11'])
Any idea what I'm doing wrong here (I'm using pandas version 0.21.0
)?
Moreover, is there an efficient way I can get all items in 2017-10
and 2017-12
(skipping 2017-11
)? I've come up with the following solution but I shouldn't have to create new columns like so:
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
print(df[((df.month==10) & (df.year==2017) | (df.month==12) & (df.year==2017))])