Skip to content

pd.concat inconsistent coercion of None depending of value in rows #29023

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

Closed
ConorSheehan1 opened this issue Oct 16, 2019 · 5 comments
Closed
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@ConorSheehan1
Copy link

Code example using pandas 0.25.1

import pandas as pd

a = pd.DataFrame({
    "date_col": [pd.Timestamp("1990-06-08 00:00:00")],
    "foo": [0],
})
b = pd.DataFrame({
    "date_col": [None],
    "bar": [1]
})

# Works as expected, None is converted to NaT
print(pd.concat([a, b], sort=False, ignore_index=True), "\n")
#     date_col  foo  bar
# 0 1990-06-08  0.0  NaN
# 1        NaT  NaN  1.0

# change bar from 1 to "a"
b = pd.DataFrame({
    "date_col": [None],
    "bar": ["a"]
})

# Expected NaT in date_col, got None
print(pd.concat([a, b], sort=False, ignore_index=True), "\n")
#               date_col  foo  bar
# 0  1990-06-08 00:00:00  0.0  NaN
# 1                 None  NaN    a

Problem description

I expect the None in date_col to be converted to NaT. Instead, it changes depending on the values of other columns!

  • If bar contains 1, date_col will contain NaT.
  • If bar contains "a", date_col will contain None.

Note this may be related to #28786, but it may not be an exact duplicate. I think the main thing this adds is noting that the behaviour is inconsistent depending on the value of the rows.

Another strange thing is that casting the output to dict and back to df, returns the output I'd expect, where None is converted to NaT!

import pandas as pd

a = pd.DataFrame({
    "date_col": [pd.Timestamp("1990-06-08 00:00:00")],
    "foo": [0],
})
b = pd.DataFrame({
    "date_col": [None],
    "bar": ["a"]
})

merged = pd.concat([a, b], sort=False, ignore_index=True)
# Excpeted NaT in date_col, got None
print(merged, "\n")
#               date_col  foo  bar
# 0  1990-06-08 00:00:00  0.0  NaN
# 1                 None  NaN    a

# Converts None to NaT as expected!
print(pd.DataFrame(merged.to_dict()))
#     date_col  foo  bar
# 0 1990-06-08  0.0  NaN
# 1        NaT  NaN    a

Expected Output

import pandas as pd

a = pd.DataFrame({
    "date_col": [pd.Timestamp("1990-06-08 00:00:00")],
    "foo": [0],
})
b = pd.DataFrame({
    "date_col": [None],
    "bar": ["a"]
})
print(pd.concat([a, b], sort=False, ignore_index=True), "\n")
#               date_col  foo  bar
# 0  1990-06-08 00:00:00  0.0  NaN
# 1                  NaT  NaN    a

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Darwin
OS-release : 18.6.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 0.25.1
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3
setuptools : 41.4.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@jbrockmendel jbrockmendel added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Oct 16, 2019
@mroeschke mroeschke added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Jul 21, 2021
@mroeschke
Copy link
Member

This looks to work on main. Could use a test

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Apr 28, 2023
@Kapil-chn7
Copy link

Take

@Kapil-chn7
Copy link

This looks to work on main. Could use a test

Hi mroeschke, I want to work on this can you help me to guide me? Can you explain what I need to do?. How can I run the test?

@cablegui
Copy link

Hi Kapil, I was looking at this issue as well. I think there is more than 1 issue in this concat behaviour. If you notice when you concat a, b and if you concat b, ,a the column datatype always changes to the datatype of the second argument. I am not sure if this the intended behaviour of this function but it does not look correct to me. If you follow #28786 as stated above you will see the implementation in concat.py I think. I have not gone thru it though.

@jbrockmendel
Copy link
Member

closed by #52613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

5 participants