问题描述
对于这样的基本问题,我事先表示歉意,但我很沮丧.
I apologize in advance for asking such a basic question but I am stumped.
这是一个非常简单的虚拟示例.我在Pandas中遇到一些匹配日期的问题,我不知道为什么.
This is a very simple, dummy example. I'm having some issue matching dates in Pandas and I can't figure out why.
df = pd.DataFrame([[1,'2016-01-01'],
[2,'2016-01-01'],
[3,'2016-01-02'],
[4,'2016-01-03']],
columns=['ID', 'Date'])
df['Date'] = df['Date'].astype('datetime64')
说我想匹配上述df中的第1行.
我事先知道我要匹配ID 1
.
而且我也知道我想要的日期,实际上,我将直接从df的第1行提取该日期,以使其防弹.
Say I want to match row 1 in the above df.
I know beforehand that I want to match ID 1
.
And I know the date I want as well, and as a matter of fact, I'll extract that date directly from row 1 of the df to make it bulletproof.
some_id = 1
some_date = df.iloc[1:2]['Date'] # gives 2016-01-01
那为什么为什么这一行不给我返回第1行?
So why doesn't this line work to return me row 1??
df[(df['ID']==some_id) & (df['Date'] == some_date)]
相反,我得到了ValueError: Series lengths must match to compare
据我所知,这是有道理的...但是让我纳闷...如果我不能一一比较,该如何在熊猫中比较日期?
Instead I get ValueError: Series lengths must match to compare
which I understand, and makes sense...but leaves me wondering...how else can I compare dates in pandas if I can't compare one to many?
推荐答案
您说:
some_date = df.iloc[1:2]['Date'] # gives 2016-01-01
但这不是它所提供的.它为Series提供了一个元素,而不仅仅是一个值-当您使用[1:2]
作为切片时,您不会得到一个元素,而是一个包含一个元素的容器:
but that's not what it gives. It gives a Series with one element, not simply a value -- when you use [1:2]
as your slice, you don't get a single element, but a container with one element:
>>> some_date
1 2016-01-01
Name: Date, dtype: datetime64[ns]
相反,做
>>> some_date = df.iloc[1]['Date']
>>> some_date
Timestamp('2016-01-01 00:00:00')
之后
>>> df[(df['ID']==some_id) & (df['Date'] == some_date)]
ID Date
0 1 2016-01-01
(请注意,如果要查找很多some_id
和some_date
值,则模式会更有效,但这是一个单独的问题.)
(Note that there are more efficient patterns if you have a lot of some_id
and some_date
values to look up, but that's a separate issue.)
这篇关于ValueError:系列长度必须匹配才能在 pandas 中匹配日期时进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!