question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Joining on a nullable column

See original GitHub issue
import pandas as pd

left = pd.DataFrame({"a": [1, 2, pd.NA]}, dtype="Int64")
right = pd.DataFrame({"b": [1, 2, pd.NA]}, dtype="Int64")

pd.merge(left, right, how="inner", left_on="a", right_on="b")
#       a     b
# 0     1     1
# 1     2     2
# 2  <NA>  <NA>

(Above is from 1.0.1 and master)

I think when joining on a nullable column we should not be matching NA with NA and should only be joining where we have unambiguous equality (as in SQL). Also worth noting that this is the same as what happens when we have NaN which also seems incorrect, so could be an opportunity to fix this behavior?

pd.merge(left.astype(float), right.astype(float), how="inner", left_on="a", right_on="b")
#      a    b
# 0  1.0  1.0
# 1  2.0  2.0
# 2  NaN  NaN

Expected Output

      a     b
0     1     1
1     2     2

cc @jorisvandenbossche

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
TheGustafsoncommented, Nov 12, 2021

Has there been any additional discussion on this? I came across this behavior in my work, and I was genuinely surprised that this was the default behavior of Pandas.

The merge: match_na solution with a deprecation warning that @kasuteru proposed is a great idea.

In the interim, a warning should be put into the merge documentation. This should be a high priority…

2reactions
jorisvandenbosschecommented, Mar 3, 2020

The issue is https://github.com/pandas-dev/pandas/issues/22491, but we apparently have many other related, possibly duplicate, issues (from a quick search): https://github.com/pandas-dev/pandas/issues/28522, https://github.com/pandas-dev/pandas/issues/22618, https://github.com/pandas-dev/pandas/issues/25138, https://github.com/pandas-dev/pandas/issues/7473 (we should probably clean that up a bit)

But in any case, it would be good to directly have the “correct” behaviour for the new nullable dtypes (where we have less concerns about backwards compatibility)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Join SQL Server tables where columns include NULL values
Option 1. The first option is to update your NULL values with a default value so you are always joining on a value...
Read more >
Joining on NULLs - Data with Bert
One option is to make our AccountType column NOT NULL and set some other default value. Another option is to create a new...
Read more >
SQL-Join with NULL-columns - Stack Overflow
* FROM b NATURAL JOIN a; and everything is fine. When a.bid or a.cid is NULL , I want to get every row...
Read more >
Joining on columns with NULL values - IBM
When joining on 2 tables in a column with NULL values, there is a NULL value in Table A and a NULL value...
Read more >
How null values affect joins - Sybase Infocenter
Null values in tables or views being joined never match each other. Since bit columns do not permit null values, a value of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found