Description
Problem description
pandas.DataFrame.apply()
seems to be converting series from int64 to object in some circumstances, and I'm not sure why. An example of the strange behavior I'm seeing is shown below, along with comments on what I'm expecting to see versus what I actually see. Note, this issue was originally reported on SO here: https://stackoverflow.com/questions/58222263/unexpected-behavior-when-applying-function-to-all-columns-in-pandas-data-frame.
Code Sample, a copy-pastable example if possible
import pandas as pd
import numpy as np
df = pd.DataFrame({
"col_1": [1, 2, 3],
"col_2": ["hi", "there", "friend"]
})
print(df)
#> col_1 col_2
#> 0 1 hi
#> 1 2 there
#> 2 3 friend
print(df.dtypes)
#> col_1 int64
#> col_2 object
#> dtype: object
# looks like np.issubdtype returns the expected result when calling the function
# on each series:
np.issubdtype(df.col_1, np.number)
#> True
np.issubdtype(df.col_2, np.number)
#> False
# but it doesn't return the expected result when using the apply function:
print(df.apply(lambda x: np.issubdtype(x, np.number)))
#> col_1 False
#> col_2 False
#> dtype: bool
# we can see that apply seems to be coercing the series to objects here:
print(df.apply(lambda x: x.dtype))
#> col_1 object
#> col_2 object
#> dtype: object
# what's also pretty weird is that i get the expected result when applying the
# replace_nulls() function below (e.g., median imputation is used if the series
# is a number, otherwise nulls are replaced with "MISSING"):
df = pd.DataFrame({
"col_1": [1, 2, np.nan],
"col_2": ["hi", "there", np.nan]
})
def replace_nulls(s):
is_numeric = np.issubdtype(s, np.number)
missing_value = s.median() if is_numeric else "MISSING"
return np.where(s.isnull(), missing_value, s)
print(df.apply(replace_nulls))
#> col_1 col_2
#> 0 1.0 hi
#> 1 2.0 there
#> 2 1.5 MISSING
Created on 2019-10-03 by the reprexpy package
Output of pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.6.5.final.0
python-bits : 64
OS : Darwin
OS-release : 18.6.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 0.25.1
numpy : 1.17.2
pytz : 2019.2
dateutil : 2.8.0
pip : 19.0.3
setuptools : 40.8.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 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None