【发布时间】:2018-03-29 17:18:38
【问题描述】:
我在应用一个在 pandas 数据帧上返回一个元组的函数时遇到了一些我不理解的行为。我的意图是让df.apply() 返回一个新系列,但这似乎只有在我将数据框中的列子集以排除一个日期时间系列的列时才有效。
这个虚拟示例演示了我看到的行为:
df = pd.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))
def random(row):
# Return an tuple with more elements than df has columns
return (1,2,3,4,5,6,7,8)
df.apply(random,axis=1)
# Output, returns new series as expected:
0 (1, 2, 3, 4, 5, 6, 7, 8)
1 (1, 2, 3, 4, 5, 6, 7, 8)
2 (1, 2, 3, 4, 5, 6, 7, 8)
3 (1, 2, 3, 4, 5, 6, 7, 8)
4 (1, 2, 3, 4, 5, 6, 7, 8)
按预期工作,但是当我向数据框添加日期时间列时...
df['E'] = datetime.now()
df.apply(random,axis=1)
我收到此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in create_block_manager_from_arrays(arrays, names, axes)
4262 blocks = form_blocks(arrays, names, axes)
-> 4263 mgr = BlockManager(blocks, axes)
4264 mgr._consolidate_inplace()
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in __init__(self, blocks, axes, do_integrity_check, fastpath)
2760 if do_integrity_check:
-> 2761 self._verify_integrity()
2762
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in _verify_integrity(self)
2970 if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
-> 2971 construction_error(tot_items, block.shape[1:], self.axes)
2972 if len(self.items) != tot_items:
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in construction_error(tot_items, block_shape, axes, e)
4232 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4233 passed, implied))
4234
ValueError: Shape of passed values is (5, 8), indices imply (5, 5)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-29-b57dd4b93995> in <module>()
----> 1 df.apply(random,axis=1)
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4150 if reduce is None:
4151 reduce = True
-> 4152 return self._apply_standard(f, axis, reduce=reduce)
4153 else:
4154 return self._apply_broadcast(f, axis)
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
4263 index = None
4264
-> 4265 result = self._constructor(data=results, index=index)
4266 result.columns = res_index
4267
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
264 dtype=dtype, copy=copy)
265 elif isinstance(data, dict):
--> 266 mgr = self._init_dict(data, index, columns, dtype=dtype)
267 elif isinstance(data, ma.MaskedArray):
268 import numpy.ma.mrecords as mrecords
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
400 arrays = [data[k] for k in keys]
401
--> 402 return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
403
404 def _init_ndarray(self, values, index, columns, dtype=None, copy=False):
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
5406 axes = [_ensure_index(columns), _ensure_index(index)]
5407
-> 5408 return create_block_manager_from_arrays(arrays, arr_names, axes)
5409
5410
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in create_block_manager_from_arrays(arrays, names, axes)
4265 return mgr
4266 except ValueError as e:
-> 4267 construction_error(len(arrays), arrays[0].shape, axes, e)
4268
4269
/Users/jguillette/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in construction_error(tot_items, block_shape, axes, e)
4231 raise ValueError("Empty data passed with indices specified.")
4232 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4233 passed, implied))
4234
4235
ValueError: Shape of passed values is (5, 8), indices imply (5, 5)
我没有收到错误的唯一一次是当函数返回一个元组时,它的元素数量与数据框的列数相同,当它返回一个 DataFrame 而不是一个系列时。
有没有办法改变这种行为?在我的情况下,我不需要在函数中使用日期时间信息,但我仍然不明白排除它如何改变 apply 的行为。
任何见解都将不胜感激。
【问题讨论】: