- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 pandas DataFrames,我正在添加新列以进行更高级的分析。我的 f 函数给我一个错误 TypeError: f() takes 0 positional arguments but 1 was given.我不明白为什么,如果您需要知道它的作用,我会在代码中记录我的 f 函数。
from pandas_datareader import data as dreader
import pandas as pd
from datetime import datetime
import dateutil.parser
# Sets the max rows that can be displayed
# when the program is executed
pd.options.display.max_rows = 200
# df is the name of the dataframe, it is
# reading the csv file containing date loaded
# from yahoo finance(Date,Open,High,Low,Close
# volume,adj close,)the name of the ticker
# is placed before _data.csv i.e. the ticker aapl
# would have a csv file named aapl_data.csv.
df = pd.read_csv("cde_data.csv")
# the following code will allow for filtering of the datafram
# based on the year, day of week (dow), and month. It then gets
# applied to the dataframe and then can be used to sort data i.e
# print(df[(df.year == 2015) & (df.month == 5) & (df.dow == 4)])
# which will give you all the days in the month of May(df.month == 5),
# that fall on a Thursday(df.dow == 4), in the year 2015
# (df.year == 2015)
#
# Month Day Year
# January = 1 Monday = 1 The year will be dispaly in a four
# February = 2 Tuesday = 2 digit format i.e. 2015
# March = 3 Wednesday = 3
# April = 4 Thursday = 4
# May = 5 Friday = 5
# June = 6
# July = 7
# August = 8
# September = 9
# October = 10
# November = 11
# December = 12
def year(x):
return(x.year)
def dow(x):
return(x.isoweekday())
def month(x):
return(x.month)
# f is a function that checks to see if the up_down column
# has a value that is greater than, less than, or equal to
# zero. The value in the up_down column is derived from
# subtracting the opening price of the stock(open column)
# from closing price of the stock(close column). If up_down
# has a negative value than the stocks price was Down, a positive
# value then Up, and no change is Flat
def f():
if up_down > 0:
x = Up
elif up_down < 0:
x = Down
else:
x = Flat
return (f)
df.reset_index()
df.Date = df.Date.apply(dateutil.parser.parse)
df['year'] = df.Date.apply(year)
df['dow'] = df.Date.apply(dow)
df['month'] = df.Date.apply(month)
df['up_down'] = df['Close'] - df['Open']
df['up_down_flat'] = df.up_down.apply(f)
df2= (df[(df.year > 1984) & (df.month == 5) & (df.dow == 1)])
print (df2)
这是错误
Traceback (most recent call last):
File "dframt1est.py", line 77, in <module>
df['up_down_flat'] = df.up_down.apply(f)
File "C:\Users\Zac\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\core\series.py", line 2220, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas\src\inference.pyx", line 1088, in pandas.lib.map_infer (pandas\lib.c:63043)
TypeError: f() takes 0 positional arguments but 1 was given
Press any key to continue . . .
这是一个示例 csv
Date Open High Low Close Volume Adj Close \
0 1990-04-12 26.875000 26.875000 26.625 26.625 6100 250.576036
1 1990-04-16 26.500000 26.750000 26.375 26.750 500 251.752449
2 1990-04-17 26.750000 26.875000 26.750 26.875 2300 252.928863
3 1990-04-18 26.875000 26.875000 26.500 26.625 3500 250.576036
4 1990-04-19 26.500000 26.750000 26.500 26.750 700 251.752449
5 1990-04-20 26.750000 26.875000 26.750 26.875 2100 252.928863
6 1990-04-23 26.875000 26.875000 26.750 26.875 700 252.928863
7 1990-04-24 27.000000 27.000000 26.000 26.000 2400 244.693970
8 1990-04-25 25.250000 25.250000 24.875 25.125 9300 236.459076
最佳答案
您正在将 f
作为 apply
中的函数传递。为数据框中的每一行调用该函数,并且需要将该行作为其参数。
另请注意,您返回的是函数本身作为结果;我很确定您打算返回 x
而不是 f
。此外,您似乎没有在任何地方定义 Up
、Down
和 Flat
。
关于python - 为什么我创建的函数出现 TypeError : f() takes 0 positional arguments but 1 was given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430440/
我有一个代表每个年龄段的人口(M,F)的集合。 为了通过时间来预测人口,我必须首先对女性进行计算,以便我可以根据男性出生率的统计常数来计算新出生的男性和女性的百分比。 也就是说,我有一个包含每岁男性和
我正在尝试从队列中获取 n 条消息(使用 langohr)。我有一个工作版本,但我想知道是否有更好的 clojurist 方法来做到这一点: (def not-nil? (complement nil
我有这些结果用于分析一个简单的查询,该查询不会从少于 200 条记录的表中返回超过 150 条记录,因为我有一个存储最新值的表,而其他字段是数据的 FK . 更新:稍后查看同一查询的新结果。该网站未公
我正在使用 .Take() 来获取固定数量的结果。 获取 TotalCountBeforeTake 的最佳方法是什么(即好像我没有使用 .Take())? 我可以在不运行查询两次的情况下获得 Tota
我有一个 BatchConfigurable 类 public class BatchConfigurable() {} 我正在尝试为其编写一个包装器。这将是另一个类,它采用此类或任何扩展 Batch
byte[] result = memStream.ToArray(); memStream.Close(); byte[] temp = r
很简单的问题。我有一个值列表,我想用空值填充这些值,这样我总是会返回 X 个项目。 List list = new List() { 10, 20, 30 }; IEnumerable values
我正在构建一个购物车,并且我使用了一个购物车服务,在该服务中我将数量分配给产品/将产品添加到购物车。除了使用 take 获取可观察项 $ 的第一个实例之外,还有其他方法吗? 我正在正确导入 take
这是欧拉计划的问题 8。 我试图通过数字数组foreach,每次跳过最后一个数字并拉接下来的13个相邻数字数组。 我的代码: for(int x = 0; x product) {
我有 3 个 div 元素,一个是父元素,另外两个是子元素: dinesh pathak and their css are: #table {
我在 Hudson 上发现了异常行为。Hudson 作业大约需要 25 分钟,而当我在本地运行相同的作业时,需要 9 分钟。我在这里缺少什么? 我增加了 JAVA_OPTS、MAVEN_OPTS,甚至
let a = [1;2;3;] for i in (a |> Seq.take 10) do Console.WriteLine(i) for i in (a |> Seq.take 100) do
我正在尝试编写一些 LINQ To SQL 代码来生成类似 SQL 的代码 SELECT t.Name, g.Name FROM Theme t INNER JOIN ( SELECT TOP
给定这样的设置.. class Product { int Cost; // other properties unimportant } var products = new List
我有一个 List 类型的元素 public class FriendList { public List friends { get; set; } // List
给定以下 LINQ 语句,哪个更有效? 一个: public List GetLatestLogEntries() { var logEntries = from entry in db.Lo
我只是在尝试新的 kotlin 语言。我遇到了生成无限列表的序列。我生成了一个序列并尝试打印前 10 个元素。但是下面的代码没有打印任何东西: fun main(args: Array) {
我见过 sagas 以 3 种方式监听 Action : 1。 while(true) take() function* onUserDetailsRequest() { while(true)
假设我有一些神奇的分页黑盒类,它使用 pageIndex 和 pageSize 检索数据,如下所示: public class PaginatedList { // ... // Ch
我有两个 git 分支 b' 和 b" 具有完全相同的 SHA-1 和因此内容。我提交 b ' 并在提交时,我使用 -x 应用 cherry-pick 而不是 merge 或 rebase单个提交到我
我是一名优秀的程序员,十分优秀!