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()
Comment From: sinhrks
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
Comment From: jreback
we already note this bug multiple times: https://github.com/pandas-dev/pandas/issues?q=is%3Aissue+label%3AApply+is%3Aclosed
Comment From: jreback
though it’s not actually a bug see he related issues
Comment From: jreback
master is here: https://github.com/pandas-dev/pandas/issues/15628