Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas -- append data to series while increasing datetime index

I have a dataframe

import pandas as pd
import datetime

#Create data
df = {'Day': ['2012-12-29','2013-01-05'],
      'Whatev': [1,0]}
index = range(len(df['Day']))
columns = ['Day','Whatev']
df = pd.DataFrame(df,  index=index, columns=columns)
print(df)
print()

From which I extract the first column as a series:

#Index to datetime
df['Day'] = pd.to_datetime(df['Day'])
df.set_index('Day', inplace=True) #use day as index


#Create series from dataframe column
s = df[df.columns[0]]
print(s)
print()

To which I would like to append zero values at the end. So first I extend the datetime index and then specify the last value as zero:

#Append 1 data to series
last_date = s.iloc[[-1]].index + datetime.timedelta(days=7)
s.append(pd.DataFrame(index=[last_date]))
s.iloc[[-1]]=0
print(s)
print()

Yes, I did not work. During testing, it seemed to work but here, somehow, the dataframe is unchanged. Which is the reason?

Also, is there a smarter way to achieve the result? In fact, if I want to append more than one value, my solution does not look pretty:

#Append many data to series
L = 2 #hoe many zeros
i=0
while i<=L:
    i+=1
    last_date = s.iloc[[-1]].index + datetime.timedelta(days=7)
    s.append(pd.DataFrame(index=[last_date]))   
    s.iloc[[-1]]=0
print(s)
like image 418
shamalaia Avatar asked Jan 23 '19 14:01

shamalaia


People also ask

How do I append data to Pandas series?

Pandas Series: append() functionThe append() function is used to concatenate two or more Series. Series to append with self. If True, do not use the index labels. If True, raise Exception on creating index with duplicates.

How do you increment index of a data frame?

To increase your index with 1 you can simply modify the index like this, df. index += 1 . Note: df. index += 1 results in a IntXXIndex whereas df.

How can you change the index of a panda series?

rename() function in the pandas Series functionalities, which is used to change the series index labels or to change the name of the series object.

Can Pandas series have index?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.


1 Answers

You can simplify your code with setting-with-enlargement:

df['Day'] = pd.to_datetime(df['Day'])
s = df.set_index('Day')['Whatev']

Loop version:

L = 2 #hoe many zeros
i=0
while i<=L:
    i+=1
    last_date = s.index[-1] + pd.Timedelta(days=7)
    s.loc[last_date]=0
print(s)


2012-12-29    1
2013-01-05    0
2013-01-12    0
2013-01-19    0
2013-01-26    0
Name: Whatev, dtype: int64

Non loop version:

#create DatetimeIndex by last value of index and remove first value by indexing
idx = pd.date_range(s.index[-1], periods=4, freq='7d')[1:]
#append Series to original
s = s.append(pd.Series(np.repeat(0, len(idx)), index=idx))
print(s)

2012-12-29    1
2013-01-05    0
2013-01-12    0
2013-01-19    0
2013-01-26    0
dtype: int64
like image 106
jezrael Avatar answered Oct 20 '22 05:10

jezrael