将数组作为列附加到数据框或根据其他数据框的日期创建新数据框

问题描述

首先,我想说有很多类似的问题,我花了将近 2 天的时间寻找并尝试解决我的问题,使用所有功能但找不到我需要的东西,即使我相信会有一个非常简单的解决方案。

完整代码

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

mt = pd.Series([34.678714,34.087302,33.857141,33.250000,33.124999,31.818181,31.082676,29.107807,30.144405],index=['2019-12-31','2020-01-02','2020-01-03','2020-01-06','2020-01-07','2020-01-08','2020-01-09','2020-01-10','2020-01-13'])

mn = np.array([ 7.76179772 16.68166719 23.3037243,27.30909839,29.68638615,30.56226802,30.77646665,30.08922891,30.11195783])

plt.figure(figsize=(10,6))
print(mt)
print(mn)
mt.plot()
plt.show()

我明白了

plot(1)

打印我的结果是:

print(mt)
Date
2019-12-31    34.678714
2020-01-02    34.087302
2020-01-03    33.857141
2020-01-06    33.250000
2020-01-07    33.124999
2020-01-08    31.818181
2020-01-09    31.082676
2020-01-10    29.107807
2020-01-13    30.144405

print(mn)
[ 7.76179772 16.68166719 23.3037243  27.30909839 29.68638615 30.56226802 30.77646665 30.08922891 30.11195783 ... ]

我需要将 mn 数组添加mt 系列并将它们与 mt 的日期索引一起绘制。所以它看起来像这样: (第一个问题是如何合并上面的序列和数组,使其看起来像下面)

    print(mt)
Date          actual      est
2019-12-31    34.678714  7.76179772 
2020-01-02    34.087302  16.68166719 
2020-01-03    33.857141  23.3037243  
2020-01-06    33.250000  27.30909839 
2020-01-07    33.124999  29.68638615 
2020-01-08    31.818181  30.56226802  
2020-01-09    31.082676  30.77646665 
2020-01-10    29.107807  30.08922891 
2020-01-13    30.144405  30.11195783 

最后也是更重要的问题,如何将 mt(带跳跃日期)和 mn(不带日期索引)一起绘制成类似

this plot(2)

(x 轴作为日期)

我使用了 hstack、join、append、insert、add、asarray 以及许多其他甚至不记得的东西。也许他们用错了,真的可以接受各种答案。

解决方法

最简单的方法是像这样使用 pd.concat

mt = pd.Series(
    [34.678714,34.087302,33.857141,33.250000,33.124999,31.818181,31.082676,29.107807,30.144405],index=['2019-12-31','2020-01-02','2020-01-03','2020-01-06','2020-01-07','2020-01-08','2020-01-09','2020-01-10','2020-01-13'],name='mt'  # Added for next step
)

mn = np.array([ 7.76179772,16.68166719,23.3037243,27.30909839,29.68638615,30.56226802,30.77646665,30.08922891,30.11195783])


# Combine data:
combined_data = pd.concat([
    mt,pd.Series(data=mn,index=mt.index,name='mn')
],axis=1)

#               mt          mn
# 2019-12-31    34.678714   7.761798
# 2020-01-02    34.087302   16.681667
# 2020-01-03    33.857141   23.303724
# 2020-01-06    33.250000   27.309098
# 2020-01-07    33.124999   29.686386
# 2020-01-08    31.818181   30.562268
# 2020-01-09    31.082676   30.776467
# 2020-01-10    29.107807   30.089229
# 2020-01-13    30.144405   30.111958

# Plot data:
combined_data.plot(marker='o',figsize=(12,4.8))

生成此图的工具:enter image description here

额外提示,您当前使用的是字符串索引,即使它们表示日期。您可以像这样将其转换为 pd.DatetimeIndex

$ combined_data.index = pd.to_datetime(combined_data.index)
$ combined_data.index

DatetimeIndex(['2019-12-31',dtype='datetime64[ns]',freq=None)

这个功能也非常有用pd.date_range

$ pd.date_range('2019-12-31','2020-01-13',freq='1B') # 1B = 1 business day
DatetimeIndex(['2019-12-31','2020-01-01',freq='B')