TAGS :Viewed: 6 - Published at: a few seconds ago

[ Select and Count items datetime64[ns] in Pandas ]

I have a pd as following:

df=
              CreationDate
0  2008-11-04 13:21:39
1  2008-11-24 23:50:29
2  2009-05-18 07:46:48
3  2009-09-22 06:03:34
4  2009-11-07 07:28:21
5  2009-12-08 14:29:56
6  2010-01-12 06:42:00
7  2010-05-20 17:56:01
8  2010-06-05 19:27:02
9  2010-07-16 19:52:22
10 2010-07-25 16:27:13
11 2010-07-25 21:59:56
CreationDate    datetime64[ns]
dtype: object

Now I need select and count Items in a specific period, for example,I need the items and count them form '2008-11' to '2009-12'. How to select and count? thanks.

Answer 1


You need to first set CreationDate as index and then use the slice '2008-11':'2009-12' to select.

print(df)

          CreationDate       A
0  2008-11-04 13:21:39  1.7641
1  2008-11-24 23:50:29  0.4002
2  2009-05-18 07:46:48  0.9787
3  2009-09-22 06:03:34  2.2409
4  2009-11-07 07:28:21  1.8676
5  2009-12-08 14:29:56 -0.9773
6  2010-01-12 06:42:00  0.9501
7  2010-05-20 17:56:01 -0.1514
8  2010-06-05 19:27:02 -0.1032
9  2010-07-16 19:52:22  0.4106
10 2010-07-25 16:27:13  0.1440
11 2010-07-25 21:59:56  1.4543

# processing
# ==========================
df.set_index('CreationDate')['2008-11':'2009-12']

                          A
CreationDate               
2008-11-04 13:21:39  1.7641
2008-11-24 23:50:29  0.4002
2009-05-18 07:46:48  0.9787
2009-09-22 06:03:34  2.2409
2009-11-07 07:28:21  1.8676
2009-12-08 14:29:56 -0.9773