草庐IT

set_relation

全部标签

python - 数据框 set_index 未设置

我有一个数据框,正在尝试将索引设置为“时间戳”列。目前索引只是一个行号。时间戳格式的一个例子是:2015-09-0316:35:00我试过设置索引:df.set_index('Timestamp')我没有收到错误,但是当我打印数据框时,索引仍然是行号。如何使用时间戳作为索引? 最佳答案 您需要指定inplace=True,或将结果分配给变量。尝试:df.set_index('Timestamp',inplace=True,drop=True)基本上,在设置索引时,您可能需要做两件事。一个是new_df=old_df.set_inde

python - 错误 : Setting an array element with a sequence. Python/Numpy

我在尝试将数组分配给另一个数组特定位置时收到此错误。在创建简单列表并进行此类分配之前,我正在这样做。但是Numpy比简单的列表更快,我现在正在尝试使用它。问题是因为我有一个存储一些数据的2D数组,并且在我的代码中,例如,我要计算每个位置值的梯度,所以我创建了另一个2D数组,其中每个位置存储其梯度值(value)。importnumpyasnpcols=2rows=3#Thisworksmatrix_a=[]foriinrange(rows):matrix_a.append([0.0]*cols)printmatrix_amatrix_a[0][0]=np.matrix([[0],[0]

python - Django select_related 过滤器

我有以下Django模型。classA(models.Model):tmp=models.ForeignKey(B)active=models.BooleanField()classB(models.Model):active=models.BooleanField()archived=models.BooleanField()现在我有以下查询。A.objects.select_related(B).filter(active=True)现在这会获取B的所有对象。现在我如何在select_related中包含active=True和archived=False的过滤器>模型B的子句。

python - 值错误 : Attempted relative import in non-package not for tests package

我知道这个问题已经被问过很多次了,但不知何故我无法克服这个错误。这是我的目录结构-project/pkg/__init__.pysubpackage1/script1.py__init__.pysubpackage2/script2.py__init__.pyscript2.py有:classmyclass:defmyfunction:script1.py有from..subpackage2importscript2我也试过from..subpackage2importmyclass这给了我:ValueError:Attemptedrelativeimportinnon-package

python - 性能比较 : insert vs build Python set operations

在python中,是否更快a)从n个项目的列表构建一个集合b)将n个项目插入集合中?我找到了这个页面(http://wiki.python.org/moin/TimeComplexity),但它没有足够的信息来断定哪个更快。看起来,一次插入一个项目在最坏的情况下可能需要O(n*n)时间(假设它使用字典),而在平均情况下则需要O(n*1)。使用列表初始化集合是否可以提高性能? 最佳答案 就O()复杂度而言-它绝对相同,因为两种方法完全相同-将n项插入集合。差异来自实现:从可迭代对象初始化的一个明显优势是您可以节省大量Python级函数

python - Django-Grappelli:使用参数 'grp_related_lookup' 和关键字参数 '()' 找不到 '{}' 的反向

我使用django-grappelli在管理站点上创建可订购的内联。偶尔(不可重现-大约50%的时间,这特别奇怪),当我尝试从内联中保存顺序时,Django会抛出以下异常:ExceptionType:NoReverseMatchExceptionValue:Reversefor'grp_related_lookup'witharguments'()'andkeywordarguments'{}'notfound.ExceptionLocation:/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.pyi

Python 元类 : Why isn't __setattr__ called for attributes set during class definition?

我有以下python代码:classFooMeta(type):def__setattr__(self,name,value):printname,valuereturnsuper(FooMeta,self).__setattr__(name,value)classFoo(object):__metaclass__=FooMetaFOO=123defa(self):pass我希望元类的__setattr__被FOO和a调用。但是,它根本没有被调用。当我在定义类后将某些内容分配给Foo.whatever时,方法被调用。这种行为的原因是什么?有没有办法拦截在创建类期间发生的分配?在__ne

python - 遍历 pandas 数据帧并更新值 - AttributeError : can't set attribute

我正在尝试遍历pandas数据框并在满足条件时更新值,但出现错误。forline,rowinenumerate(df.itertuples(),1):ifrow.Qty:ifrow.Qty==1androw.Price==10:row.Buy=1AttributeError:can'tsetattribute 最佳答案 首先在pandas中迭代是可能的,但非常慢,因此使用了另一种矢量化解决方案。我想你可以使用iterrows如果你需要迭代:foridx,rowindf.iterrows():ifdf.loc[idx,'Qty']==

python - scipy 优化 fmin ValueError : setting an array element with a sequence

当使用scipy.optimize的fmin时,我收到一个我不明白的错误:ValueError:settinganarrayelementwithasequence.这里有一个简单的平方误差示例来演示:importnumpyasnpfromscipy.optimizeimportfmindefcost_function(theta,X,y):m=X.shape[0]error=X.dot(theta)-yJ=1/(2*m)*error.T.dot(error)returnJX=np.array([[1.,1.],[1.,2.],[1.,3.],[1.,4.]])y=np.array([

python - 属性错误 : '_socketobject' object has no attribute 'set_tlsext_host_name'

在python中,在Ubuntu服务器上,我试图让requests库发出https请求,如下所示:importrequestsrequests.post("https://example.com")首先,我得到了以下信息:/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90:InsecurePlatformWarning:AtrueSSLContextobjectisnotavailable.Thispreventsurllib3fromconfiguringSSLappropr