数据帧的时间序列丢失了值

时间:2015-02-23 10:49:38

标签: python pandas merge time-series

我有2个数据框,df1df2大小相同,df1date列,df2float柱。当我执行以下操作时:

series = pandas.Series(df2['float'], df1['date'])

时间序列上的值均为NaN。我在检查之前检查过两个数据帧都有数据。我做错了什么?

1 个答案:

答案 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