为什么会出现错误:
import pandas as pd
a = pd.Series(index=[4,5,6], data=0)
print a.loc[4:5]
a.loc[4:5] += 1
Run Code Online (Sandbox Code Playgroud)
输出:
4 0
5 0
Traceback (most recent call last):
File "temp1.py", line 9, in <module>
dtype: int64
a.loc[4:5] += 1
File "lib\site-packages\pandas\core\indexing.py", line 88, in __setitem__
self._setitem_with_indexer(indexer, value)
File "lib\site-packages\pandas\core\indexing.py", line 177, in _setitem_with_indexer
value = self._align_series(indexer, value)
File "lib\site-packages\pandas\core\indexing.py", line 206, in _align_series
raise ValueError('Incompatible indexer with Series')
ValueError: Incompatible indexer with Series
Run Code Online (Sandbox Code Playgroud)
熊猫0.12。
我正在尝试将属性添加到pandas.DataFrame的子类中,并且它们在酸洗和去除斑点后消失:
import cPickle
import pandas as pd
class MyClass(pd.DataFrame):
def __init__(self):
super(MyClass, self).__init__()
self.bar = 1
myc = MyClass()
with open('myc.pickle', 'wb')as myfile:
cPickle.dump(myc,myfile)
with open('myc.pickle', 'rb')as myfile:
b = cPickle.load(myfile)
print b.bar
Run Code Online (Sandbox Code Playgroud)
输出:
Traceback (most recent call last):
File "test_df.py", line 14, in <module>
print b.bar
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1771, in __getattr__
(type(self).__name__, name))
AttributeError: 'MyClass' object has no attribute 'bar'
Run Code Online (Sandbox Code Playgroud)
知道如何安全地添加属性吗?
a并且b是两个Numpy整数数组.它们是有序的,没有重复.b是一个子集a.我需要找到a每个元素的索引b.是否有一个有效的Numpy函数可以帮助,所以我可以避免python循环?
(实际上,数组是pandas.DatetimeIndex和Numpy一样datetime64,但我想它并没有改变答案.)
Timespan(0,0,secs)和之间的返回值是否有差异Timespan.FromSeconds(secs)?
在我看来,区别在于FromSeconds接受了double.
我需要以下列方式迭代两个列表:
伪代码:
j=1
for i=1 to n:
print a[i], b[j]
while b[j+1] <= a[i]:
j++
print a[i], b[j]
Run Code Online (Sandbox Code Playgroud)
例如:
a = [1 3 5 7]
b = [2 4 9]
Run Code Online (Sandbox Code Playgroud)
期望的输出:
1 2
3 2
5 2
5 4
7 4
Run Code Online (Sandbox Code Playgroud)
你怎么干净地在python中做到这一点?
我将追加到pandas.DataFrame,然后以意外方式转换列的dtype:
import pandas as pd
df=pd.DataFrame({'a':1.0, 'b':'x'}, index=[0])
print df.dtypes
df = df.append({'a':3.0}, ignore_index=True)
print df.dtypes
df = df.append({'a':3.0, 'b':'x'}, ignore_index=True)
print df.dtypes
Run Code Online (Sandbox Code Playgroud)
输出:
a float64
b object
dtype: object
a float64
b object
dtype: object
a object <- ???
b object
dtype: object
Run Code Online (Sandbox Code Playgroud)
而我本来期望一个float64替代那个object。如何避免这种转换?
我正在使用熊猫0.11。
这个最小的代码崩溃了我的Python.(设置:pandas 0.13.0,python 2.7.3 AMD64,Win7.)
import pandas as pd
input_file = r"c3.csv"
input_df = pd.read_csv(input_file)
for col in input_df.columns: # strip whitespaces from string values
if input_df[col].dtype == object:
input_df[col] = input_df[col].apply(lambda x: x.strip())
print 'start'
for idx in range(len(input_df)):
input_df['LL'].iloc[idx] = 3
print idx
print 'finished'
Run Code Online (Sandbox Code Playgroud)
输出:
start
0
Process finished with exit code -1073741819
Run Code Online (Sandbox Code Playgroud)
什么可以防止崩溃:
.strip()从代码中删除.for以意外的方式更改迭代次数,直到崩溃.c3.csv的内容:
Size , B/S , Symbol , Type , BN , Duration , VR , Time , SR …Run Code Online (Sandbox Code Playgroud) 是否有一个内置的方法可以帮助我有效地实现以下目标:给定一个数组,我需要一个数组列表,每个数组都有索引到数组的不同唯一值?
如果f是所需的功能,
b = f(a)
Run Code Online (Sandbox Code Playgroud)
和
u, idxs = unique(a)
Run Code Online (Sandbox Code Playgroud)
然后
b[i] == where(idxs==i)[0]
Run Code Online (Sandbox Code Playgroud)
我知道pandas.Series.groupby()可以做到这一点,但是当有超过10 ^ 5个唯一整数时创建一个dict可能没有效率.
码:
class MyClass:
def __init__(self, aa ):
print('aa='+str(aa)+' of type '+str(type(aa)))
self.aa = aa,
print('self.aa='+str(self.aa)+' of type '+str(type(self.aa)))
DEBUG = MyClass(aa = 'DEBUG')
Run Code Online (Sandbox Code Playgroud)
输出:
aa=DEBUG of type <type 'str'>
self.aa=('DEBUG',) of type <type 'tuple'>
Run Code Online (Sandbox Code Playgroud)
为什么会self.aa成为元组而不是字符串?
在Numpy中,我试图获取数组中每个元素的时间datatime64。
我可以选择一个新数组,timedelta64其中包含每个元素从一天开始以来经过的时间。
我已经尝试过使用numpy.datetime_as_string,但是我不知道如何操作字符串。
如何将变量从Python转换datetime.timedelta为numpy.timedelta64?
是否有类似的方法datetime.datetime.strptime(),接受类似的字符串'16:00'并返回一个datetime.time(16,0)对象(即一个只保留时间而不是日期的对象)?
编辑:我可以使用datetime.datetime.strptime(),但它会返回datetime.datetime,我只想要时间,而不是日期.