【发布时间】:2014-08-01 14:38:49
【问题描述】:
第二个参数应该只是数组中每个元素的相同标量。
我只是想从 YYYYMMMDD 的数字代码中提取月份和日期。我会为每个值采用numpy.mod(datenum,10000),但numpy ufunc mod 采用两个类似数组的参数。
按照 pandas.apply 上的instructions,我尝试使用以下测试代码但失败了:
import numpy as np
from pandas import *
s = Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
t = s.apply(np.mod,raw=True,args=(10000,))
print s
print t
Traceback (most recent call last):
File "…", line 7, in <module>
t = s.apply(np.mod,raw=True,args=(10000,))
File "…/miniconda/lib/python2.7/site-packages/pandas/core/series.py", line 2023, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "inference.pyx", line 920, in pandas.lib.map_infer (pandas/lib.c:44780)
File "…/miniconda/lib/python2.7/site-packages/pandas/core/series.py", line 2012, in <lambda>
f = lambda x: func(x, *args, **kwds)
TypeError: 'raw' is an invalid keyword to ufunc 'remainder'
没有raw=True,错误信息为:
Traceback (most recent call last):
File "…", line 7, in <module>
t = s.apply(np.mod,args=(10000,))
File "…/miniconda/lib/python2.7/site-packages/pandas/core/series.py", line 2017, in apply
return f(self)
ValueError: invalid number of arguments
这是如何工作的?
【问题讨论】:
-
s应该如何表示日期?您正在从标准正态分布中绘制浮点数;结果看起来不会像 8 位日期。 -
这是我的测试代码,我可以通过快速编辑使其更清晰。但是,那我该如何为
mod指定10000呢?
标签: python arrays numpy pandas