2016-02-20 102 views
1

我是工作在時間序列追加的問題,我發現非常奇特的行爲大熊貓數據幀時間序列與大熊貓數據幀

以下時,指數是不是時間序列

import pandas as pd 
df = pd.DataFrame({"a":[1,2,3], "b":[31,41,51],"c":[31,52,23]}, index=["z", "y", "x"]) 
df1 = pd.DataFrame({"a":[41,55,16]}, index=["w", "v", "u"]) 
df2 = pd.DataFrame({"b":[24,3,57]}, index=["w", "v", "u"]) 
df3 = pd.DataFrame({"c":[111,153,123]}, index=["w", "v", "u"]) 
df = df.append(df1) 
dfx.ix[df2.index, "b"] = df2 

輸出代碼工作df

a b c 
z 1 31 31 
y 2 41 52 
x 3 51 23 
w 41 24 NaN 
v 55 3 NaN 
u 16 57 NaN 

然而,這並不時有datetime64[ns]指數或工作時,尺寸太大

除了下面的命令工作,當有datetime64[ns]指數

df = df.append(df1) 
df["b"][df2.index] = df2 

爲什麼它是什麼?

回答

1

您可以嘗試fillna

df = df.append(df1) 
print df.fillna(df2) 
    a b c 
z 1 31 31 
y 2 41 52 
x 3 51 23 
w 41 24 NaN 
v 55 3 NaN 
u 16 57 NaN 

Datatimeindex測試它和它的作品非常好:

import pandas as pd 

df = pd.DataFrame({"a":[1,2,3], "b":[31,41,51],"c":[31,52,23]}, index=["z", "y", "x"]) 
df.index = pd.date_range('20160101',periods=3,freq='T') 

df1 = pd.DataFrame({"a":[41,55,16]}, index=["w", "v", "u"]) 
df1.index = pd.date_range('20160104',periods=3,freq='T') 

df2 = pd.DataFrame({"b":[24,3,57]}, index=["w", "v", "u"]) 
df2.index = pd.date_range('20160104',periods=3,freq='T') 

df3 = pd.DataFrame({"c":[111,153,123]}, index=["w", "v", "u"]) 
df3.index = pd.date_range('20160104',periods=3,freq='T') 
df = df.append(df1) 
print df 
         a b c 
2016-01-01 00:00:00 1 31 31 
2016-01-01 00:01:00 2 41 52 
2016-01-01 00:02:00 3 51 23 
2016-01-04 00:00:00 41 NaN NaN 
2016-01-04 00:01:00 55 NaN NaN 
2016-01-04 00:02:00 16 NaN NaN 

print df.fillna(df2) 
         a b c 
2016-01-01 00:00:00 1 31 31 
2016-01-01 00:01:00 2 41 52 
2016-01-01 00:02:00 3 51 23 
2016-01-04 00:00:00 41 24 NaN 
2016-01-04 00:01:00 55 3 NaN 
2016-01-04 00:02:00 16 57 NaN 

df.ix[df2.index, "b"] = df2 
print df 
         a b c 
2016-01-01 00:00:00 1 31 31 
2016-01-01 00:01:00 2 41 52 
2016-01-01 00:02:00 3 51 23 
2016-01-04 00:00:00 41 24 NaN 
2016-01-04 00:01:00 55 3 NaN 
2016-01-04 00:02:00 16 57 NaN 
+0

它工作得很好'熊貓:0.17.1' 。什麼是「熊貓」的版本?檢查它'print pd.show_versions()'。如果你的記憶體很小而且'df'太大,可能會出現下一個問題。 – jezrael