• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ValueError:系列长度必须匹配才能在 pandas 匹配日期时进行比较

用户头像
it1352
帮助1

问题说明

对于这样的基本问题,我事先表示歉意,但我很沮丧.

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?

正确答案

#1

您说:

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_idsome_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.)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgchii
系列文章
更多 icon
同类精品
更多 icon
继续加载