Description
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
contains1
,date_col
will containNaT
. - If
bar
contains"a"
,date_col
will containNone
.
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