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

DataFrame.apply returns NaN if DataFrame contains datetime column #18775

Closed
AlexHentschel opened this Issue Dec 14, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@AlexHentschel

AlexHentschel commented Dec 14, 2017

Code Sample, a copy-pastable example if possible

import pandas as pd

A = pd.DataFrame()
A["author"] = ["X", "Y", "Z"]
A["publisher"] = ["BBC", "NBC", "N24"]
A["date"] = pd.to_datetime(['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"])

# the following produces the faulty result
A.apply(lambda x: {}, axis=1)

Problem description

The last line returns a dataframe with all entries replaced by NaN. This only happens if the following two conditions are both satisfied:

  • a column with datetime64[ns] is present in the dataframe (in the above example the column with name date)
  • the function applied to the dataframe returns a dictionary
    When using a Dataframe without the datetime column, the code returns the expected result (for the above result a pd.Series with empty dictionaries).

Why this is a (significant) problem:
Output of apply depends on presence of another column that is not used by applied function.

Potentially related:
I tried to search for a similar issues and found the already closed issues

However, these issues are fixed and already closed since 2015.

Expected Output

the expected output can be easily produced by removing the 6th line (A["date"] = ...)

>>> A.apply(lambda x: {}, axis=1)
0    {}
1    {}
2    {}
dtype: object

Output of pd.show_versions()

Checked with newest version of pandas:

  • pandas (0.21.1)
  • numpy (1.13.3)
  • Python 3.6.2

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Darwin
OS-release: 17.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_CA.UTF-8
pandas: 0.21.1
pytest: None
pip: 9.0.1
setuptools: 36.6.0
Cython: None
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
@AlexHentschel

This comment has been minimized.

AlexHentschel commented Dec 14, 2017

Just wanted to mention that Pandas is a great tool and you are doing awesome work. Thanks.

@chris-b1

This comment has been minimized.

Contributor

chris-b1 commented Dec 14, 2017

Thanks for the report, this should fall under @jreback's PR at #18577 (in progress)

That said, with my last pull of it the output is inferred as completely empty - some discussion over there on exactly what kwargs should be used to control this.

pd.__version__
Out[7]: '0.22.0.dev0+310.gf6f0371'

A.apply(lambda x: {}, axis=1)
Out[5]: 
Empty DataFrame
Columns: []
Index: [0, 1, 2]
@jreback

This comment has been minimized.

Contributor

jreback commented Dec 14, 2017

yep, this is already covered by #18577, added as an additional test.

@jreback jreback added the Duplicate label Dec 14, 2017

jreback added a commit to jreback/pandas that referenced this issue Dec 14, 2017

@AlexHentschel

This comment has been minimized.

AlexHentschel commented Dec 15, 2017

Thanks for the quick feedback. Looking forward to working with the fixed version.

jreback added a commit to jreback/pandas that referenced this issue Dec 21, 2017

jreback added a commit to jreback/pandas that referenced this issue Dec 23, 2017

jreback added a commit to jreback/pandas that referenced this issue Jan 6, 2018

API/BUG: .apply will correctly infer output shape when axis=1
closes #16353
closes #17348
closes #17437
closes #18573
closes #17970
closes #17892
closes #17602
closes #18775
closes #18901
closes #18919

jreback added a commit to jreback/pandas that referenced this issue Feb 5, 2018

API/BUG: .apply will correctly infer output shape when axis=1
closes #16353
closes #17348
closes #17437
closes #18573
closes #17970
closes #17892
closes #17602
closes #18775
closes #18901
closes #18919

jorisvandenbossche added a commit that referenced this issue Feb 7, 2018

harisbal pushed a commit to harisbal/pandas that referenced this issue Feb 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment