0

I have the following dataframe

df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
}) 

I want to make a time series based on this df. How can i change the year from int to a datetimeindex so i can plot a timeseries?

Tamarie
  • 125
  • 2
  • 6
  • 18

2 Answers2

0
df = pd.DataFrame({
'date': [1988, 1988, 2000, 2005],
'value': [2100, 4568, 7896, 68909]
}) 

date = []
for year in  df.date:
    date.append(pd.datetime(year,1,1))
df.index=date
df['value'].plot()

enter image description here

Guinther Kovalski
  • 1,629
  • 1
  • 7
  • 15
  • for loop here is innecesary https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html accept `Series` like arg – ansev Feb 20 '20 at 16:49
0

Use: pd.to_datetime to convert to datetime. DataFrame.set_index in order to get and plot the Series. You can plot it with Series.plot

(df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
   .set_index('date')['value']
   .plot())

If you want keep the series use:

s = (df.assign(date = pd.to_datetime(df['date'],format = '%Y'))
       .set_index('date')['value'])

and then:

s.plot()

enter image description here

ansev
  • 30,322
  • 5
  • 17
  • 31