草庐IT

try-convert

全部标签

python - 使用 map : A value is trying to be set on a copy of a slice from a DataFrame 时的 Pandas 警告

我有以下代码并且可以正常工作。这基本上重命名了列中的值,以便以后可以合并它们。pop=pd.read_csv('population.csv')pop_recent=pop[pop['Year']==2014]mapping={'Korea,Rep.':'SouthKorea','Taiwan,China':'Taiwan'}f=lambdax:mapping.get(x,x)pop_recent['CountryName']=pop_recent['CountryName'].map(f)Warning:Avalueistryingtobesetonacopyofaslicefrom

python - 为什么在使用 matplotlib 的 DateFormatter 格式化 x 轴上的日期时会出现 "python int too large to convert to C long"错误?

正在关注thisanswer'suseofDateFormatter,我尝试使用pandas0.15.0和matplotlib1.4.2绘制时间序列并用年份标记其x轴:importdatetimeasdtimportmatplotlibasmplimportmatplotlib.pyplotaspltimportpandas.io.dataaspdioimportscipyasspt1=dt.datetime(1960,1,1)t2=dt.datetime(2014,6,1)data=pdio.DataReader("GS10","fred",t1,t2).resample("Q",h

python - 在使用 try 和 except 语句赋值之前引用的局部变量

这个问题在这里已经有了答案:localvariable'servers'referencedbeforeassignment(3个答案)关闭9年前。我在try和except语句中遇到了一些问题,我有一个输入字符串的条目小部件,但我有稍后将其转换为整数的代码,问题是如果用户输入类似文本的内容,它会抛出像这样的错误:ExceptioninTkintercallbackTraceback(mostrecentcalllast):File"/usr/lib/python3.2/tkinter/__init__.py",line1402,in__call__returnself.func(*ar

python - numpy.ndarray : converting to a "normal" class

[python3]我喜欢ndarray但我觉得用起来很烦人。这是我面临的一个问题。我想编写classArray来继承ndarray的大部分功能,但只有一种实例化方式:作为特定大小的零填充数组。我希望写:classArray(numpy.ndarray):def__init__(size):#Whatdohere?我想用一些参数调用super().__init__来创建一个零填充数组,但它不会工作,因为ndarray使用全局函数numpy.zeros(而不是构造函数)来创建一个零填充数组。问题:为什么ndarray在很多情况下使用全局(模块)函数而不是构造函数?如果我试图在面向对象的环境

python - Pandas 数据帧 : SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

这个问题在这里已经有了答案:HowtodealwithSettingWithCopyWarninginPandas(20个答案)关闭3年前。我知道有很多关于此警告的帖子,但我找不到解决我的情况的方法。这是我的代码:df.loc[:,'my_col']=df.loc[:,'my_col'].astype(int)#df.loc[:,'my_col']=df.loc[:,'my_col'].astype(int).copy()#df.loc[:,'my_col']=df['my_col'].astype(int)它产生警告:SettingWithCopyWarning:Avalueistr

python - 使用 Try & Except Python 2.7.1 的文件打开功能

defFileCheck(fn):try:fn=open("TestFile.txt","U")exceptIOError:print"Error:Filedoesnotappeartoexist."return0我正在尝试制作一个函数来检查文件是否存在,如果不存在,则它应该打印错误消息并返回0。为什么这不起作用??? 最佳答案 如果你想从exceptblock中返回,你需要缩进return0。另外,您的论点没有做任何事情。我假设您希望此函数能够测试任何文件,而不是为其分配文件句柄?如果不是,则不需要任何参数。defFileChec

python - Python 中嵌套的 try/except

try:commandstry:commandstry:commandstry:commandsexcept:commandsreturntofinalcommandsexcept:commandsreturntofinalcommandsexcept:commandsreturntofinalcommandsexcept:commandsfinalcommands我应该编写哪条指令来代替returntofinalcommands以使任何except都在外部try之后返回到顶级指令?它是可接受的结构吗?编辑:这是一个玩具示例(我知道我可以使用if来完成它,这只是一个示例;假设您必须使用

python - 类型错误 : only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

我想编写一个函数,根据提供的bin概率从训练集中随机挑选元素。我将集合索引分成11个bin,然后为它们创建自定义概率。bin_probs=[0.5,0.3,0.15,0.04,0.0025,0.0025,0.001,0.001,0.001,0.001,0.001]X_train=list(range(2000000))train_probs=bin_probs*int(len(X_train)/len(bin_probs))#extendprobabilitiesacrossbinelementstrain_probs.extend([0.001]*(len(X_train)-len(

javascript - "convert foo to bar"的函数名称约定

我有一个非常常见的模式“给定一个Foo,返回一个Bar”,例如,给定一个user_id,返回一个用户。这些类型的函数是否有约定俗成的命名模式?正在关注JoelonSoftware,我个人使用了很多bar_from_foo(),但我很少看到其他人这样做,它很快就变得冗长,例如widgets=user_widgets_from_user(user_from_param_map(params))是否有任何一种流行语言的常规命名方式或命名空间(例如User.from_map())?我对Python特别感兴趣,但您能想到的任何语言都会很有用。 最佳答案

c++ - try..catch cython 中等效的宏包装器

我包装了大量的C++函数,如果底层套接字连接丢失,这些函数会引发异常。虽然我已经想出如何包装我的“获取连接”功能以重新建立连接和/或尝试列表中的其他可用服务器,但我无法找到一个解决方案来创建一个try..except包装器以提供给80+C++函数。#--client.pxd---cdefexternfrom"rpc/RpcService.h":cdefcppclassRpcServiceClient:voidgetProject(ProjectT&,Guid&id)nogilexcept+cdefexternfrom"client.h":cdefcppclassClient:RpcSe