Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Series lengths must match to compare when matching dates in Pandas

Tags:

python

pandas

I apologize in advance for asking such a basic question but I am stumped.

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')

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

So why doesn't this line work to return me row 1??

df[(df['ID']==some_id) & (df['Date'] == some_date)] 

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?

like image 272
Monica Heddneck Avatar asked Jan 04 '16 06:01

Monica Heddneck


Video Answer


1 Answers

You say:

some_date = df.iloc[1:2]['Date']  # gives 2016-01-01

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]

Instead, do

>>> some_date = df.iloc[1]['Date']
>>> some_date
Timestamp('2016-01-01 00:00:00')

after which

>>> df[(df['ID']==some_id) & (df['Date'] == some_date)] 
   ID       Date
0   1 2016-01-01

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

like image 53
DSM Avatar answered Sep 28 '22 05:09

DSM