gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:20:21 25 4
gpt4 key购买 nike

我正在尝试处理 future 值:

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

关于python - Pandas 将日期值存储在错误的列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48862506/

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