Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python 3.6.9 (default, Oct 17 2019, 06:49:31)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> df1 = pd.DataFrame({'A': pd.Series([1, 3, np.nan], dtype='Int64'), 'B': list('abc')})
>>> df1
A B
0 1 a
1 3 b
2 NaN c
>>> df1.dtypes
A Int64
B object
dtype: object
>>> df2 = pd.DataFrame({'C': list('xz')})
>>> df1.join(df2)
A B C
0 1 a x
1 3 b z
2 NaN c NaN
>>> df2.join(df1)
C A B
0 x 1 a
1 z 3 b
>>> df1.loc[:, 'G'] = np.nan
>>> df1
A B G
0 1 a NaN
1 3 b NaN
2 NaN c NaN
>>> df1.join(df2)
A B G C
0 1 a NaN x
1 3 b NaN z
2 NaN c NaN NaN
>>> df2.join(df1)
C A B G
0 x 1 a NaN
1 z 3 b NaN
>>> df1.loc[:, 'G'] = df1.loc[:, 'G'].astype('Int64')
>>> df1.dtypes
A Int64
B object
G Int64
dtype: object
>>> df1.join(df2)
A B G C
0 1 a NaN x
1 3 b NaN z
2 NaN c NaN NaN
>>> df2.join(df1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 6824, in join
rsuffix=rsuffix, sort=sort)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 6839, in _join_compat
suffixes=(lsuffix, rsuffix), sort=sort)
File "/usr/local/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 48, in merge
return op.get_result()
File "/usr/local/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 560, in get_result
concat_axis=0, copy=self.copy)
File "/usr/local/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 2061, in concatenate_block_managers
concatenate_join_units(join_units, concat_axis, copy=copy),
File "/usr/local/lib/python3.6/site-packages/pandas/core/internals/concat.py", line 240, in concatenate_join_units
for ju in join_units]
File "/usr/local/lib/python3.6/site-packages/pandas/core/internals/concat.py", line 240, in <listcomp>
for ju in join_units]
File "/usr/local/lib/python3.6/site-packages/pandas/core/internals/concat.py", line 194, in get_reindexed_values
missing_arr = np.empty(self.shape, dtype=empty_dtype)
TypeError: data type not understood
>>> df2
C
0 x
1 z
>>> df3 = pd.DataFrame({'Z': list('lmn')})
>>> df2.join(df3)
C Z
0 x l
1 z m
>>> df1.join(df3)
A B G Z
0 1 a NaN l
1 3 b NaN m
2 NaN c NaN n
>>> df3.join(df1)
Z A B G
0 l 1 a NaN
1 m 3 b NaN
2 n NaN c NaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment