Pandas 将日期值存储在错误的列中

Pandas storing the date values in wrong column

我正在尝试处理未来值:

last_date = df.iloc[-1]
print(last_date)
last_unix = last_date.Timestamp
# one_day = 86400
one_minute = 60
next_unix = last_unix + one_minute
matplotlib.rc('figure', figsize=(20, 10))
for i in forecast_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
#     next_date = next_unix
    next_unix += 60
    df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)]+[i]
#     print(next_unix)

我正在正确获取代码 运行。保存数据框后,我发现数据格式不正确:

        Timestamp   Adj. Open   Adj. High   Adj. Low    Adj. Close  Adj. Volume label   Forecast

35866   1518744240  10356.7 10383.1 10356.7 10383.1 0.99564597  10674.5 
35867   1518744300  10398.9 10398.9 10373.1 10397   0.17246706  10637.9 
35868   1518744360  10397   10409.9 10387.5 10409.9 0.91689198  10692.3 
35869   1518744420  10397.3 10408.1 10381.2 10406.3 2.2375806   10691.2 
2018-02-16 06:58:00                             10846.7419537654
2018-02-16 06:59:00                             10842.8747135627
2018-02-16 07:00:00                             10832.5305557475
2018-02-16 07:01:00                             10840.6966663947
2018-02-16 07:02:00                             10833.9536747933

我在序列号列中得到了日期值,这扰乱了数据可视化。如何在连载中做到位?

原来的DataFrame好像不是DatetimeIndex,所以通过set_index添加:

last_unix = last_date.Timestamp
#convert column to datetime if necessary 
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
#create DatetimeIndex
df = df.set_index('Timestamp')

改进代码的想法:

df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)]+[i]

至:

df.loc[next_date, df.columns[-1]] = i