gpt4 book ai didi

python - pandas 数据框中带有附加问题的时间序列

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:45 24 4
gpt4 key购买 nike

我正在研究时间序列,我发现 pandas 数据框中有非常奇怪的行为

当索引不是时间序列时,以下代码有效

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

为什么会这样?

最佳答案

您可以尝试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

关于python - pandas 数据框中带有附加问题的时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com