-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
pd.Series.apply can't be made to produce Series of expected dtype when base series is empty #29323
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
Comments
I'm interested in taking a look. |
My initial thought is that it has something to do with this logic that goes first in the definition for
This would make it so that, no matter what, an empty series will always return a copy of itself and will never check if the function given to What about just making the return type Also, while I could reproduce the behaviour in the spec, I couldn't reproduce the motivating example with the datetime -> bool conversion when I uncommented the empty list creation. The resulting series was an empty series with dtype of One objection to this approach is that the default dtype for empty series (i.e. |
Yeah looks like it is that. From a quick blame check in tig, it seems to have been introduced in Quick fix would ofc be to allow the user to input a kwarg to
What does
I think other things probably depend on the default being float.. |
It looks like dtype of
I could put that in. Alternatively, we could do Here's my
### INSTALLED VERSIONS
commit : aa0f138 pandas : 0.24.0.dev0+3421.gaa0f1382f.dirty |
@pambot you can just run the function for the empty case and return the result might just work |
@TomAugspurger I think you recommended we close this? |
Probably. For DataFrame.apply on an empty df, we should be able to pass the empty cols to the user's function. For @jreback I think this issue is specifically about Series.apply. |
That's right, it's about
where you can know what type Although I understand your point when you say:
But that argues it should actually throw an exception (I'm not suggesting it do so, as obviously that would cause a lot of breakages). So giving the option to force a type may be better than nothing |
ok closing yeah this is basically impossible to infer for a Series because we can’t call the function |
Code Sample
Problem description
May be related to #28427.
When calling
apply()
on a series, you expect to get back a series with a certaindtype
based on the function you give it. However, if the baseSeries
is empty, there is nothing to apply the function to, so the resultantSeries
just has the samedtype
as the base.My actual use case is more along the lines of this:
I would have thought there would be a way to force the type of the return value of
.apply
even when the base vector is empty. You can obviously get around this easily by checking whether the base is empty when you assigncolB
:Which coerces the
dtype
without setting any values, and that seems hacky and it feels like it should be possible to just make apply do the right thing.Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.2.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 0.25.3
numpy : 1.17.3
pytz : 2019.3
dateutil : 2.8.0
pip : 18.1
setuptools : 40.6.3
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 : 7.9.0
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
The text was updated successfully, but these errors were encountered: