Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Outer merge casts EA dtype to object #40073

Closed
2 of 3 tasks
adaitche opened this issue Feb 26, 2021 · 3 comments · Fixed by #43152
Closed
2 of 3 tasks

BUG: Outer merge casts EA dtype to object #40073

adaitche opened this issue Feb 26, 2021 · 3 comments · Fixed by #43152
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@adaitche
Copy link

adaitche commented Feb 26, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

d1 = pd.DataFrame([("a",)], columns=["id"], dtype="string")
d2 = pd.DataFrame([("b",)], columns=["id"], dtype="string")
result = d1.merge(d2, on="id", how="outer")
dtype = result.dtypes["id"]
print(dtype) # expected 'string', but is 'object'

Problem description

When doing an outer merge on a column of type string, the result contains this column with the type object. This makes it more difficult to use the new string type.

The problem doesn't occur with the types Int32, Float32, boolean. It seems to be specific to the string type.

The problem doesn't occur with a left, right or inner-merge.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
Version : Darwin Kernel Version 18.7.0: Tue Nov 10 00:07:31 PST 2020; root:xnu-4903.278.51~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.0.1
hypothesis : 5.30.1
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.3.23
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@adaitche adaitche added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 26, 2021
@mzeitlin11
Copy link
Member

Thanks for the report @adaitche! Can confirm this is still a bug on master, investigations to fix are welcome!

@mzeitlin11 mzeitlin11 added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Strings String extension data type and string data and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 18, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Mar 18, 2021
@mzeitlin11
Copy link
Member

Issue here looks similar to #40148. An Index call loses the ExtensionDType, so either that call would need to be avoided, or #39133 solved.

@mzeitlin11 mzeitlin11 added NA - MaskedArrays Related to pd.NA and nullable extension arrays ExtensionArray Extending pandas with custom dtypes or arrays. and removed NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Mar 25, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue May 9, 2021
@simonjayhawkins
Copy link
Member

The problem doesn't occur with a left, right or inner-merge.

on master the problem now occurs with how="right"

>>> print(pd.__version__)
1.3.0.dev0+1567.g67c9385787
>>> d1 = pd.DataFrame([("a",)], columns=["id"], dtype="string")
>>> d2 = pd.DataFrame([("b",)], columns=["id"], dtype="string")
>>> result = d1.merge(d2, on="id", how="right")
>>> dtype = result.dtypes["id"]
>>> print(dtype)
object

first bad commit: [08ff65f] BUG: merge between partial index and index fails when result is empty (#34414) cc @phofl

The problem doesn't occur with the types Int32, Float32, boolean. It seems to be specific to the string type.

it appears this is also an issue for other EA dtypes (and now on master also for how="right")

>>> print(pd.__version__)
1.3.0.dev0+1567.g67c9385787
>>> d1 = pd.DataFrame([(1,)], columns=["id"], dtype="Int64")
>>> d2 = pd.DataFrame([(2,)], columns=["id"], dtype="Int64")
>>> result = d1.merge(d2, on="id", how="outer")
>>> dtype = result.dtypes["id"]
>>> print(dtype)
object
>>> 

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version and removed Strings String extension data type and string data labels May 9, 2021
@simonjayhawkins simonjayhawkins changed the title BUG: Outer merge casts dtype string to object BUG: Outer merge casts EA dtype to object May 9, 2021
@mroeschke mroeschke added the Dtype Conversions Unexpected or buggy dtype conversions label Aug 19, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3.3 Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants