简体   繁体   English

使用<和>在Pandas中选择日期范围时遇到麻烦

[英]trouble selecting a period of dates in Pandas with < and >

a really simple question perhaps, but it's my first time working with pandas already and I'm having trouble slicing up my dataframes into smaller ones based on dates. 也许这是一个非常简单的问题,但这是我第一次与熊猫打交道,而且我很难根据日期将数据框切成较小的数据框。

So, i have a dataframe (named firstreadin ) that looks like this (and thousands more rows): 所以,我有一个数据firstreadin (名为firstreadin ),看起来像这样(还有数千行):

        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 i want to slice them into a couple periods given specific date dividers, and while I'm having no trouble specifying the first and last periods, i can't seem to specify the middle periods: 并且我想在给定特定日期分隔符的情况下将它们分成几个时段,尽管我可以很方便地指定第一个和最后一个时段,但似乎无法指定中间时段:

#firsttime and secondtime are the 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 get the secondperiod! 我无法获得第二期! (the one between firsttime and secondtime). (第一次和第二次之间的时间)。 I've tried this: 我已经试过了:

secondperiod = firstreadin[firstreadin.date >= firsttime and firstreadin.date < secondtime] 

but this just gives me ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 但这只是给我ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What's going on and how should I solve it? 怎么回事,我应该如何解决? Thanks! 谢谢!

You can use numpy.logical_and : numpy.logical_and(a, b) 您可以使用numpy.logical_and :numpy.logical_and(a,b)

    secondperiod = firstreadin[numpy.logical_and(firstreadin.date >= firsttime, firstreadin.date < secondtime)]

I hope this can help you! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM