Reputation: 21971
In the following dataframe, datetime column is the index, and I want to extract values from 2015 to 2020.
df[(df.index>=2015) & (df.index<=2020)]
However, when I use the command above, it only extracts values ranging from Jan 1st 2015 to Jan 1st 2020. I would like it to extract all values for 2020. Any suggestions?
datetime NMN
3/21/2012 0.5221
3/22/2012 0.5089
3/23/2012 0.4782
3/24/2012 0.4587
3/25/2012 0.5012
3/26/2012 0.5468
3/27/2012 0.5949
3/28/2012 0.6279
3/29/2012 0.682
...
12/21/2022 0.6377
12/22/2022 0.5864
12/23/2022 0.559
12/24/2022 0.5429
12/25/2022 0.5293
12/26/2022 0.5394
12/27/2022 0.6248
12/28/2022 0.6565
12/29/2022 0.6799
12/30/2022 0.5756
12/31/2022 0.5475
Upvotes: 0
Views: 503
Reputation: 109546
specify the end of the year, use quotations, and also use ix to select index values:
df.ix['2012':'2022-12-31']
NMN
datetime
2012-03-21 0.5221
2012-03-22 0.5089
2012-03-23 0.4782
2012-03-24 0.4587
2012-03-25 0.5012
2012-03-26 0.5468
2012-03-27 0.5949
2012-03-28 0.6279
2012-03-29 0.6820
2022-12-21 0.6377
2022-12-22 0.5864
2022-12-23 0.5590
2022-12-24 0.5429
2022-12-25 0.5293
2022-12-26 0.5394
2022-12-27 0.6248
2022-12-28 0.6565
2022-12-29 0.6799
2022-12-30 0.5756
2022-12-31 0.5475
The following also works just fine, producing the same output as above:
df.ix['2012':'2022']
Upvotes: 1