# First Differencing to remove seasonality and trends

I am trying to remove seasonality and trends from my time series data. I found this post that said to use df_diff = df.diff().diff(12).dropna() (https://www.tobiolabode.com/blog/2020/12/30/how-to-convert-non-stationary-data-into-stationary-for-arima-model-with-python). I don't understand why we need to use diff(12) from pandas. Could someone explain this to me? Thanks!

• Before copying any code, how do you know your data has some sort of seasonality? Can you show us? I would also recommend reading what diff does and finally Google ARIMA and in particular Autocorrelation. May 23, 2021 at 22:15

Since the data is recorded every month (i.e. a data point for each month in the year) and we see a yearly seasonality trend we compare the data for the same month against previous year. This is done by taking the difference against the data from last year (.diff()), which is equal to going back 12 observations since each observation is data for a specific month.
• The .diff() method in general takes the difference between the current row and some previous row (depending on the value provided for the periods argument). Since there is a yearly seasonality we want the difference compared to 12 months (which is 12 observations in this case) back, and therefore use the value of 12 for the periods argument and use .diff(12). May 22, 2021 at 16:41