我有2个数据框,df1
和df2
大小相同,df1
有date
列,df2
有float
柱。当我执行以下操作时:
series = pandas.Series(df2['float'], df1['date'])
时间序列上的值均为NaN
。我在检查之前检查过两个数据帧都有数据。我做错了什么?
答案 0 :(得分:3)
对我来说这看起来像个错误:
In [29]:
df = pd.DataFrame({'date':[dt.datetime.now()]})
print(df)
df1 = pd.DataFrame({'float':[2.1145]})
df1
date
0 2015-02-23 11:23:03.550538
Out[29]:
float
0 2.1145
In [30]:
pd.Series(df1['float'], df['date'])
Out[30]:
date
2015-02-23 11:23:03.550538 NaN
Name: float, dtype: float64
如果您调用属性.values
:
In [31]:
pd.Series(df1['float'].values, df['date'])
Out[31]:
date
2015-02-23 11:23:03.550538 2.1145
dtype: float64
这看起来像是一般错误:
In [34]:
df3 = pd.DataFrame({'int':[323]})
df3
Out[34]:
int
0 323
以下组合均失败:
In [39]:
pd.Series(df2['float'], df3['int'])
Out[39]:
int
323 NaN
Name: float, dtype: float64
In [40]:
pd.Series(df['date'], df2['float'])
Out[40]:
float
1.21554 NaT
Name: date, dtype: datetime64[ns]
In [41]:
pd.Series(df3['int'], df2['float'])
Out[41]:
float
1.21554 NaN
Name: int, dtype: float64