0

My current data is in the following monthly format:

DATE         HPI_F_ARIMA 
1987-01-01     63.967000
1987-02-01     64.42600
1987-03-01     64.736000             ...
...             ...
...             ...
2021-12-01      236.078323

--------Code so far----------

import pandas as pd
import matplotlib.pyplot as plt


if __name__ == '__main__':
    usdata = pd.read_csv('arima_forecast.csv')
    usdata['DATE'] = pd.to_datetime(usdata['DATE'])
    usdata.set_index('DATE', inplace = True)

print('monthly format')
print(usdata)

Question 1: How do I convert this so it's in the quarterly format whereby the monthly values are averaged for the relevant months in a quarter and Does the DATE column have the last date value of each quarter? The end goal is to get data in the following format, save it to a dataframe and then save the dataframe to a CSV file.

DATE         HPI_F_ARIMA 
1987-03-31     64.37
1987-06-30     ...
1987-09-30     ...
...             ...
...             ...
2021-12-31      ...

Also, how do I attach the CSV file I am using?

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
UBS
  • 35
  • 1
  • 4
  • You don’t need to attach the csv file, see [how to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) (for future questions) and [what to do when someone answers](https://stackoverflow.com/help/someone-answers) – Cimbali Aug 11 '21 at 11:28

1 Answers1

0

That’s exactly what .resample() does, with Q for quarterly:

>>> usdata.resample('Q').mean()
            HPI_F_ARIMA
DATE                   
1987-03-31    64.376333
1987-06-30          NaN
1987-09-30          NaN
1987-12-31          NaN
1988-03-31          NaN
...                 ...
2020-12-31          NaN
2021-03-31          NaN
2021-06-30          NaN
2021-09-30          NaN
2021-12-31   236.078323

[140 rows x 1 columns]
Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • You’re welcome @UBS, see also [what to do when someone answers](https://stackoverflow.com/help/someone-answers) – Cimbali Aug 13 '21 at 15:08
  • 1
    Ok I added feedback (accepted response). Still a newbie in Python and StackOverflow. But trying to learn fast (I think). – UBS Aug 14 '21 at 17:28