Description
In [1]: pd.DataFrame(dict(a=[1, 1, 2, 2], b=["", "", "", ""])).groupby("b").apply(lambda df: df.iloc[-1])
Out[1]:
3 a b
b
2
In [2]: pd.DataFrame(dict(a=[1, 1, 2, 2], b=["", "", "", ""], c=pd.to_datetime([1, 2, 3, 4], unit="s"))).groupby("b").apply(lambda df: df.iloc[-1])
Out[2]:
3 a b c
b
2 NaN 1970-01-01 00:00:04 <------------ empty string converted to NaN
In [3]: pd.DataFrame(dict(a=[1, 1, 2, 2], b=["abc", "abc", "", ""], c=pd.to_datetime([1, 2, 3, 4], unit="s"))).groupby("b").apply(lambda df: df.iloc[-1])
Out[3]:
a b c
b
2 1970-01-01 00:00:04
abc 1 abc 1970-01-01 00:00:02
Problem description
When using groupBy
with a column of empty strings and a column of Timestamp
s, the empty string can get converted to nan. See these three examples which display inconsistent behaviour:
Expected Output
The output in Out[3]
shouldn't contain a NaN but the empty string ""
.
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.16-200.fc28.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
pandas: 0.23.4
pytest: 3.3.2
pip: 10.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.13.3
scipy: 0.19.1
pyarrow: 0.8.0
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.7.2
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.1
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None