Skip to content
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

Apply on DataFrame with datetime #17949

Closed
sebov opened this issue Oct 23, 2017 · 4 comments
Closed

Apply on DataFrame with datetime #17949

sebov opened this issue Oct 23, 2017 · 4 comments
Labels
API Design Apply Apply, Aggregate, Transform, Map Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@sebov
Copy link

sebov commented Oct 23, 2017

Code Sample, a copy-pastable example if possible

x = datetime.datetime.now()
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'result': row['b']}, axis=1))
print()
x = datetime.datetime.now()
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'], dtype='int64')
print(df.apply(lambda row: {'result': row['b']}, axis=1))
print()
x = 101
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'result': row['b']}, axis=1))
print()
x = datetime.date.today()
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'result': row['b']}, axis=1))
print()
x = {'x': 2}
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'result': row['b']}, axis=1))

Problem description

The latter 4 cases produce the results that I can expect -- Series containing dicts. The first one is somehow strange as it produces a DataFrame:
a b
0 NaN NaN
1 NaN NaN

Also, when I change the first case's print line to print(df.apply(lambda row: [10,20], axis=1)) I am also receiving a DataFrame:
a b
0 10 20
1 10 20

Expected Output

0 {'result': 1}
1 {'result': 2}
dtype: object

0 {'result': 1}
1 {'result': 2}
dtype: object

0 {'result': 1}
1 {'result': 2}
dtype: object

0 {'result': 1}
1 {'result': 2}
dtype: object

Output of pd.show_versions()

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

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-96-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.5.0
Cython: None
numpy: 1.13.3
scipy: 0.19.1
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
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: 1.0b10
sqlalchemy: None
pymysql: None
psycopg2: 2.7.3.1 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@sinhrks
Copy link
Member

sinhrks commented Oct 23, 2017

Basically, .apply returning a dict results in DataFrame. Thus, I think your expected output is caused by a bug.

x = datetime.datetime.now()
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'a': row['b'], 'b': row['a']}, axis=1))
#    a                          b
# 0  1 2017-10-23 19:40:42.481066
# 1  2 2017-10-23 19:40:42.481066
df = pd.DataFrame([[x, 1], [x, 2]], columns=['a', 'b'])
print(df.apply(lambda row: {'a': row['b']}, axis=1))
#      a   b
# 0  1.0 NaN
# 1  2.0 NaN

@sinhrks sinhrks added the Bug label Oct 23, 2017
@jreback
Copy link
Contributor

jreback commented Oct 23, 2017

@jreback
Copy link
Contributor

jreback commented Oct 23, 2017

though it’s not actually a bug see he related issues

@jreback
Copy link
Contributor

jreback commented Oct 23, 2017

master is here: #15628

@jreback jreback closed this as completed Oct 23, 2017
@jreback jreback added Duplicate Report Duplicate issue or pull request Apply Apply, Aggregate, Transform, Map Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Bug labels Oct 23, 2017
@jreback jreback added this to the No action milestone Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Apply Apply, Aggregate, Transform, Map Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants