Joining on a nullable column
See original GitHub issueimport 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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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…
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)