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

时间:2018-02-19 09:01:20

标签: python pandas python-2.x

我正在尝试处理未来的价值观:

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

我在序列号列中获取日期值,这会干扰数据可视化。如何使用连续序列使其正确定位?

1 个答案:

答案 0 :(得分:3)

原始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