Ad

Pandas Storing The Date Values In Wrong Column

- 1 answer

I am trying to process the future values:

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)

I am getting the code running properly. After saving the dataframe I came to know that the data is not in a proper format:

        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

I get the date values in the serial numbers column, which is disturbing the data visualization. How to make it proper in position with the continuous serials?

Ad

Answer

It seems in original DataFrame is not DatetimeIndex, so add it by 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')

Idea for improving code:

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

to:

df.loc[next_date, df.columns[-1]] = i
Ad
source: stackoverflow.com
Ad