python - trouble selecting a period of dates in Pandas with < and > -
a simple question perhaps, it's first time working pandas , i'm having trouble slicing dataframes smaller ones based on dates.
so, have dataframe (named firstreadin
) looks (and thousands more rows):
date numbers megaball 0 1999-01-12 [5, 7, 9, 20, 46] 2 1 1999-01-08 [5, 21, 23, 26, 37] 3 2 1999-01-05 [4, 31, 32, 34, 43] 19 3 1999-01-01 [11, 19, 28, 43, 48] 5 4 1998-12-29 [3, 5, 7, 28, 35] 10
and want slice them couple periods given specific date dividers, , while i'm having no trouble specifying first , last periods, can't seem specify middle periods:
#firsttime , secondtime date separators firsttime = datetime.datetime.strptime('1/13/99', '%m/%d/%y') secondtime = datetime.datetime.strptime('5/15/02', '%m/%d/%y') firstperiod = firstreadin[firstreadin.date < firsttime].reset_index(drop=true) thirdperiod = firstreadin[firstreadin.date > secondtime].reset_index(drop=true)
i can't secondperiod! (the 1 between firsttime , secondtime). i've tried this:
secondperiod = firstreadin[firstreadin.date >= firsttime , firstreadin.date < secondtime]
but gives me valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
what's going on , how should solve it? thanks!
you can use numpy.logical_and
: numpy.logical_and(a, b)
secondperiod = firstreadin[numpy.logical_and(firstreadin.date >= firsttime, firstreadin.date < secondtime)]
i hope can you!
Comments
Post a Comment