Skip to content

Apply method broken for empty integer series with datetime index #21245

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
jluttine opened this issue May 29, 2018 · 4 comments · Fixed by #29358
Closed

Apply method broken for empty integer series with datetime index #21245

jluttine opened this issue May 29, 2018 · 4 comments · Fixed by #29358
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@jluttine
Copy link

Code Sample, a copy-pastable example if possible

# Your code here
>>> pd.Series([], index=pd.date_range(start="2018-01-01", periods=0), dtype=int).apply(lambda x: x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.6/site-packages/pandas/core/series.py", line 2526, in apply
    index=self.index).__finalize__(self)
  File "/.../lib/python3.6/site-packages/pandas/core/series.py", line 264, in __init__
    raise_cast_failure=True)
  File "/.../lib/python3.6/site-packages/pandas/core/series.py", line 3228, in _sanitize_array
    subarr = _try_cast(data, False)
  File "/.../lib/python3.6/site-packages/pandas/core/series.py", line 3163, in _try_cast
    subarr = np.array(subarr, dtype=dtype, copy=copy)
ValueError: cannot convert float NaN to integer

Problem description

For some reason, apply method doesn't work for empty series with datetime index and dtype int. Obviously, it should work.

Expected Output

>>> pd.Series([], index=pd.date_range(start="2018-01-01", periods=0), dtype=int).apply(lambda x: x)
Series([], Freq: D, dtype: int64)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.42
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.22.0
pytest: None
pip: None
setuptools: 39.0.1
Cython: 0.28.1
numpy: 1.14.2
scipy: 1.0.1
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: None
openpyxl: 2.5.2
xlrd: 0.9.4
xlwt: 1.3.0
xlsxwriter: None
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.6
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jluttine
Copy link
Author

Not sure how much this issue is related, but it might be: #21192

@mroeschke
Copy link
Member

This has been fixed in the latest release.

In [14]: pd.__version__
Out[14]: u'0.23.0'

In [15]: pd.Series([], index=pd.date_range(start="2018-01-01", periods=0), dtype=int).apply(lambda x: x)
Out[15]: Series([], Freq: D, dtype: int64)

@jreback do we have this edge case tested?

@mroeschke mroeschke added Testing pandas testing functions or related to the test suite Apply Apply, Aggregate, Transform, Map labels May 30, 2018
@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Apply Apply, Aggregate, Transform, Map Testing pandas testing functions or related to the test suite labels Oct 7, 2019
@ChandProj
Copy link

Hi,
I would like to write test case for this as part of PyCon India'19 dev sprint.
@mroeschke Can you please elaborate on the edge test case.

@mroeschke
Copy link
Member

We would just need a test seeing that the original issue was fixed. Namely that the example in #21245 (comment) gives the stated desired output

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants